社区讨论
5个超时一个wa,求助大佬
P2014[CTSC1997] 选课参与者 3已保存回复 9
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 9 条
- 当前快照
- 1 份
- 快照标识符
- @mi6xu2bf
- 此快照首次捕获于
- 2025/11/20 12:35 4 个月前
- 此快照最后确认于
- 2025/11/20 12:35 4 个月前
C
#include <cstdio>
#include <cstring>
#include <algorithm>
#define rg register
#define gc getchar()
#define max MAX
#define _ 0
const int maxn = 20010;
int n,V,value[maxn],weight[maxn],cnt=0,g[maxn<<2],f[maxn<<2],h[65][3],w[65][3];
inline int MAX(int a,int b) { return a > b ? a : b;}
inline int rd(){
int n=0,f=1; char c=gc;
while(c>'9'||c<'0') {if(c=='-') f=-1;c=gc;}
while(c>='0'&&c<='9') { n=(n<<3)+(n<<1)+c-'0';c=gc;}
return n*f;
}
inline void add(int x,int y){ //cnt为主件个数
weight[++cnt]=x;value[cnt]=x*y;
}
int main(){
V=rd(),n=rd();
for(rg int i=1;i<=n;i++){
int x=rd(),y=rd(),z=rd();
if(z==0){ add(x,y); }
else if(z>0){
h[z][0]++; //附件个数
h[z][h[z][0]]=x; //价格
w[z][h[z][0]]=x*y; //价值
}
}
for(rg int i=1;i<=cnt;i++){ //枚举主件
memset(g,0,sizeof(g)); //清零
for(rg int k=1;k<=h[i][0];k++){ //枚举附件
for(rg int j=V-weight[i];j>=h[i][k];j--)
g[j]=max(g[j],g[j-h[i][k]]+w[i][k]);
}
g[V]=g[V-weight[i]]+value[i];
for(rg int j=V;j>=0;j--)
for(rg int k=weight[i];k<=j;k++)
f[j]=max(f[j],f[j-k]+value[i]+g[k-weight[i]]);
}
printf("%d\n",f[V]);
return ~~(0^_^0);
}
回复
共 9 条回复,欢迎继续交流。
正在加载回复...