QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#148696 | #2325. Corns | sakuya726 | AC ✓ | 47ms | 11856kb | C++14 | 2.8kb | 2023-08-23 17:37:46 | 2023-08-23 20:26:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define rg register
#define maxn 215300
#define ll long long
#define IT set<node>::iterator
#define int ll
#pragma GCC optimize(3)
#pragma -fcrossjumping
#pragma -fdefer-pop
#pragma -fmerge-constans
#pragma -fthread-jumps
#pragma -floop-optimize
#pragma -fif-conversion
#pragma -fif-conversion2
#pragma -fdelayed-branch
#pragma -fguess-branch-probability
#pragma -fcprop-registers
#pragma -fforce-mem
#pragma -foptimize-sibling-calls
#pragma -fstrength-reduce
#pragma -fgcse
#pragma -fcse-follow-jumps
#pragma -frerun-cse-after-loop
#pragma -fdelete-null-pointer-checks
#pragma -fextensive-optimizations
#pragma -fregmove
#pragma -fschedule-insns
#pragma -fsched-interblock
#pragma -fcaller-saves
#pragma -fpeephole2
#pragma -freorder-blocks
#pragma -fstrict-aliasing
#pragma -funit-at-a-time
#pragma -falign-functions
#pragma -fcrossjumping
#pragma -finline-functions
#pragma -fweb
#pragma -fgcse-after-reload
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-') f=-1;
c=getchar();
}
while(c<='9'&&c>='0')
{
x=(x<<1)+(x<<3)+(c^48);
c=getchar();
}
return x*f;
}
inline int max(int a,int b)
{
if(a>b) return a;
return b;
}
int n,m,nn;
bool vis[maxn];
int val[maxn],w[maxn],num[maxn],num1[maxn];
int f[maxn];
signed main()
{
// freopen("data.out","r",stdin);
nn=read(),m=read();
for(rg int i=1;i<=nn;++i)
{
w[i]=read();//物品体积、价值和数量
num1[w[i]]++;
}
for(rg int i=1;i<=nn;++i)
{
if(vis[w[i]]==0)
{
vis[w[i]]=1;
w[++n]=w[i];
val[n]=w[i];
num[n]=num1[w[i]];
}
}
for(rg int i=1;i<=n;++i)
{
if(num[i]*w[i]>=m)
{//这种情况可以理解成该种物品有无限个,用完全背包的做法即可
for(rg int j=w[i];j<=m;++j) f[j]=max(f[j],f[j-w[i]]+val[i]);
}
else
{ //这种情况下我们将num[i]进行二进制的拆分
/*
任何一个整数都可以分解成用2的次方表示的数
1,2,4,8,16...2^n
比如7=2^0+2^1+2^2
那么对于num[i],我们可以把其分解成一共log(num[i])个数,把每个数看作数量只有1的个体
由于[0,num[i]]之间的任意一个数都可以用这些二进制数来表示,所以我们可以处理所有的情况
同时时间复杂度从num[i]->log(num[i])
*/
int now=1;//从1开始
while(num[i]>now)
{
int cost=now*w[i],money=now*val[i];
for(rg int j=m;j>=cost;--j) f[j]=max(f[j],f[j-cost]+money);
num[i]-=now;
now<<=1;
}
if(num[i]!=0)//这里可能会有剩余
{
int cost=num[i]*w[i],money=num[i]*val[i];
for(rg int j=m;j>=cost;--j) f[j]=max(f[j],f[j-cost]+money);
num[i]=0;
}
}
}
cout<<f[m];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9728kb
input:
3 10 1 1 11
output:
2
result:
ok single line: '2'
Test #2:
score: 0
Accepted
time: 0ms
memory: 9556kb
input:
4 10 3 5 3 4
output:
10
result:
ok single line: '10'
Test #3:
score: 0
Accepted
time: 5ms
memory: 9884kb
input:
30669 199323 7 6 7 7 7 7 6 7 7 7 7 6 7 6 6 7 7 7 7 6 6 7 7 7 6 6 6 7 7 7 6 6 7 7 6 6 7 6 7 6 7 6 6 6 6 6 7 7 7 6 6 6 6 6 6 6 7 7 6 6 7 6 6 7 6 6 6 6 6 7 7 7 7 6 7 7 6 6 7 6 7 6 6 6 6 7 6 7 6 7 6 7 6 7 7 7 7 6 7 7 6 6 6 6 7 6 7 7 7 7 6 7 7 7 7 7 7 6 6 6 7 7 7 6 7 7 7 7 6 7 7 6 6 6 6 7 6 7 7 7 6 6 7 7...
output:
199318
result:
ok single line: '199318'
Test #4:
score: 0
Accepted
time: 0ms
memory: 11320kb
input:
22122 199097 9 8 8 10 9 8 10 9 9 9 8 9 8 9 10 8 9 10 10 10 10 10 9 9 10 10 10 10 10 9 8 8 8 10 9 8 9 9 8 10 9 10 10 10 9 8 8 9 10 10 8 9 9 10 9 10 9 9 9 9 9 10 9 8 9 10 9 9 10 10 8 10 8 8 9 10 8 10 10 8 8 9 8 9 8 10 10 10 10 8 8 8 10 9 8 9 8 9 10 9 8 8 10 9 8 10 10 8 9 10 8 10 9 8 10 9 8 9 8 9 9 8 9...
output:
199089
result:
ok single line: '199089'
Test #5:
score: 0
Accepted
time: 3ms
memory: 10116kb
input:
120000 200000 1 2 1 1 1 2 1 2 2 1 1 1 1 1 1 2 1 1 2 2 1 2 1 2 1 1 2 1 2 1 2 2 1 1 1 1 1 2 2 2 2 2 1 1 2 2 2 2 2 1 1 1 1 1 2 2 2 2 1 1 1 2 2 2 1 1 2 1 2 2 1 1 2 2 2 2 2 2 1 2 1 1 2 1 2 1 2 2 1 2 1 1 2 2 1 1 1 1 1 1 2 1 1 2 1 1 1 1 2 2 1 2 1 1 1 1 1 1 1 2 2 1 1 1 1 2 2 2 1 1 2 1 2 1 2 1 2 1 2 2 1 2 1 ...
output:
180055
result:
ok single line: '180055'
Test #6:
score: 0
Accepted
time: 29ms
memory: 11752kb
input:
1243 152688 41 47 45 47 50 37 33 911 42 510 46 33 753 46 47 31 37 50 47 31 39 45 182 48 49 42 40 31 48 807 37 30 38 48 865 43 46 31 42 40 42 46 30 44 31 39 42 45 38 47 47 299 46 598 43 480 40 34 638 37 38 47 210 37 32 38 38 36 50 32 49 494 461 37 34 31 47 42 825 46 44 35 40 225 36 44 231 43 38 40 40...
output:
152688
result:
ok single line: '152688'
Test #7:
score: 0
Accepted
time: 31ms
memory: 10964kb
input:
1241 158399 30 47 914 37 37 31 258 539 31 865 50 47 49 41 107 39 40 704 39 195 30 48 591 557 37 48 32 45 32 31 39 979 42 975 49 32 35 50 38 565 41 34 47 48 30 34 868 993 50 41 50 270 42 535 35 43 36 35 48 72 34 49 45 46 44 48 36 50 48 34 40 972 48 33 730 34 44 30 50 48 35 35 523 668 48 43 48 32 46 3...
output:
158399
result:
ok single line: '158399'
Test #8:
score: 0
Accepted
time: 30ms
memory: 9860kb
input:
1243 157456 31 32 41 37 42 45 42 841 728 46 31 34 43 50 49 48 33 35 39 589 38 39 31 45 754 771 32 687 35 41 46 3 49 37 35 40 36 38 49 733 37 49 849 50 43 34 42 30 31 44 41 39 188 45 194 103 48 33 44 35 36 34 36 44 41 591 40 32 41 36 96 44 49 31 37 46 46 39 37 573 35 63 49 36 34 46 39 46 41 971 38 32...
output:
157456
result:
ok single line: '157456'
Test #9:
score: 0
Accepted
time: 25ms
memory: 10128kb
input:
1244 154207 48 45 36 43 46 49 49 38 871 43 685 38 44 294 38 49 33 35 627 48 564 48 30 32 46 453 43 30 33 46 36 47 440 44 44 37 48 38 34 48 44 37 50 46 37 49 43 115 43 39 419 774 46 45 38 43 35 50 224 832 157 31 34 50 101 46 35 33 49 754 41 39 42 34 49 44 23 626 32 38 39 45 44 33 39 48 48 46 43 46 38...
output:
154207
result:
ok single line: '154207'
Test #10:
score: 0
Accepted
time: 31ms
memory: 10172kb
input:
1244 157697 42 37 159 40 50 31 42 50 899 950 30 41 48 43 37 43 37 36 50 47 41 31 49 35 137 30 144 40 50 38 30 38 38 90 32 49 825 38 32 49 50 31 45 217 40 43 42 43 40 46 42 285 31 977 36 35 34 40 968 47 34 49 39 34 49 270 35 37 60 988 32 35 50 631 33 35 49 521 32 32 33 48 560 803 593 982 49 40 43 41 ...
output:
157697
result:
ok single line: '157697'
Test #11:
score: 0
Accepted
time: 47ms
memory: 11100kb
input:
7060 171395 46 9 7 17 30 48 50 17 7 17 17 18 20 9 31 2 41 17 15 11 11 80 9 3 30 18 3 5 7 7 37 4 17 44 18 14 8 5 1 71 39 19 19 3 20 20 9 17 10 10 34 18 37 10 30 9 43 3 18 8 14 15 37 13 4 19 17 15 11 15 4 11 6 18 5 17 4 13 20 50 14 2 65 5 15 18 5 3 9 9 12 8 4 42 37 62 13 16 10 46 15 17 79 7 14 14 20 3...
output:
171395
result:
ok single line: '171395'
Test #12:
score: 0
Accepted
time: 47ms
memory: 10888kb
input:
7058 172240 99 6 45 20 8 44 12 7 13 18 46 15 14 10 50 37 37 1 12 7 7 3 12 7 13 48 30 6 5 11 68 46 2 20 10 58 3 17 36 7 86 8 57 17 17 4 12 4 59 4 48 37 42 8 7 11 36 5 14 12 15 6 9 74 4 8 19 11 5 19 8 15 20 2 2 4 17 62 16 17 6 14 15 11 50 1 15 14 37 17 4 45 12 8 60 4 46 18 6 18 11 3 19 7 17 35 7 2 52 ...
output:
172240
result:
ok single line: '172240'
Test #13:
score: 0
Accepted
time: 2ms
memory: 9556kb
input:
1 1 1
output:
1
result:
ok single line: '1'
Test #14:
score: 0
Accepted
time: 47ms
memory: 10932kb
input:
7052 171344 3 17 36 19 11 15 34 3 91 17 16 18 93 15 7 1 2 5 7 61 4 6 8 10 3 7 3 2 35 10 82 39 88 4 6 15 40 8 96 17 15 20 98 43 18 11 39 13 58 13 16 49 3 49 15 9 18 10 12 5 10 12 6 3 77 4 71 45 16 3 1 77 2 38 31 13 44 4 13 20 19 10 92 20 17 87 7 64 4 39 81 63 8 52 5 3 6 5 14 2 52 20 4 16 20 13 4 2 13...
output:
171344
result:
ok single line: '171344'
Test #15:
score: 0
Accepted
time: 42ms
memory: 9820kb
input:
7065 171538 8 9 13 92 8 15 4 16 30 12 10 69 68 15 57 16 4 83 5 81 15 67 6 2 40 18 8 9 4 16 19 17 14 16 53 20 7 20 48 91 13 40 14 19 10 13 10 33 88 32 9 100 14 33 64 83 12 78 9 7 9 20 93 8 11 11 11 1 16 8 1 16 9 5 5 17 12 1 5 10 49 14 16 17 8 19 70 35 18 8 17 10 5 5 86 11 19 2 6 48 5 13 12 6 20 20 68...
output:
171538
result:
ok single line: '171538'
Test #16:
score: 0
Accepted
time: 46ms
memory: 10368kb
input:
7063 170730 19 18 3 72 10 19 3 50 5 3 100 2 71 11 6 9 13 52 32 2 69 14 6 2 13 33 13 5 19 4 9 17 1 37 42 40 50 30 19 87 16 12 81 7 20 19 19 17 17 31 12 11 45 46 18 8 2 8 9 34 5 8 74 18 84 46 58 8 15 11 76 17 84 17 7 38 72 11 17 20 20 4 14 5 20 4 5 14 11 13 47 6 1 10 20 85 8 11 7 17 20 13 31 4 10 18 1...
output:
170730
result:
ok single line: '170730'
Test #17:
score: 0
Accepted
time: 45ms
memory: 10076kb
input:
7061 171010 7 5 19 69 15 74 8 18 67 10 18 20 11 16 16 65 13 40 60 38 15 13 12 95 85 42 19 17 5 16 76 1 61 4 9 85 19 1 17 8 15 11 1 13 14 93 6 15 8 16 15 18 7 12 10 15 10 6 1 58 7 6 13 3 20 66 13 8 17 13 11 6 10 10 19 4 7 33 13 11 18 13 18 12 6 57 7 18 9 20 65 7 19 13 14 9 7 18 6 16 1 43 50 7 10 8 12...
output:
171010
result:
ok single line: '171010'
Test #18:
score: 0
Accepted
time: 46ms
memory: 11856kb
input:
7062 171278 97 20 20 68 32 61 10 1 14 1 7 69 42 17 8 7 1 74 18 14 5 2 9 14 47 5 5 13 80 38 2 6 7 31 10 11 33 1 13 11 14 16 9 45 67 6 8 1 11 4 19 78 82 58 65 12 17 98 15 47 19 49 19 11 76 12 11 30 2 60 16 99 12 18 77 2 64 17 71 20 19 3 7 20 3 19 91 18 12 13 31 16 18 30 48 1 75 2 14 16 14 15 6 19 19 1...
output:
171278
result:
ok single line: '171278'
Test #19:
score: 0
Accepted
time: 19ms
memory: 11128kb
input:
32142 191489 6 5 4 7 4 7 11 3 4 4 14 3 13 7 4 3 6 7 4 3 7 6 6 7 5 10 7 5 6 6 7 8 4 4 4 6 12 7 7 5 7 7 5 4 10 5 5 3 7 4 4 15 3 12 3 4 6 6 7 6 7 4 3 7 4 4 3 6 4 10 7 5 5 14 7 7 3 3 5 6 8 3 6 3 5 6 4 7 15 7 7 4 3 4 5 6 7 7 7 11 3 14 3 10 7 4 14 5 7 4 5 5 5 6 6 5 7 7 7 5 11 5 4 13 5 6 6 3 13 3 4 6 5 7 3...
output:
191489
result:
ok single line: '191489'
Test #20:
score: 0
Accepted
time: 15ms
memory: 11052kb
input:
32112 191429 3 6 6 6 6 11 6 3 7 6 6 3 8 4 4 4 3 7 4 7 7 12 7 5 4 7 4 7 5 6 7 3 6 7 12 7 7 4 6 12 5 4 7 5 7 3 5 3 3 5 4 6 7 6 7 5 7 4 5 5 6 4 3 4 5 5 7 6 5 5 13 11 7 4 5 3 4 15 5 5 4 5 5 7 3 3 6 6 11 5 7 12 6 11 7 5 7 4 4 3 4 6 5 7 5 3 5 12 5 6 6 5 3 5 4 6 4 6 3 7 4 7 7 6 6 7 5 7 5 5 10 7 4 4 6 3 4 3...
output:
191429
result:
ok single line: '191429'
Test #21:
score: 0
Accepted
time: 18ms
memory: 10940kb
input:
32107 192846 5 3 7 5 7 3 9 6 3 7 6 7 7 10 4 7 7 6 5 7 7 6 6 12 3 7 7 3 4 13 5 7 5 7 6 6 7 4 6 6 4 5 7 16 5 7 4 4 5 4 4 6 6 10 11 7 7 4 4 7 4 4 5 4 8 6 3 4 5 4 3 3 4 3 3 6 7 6 4 5 12 6 3 11 7 6 7 3 6 7 3 7 6 6 5 7 7 6 6 14 7 12 7 6 3 6 7 6 10 5 3 4 4 3 5 4 7 7 8 7 3 7 6 3 4 5 3 7 4 6 3 3 6 7 5 5 4 4 ...
output:
192846
result:
ok single line: '192846'
Test #22:
score: 0
Accepted
time: 18ms
memory: 10228kb
input:
32136 191995 14 7 3 6 4 4 3 4 3 4 4 14 7 3 3 6 4 5 3 7 6 4 5 4 7 7 4 4 7 3 6 4 8 8 4 7 15 13 4 10 4 7 6 4 7 3 3 6 11 10 6 7 7 5 6 10 4 7 4 5 12 7 7 3 4 6 3 6 3 3 7 4 7 5 3 11 5 7 4 13 7 5 6 6 4 5 5 7 3 4 6 5 4 7 5 3 6 15 4 3 15 4 6 7 5 10 5 7 10 4 5 5 4 5 7 4 4 6 7 3 3 5 5 12 13 3 5 5 7 10 7 5 4 6 7...
output:
191995
result:
ok single line: '191995'
Test #23:
score: 0
Accepted
time: 15ms
memory: 10236kb
input:
32162 192563 6 3 6 4 7 5 4 10 7 7 5 5 3 6 12 5 5 4 6 7 6 5 5 5 5 3 4 5 3 5 13 4 7 5 5 5 4 3 3 4 10 6 3 15 4 3 6 4 5 7 3 5 4 8 5 4 3 11 7 5 7 12 8 10 3 3 5 6 3 3 7 13 7 4 3 3 10 4 3 10 4 5 6 6 3 3 10 6 3 5 4 5 6 4 4 4 7 6 5 3 6 5 5 5 4 4 4 5 3 6 3 6 4 3 5 4 5 3 9 4 8 6 5 13 6 5 5 5 3 5 6 4 4 4 5 4 4 ...
output:
192563
result:
ok single line: '192563'
Test #24:
score: 0
Accepted
time: 1ms
memory: 7440kb
input:
1 1 200000
output:
0
result:
ok single line: '0'
Test #25:
score: 0
Accepted
time: 0ms
memory: 11592kb
input:
3 10 2 3 6
output:
9
result:
ok single line: '9'
Test #26:
score: 0
Accepted
time: 2ms
memory: 9712kb
input:
10 500 1 2 3 4 5 6 400 491 412 492
output:
500
result:
ok single line: '500'
Test #27:
score: 0
Accepted
time: 1ms
memory: 10552kb
input:
2 200000 100000 100000
output:
200000
result:
ok single line: '200000'
Test #28:
score: 0
Accepted
time: 3ms
memory: 10208kb
input:
200000 200000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
200000
result:
ok single line: '200000'
Test #29:
score: 0
Accepted
time: 4ms
memory: 10336kb
input:
100000 100000 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
output:
100000
result:
ok single line: '100000'
Test #30:
score: 0
Accepted
time: 8ms
memory: 11332kb
input:
49900 199570 3 3 3 4 4 5 3 3 5 3 4 5 3 5 5 3 3 3 5 3 3 5 4 4 3 5 3 4 3 5 4 4 3 4 5 5 5 4 3 4 5 4 4 5 4 5 5 4 3 3 5 3 4 4 3 3 3 3 4 5 4 4 5 3 4 4 5 5 3 4 3 4 3 5 3 4 5 3 4 4 3 4 4 4 4 3 5 3 3 5 3 3 3 5 3 4 4 4 5 3 4 4 5 4 5 4 4 4 5 3 5 4 3 5 3 3 5 5 3 3 4 3 4 5 5 4 4 3 4 4 5 3 3 3 3 5 5 4 5 4 3 4 4 3...
output:
199568
result:
ok single line: '199568'