晕了
同一套代码,只是全局变量的命名顺序有区别,怎么会有这么多的时间差距,甚至会报错啊?
求大佬解答下QAQ
多重背包问题 II https://www.acwing.com/problem/content/description/5/
300ms
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 11000; //s<=2000 使用1,2,4,...2^k的方式分割s,最多分成11组
int n, m;
int v[N], w[N], cnt;
int f[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
int a, b, s;
cin >> a >> b >> s; //vms
int k = 1;
while(k <= s)
{
cnt ++ ;
v[cnt] = k * a;
w[cnt] = k * b;
s -= k;
k *= 2;
}
if(s > 0)
{
cnt ++ ;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
for(int i = 1; i <= cnt; i ++ )
for(int j = m; j >= v[i]; j -- )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
return 0;
}
400ms
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 11000; //s<=2000 使用1,2,4,...2^k的方式分割s,最多分成11组
int n, m, cnt;
int f[N];
int v[N], w[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
int a, b, s;
cin >> a >> b >> s; //vms
int k = 1;
while(k <= s)
{
cnt ++ ;
v[cnt] = k * a;
w[cnt] = k * b;
s -= k;
k *= 2;
}
if(s > 0)
{
cnt ++ ;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
for(int i = 1; i <= cnt; i ++ )
for(int j = m; j >= v[i]; j -- )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
return 0;
}
WA
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 11000; //s<=2000 使用1,2,4,...2^k的方式分割s,最多分成11组
int n, m;
int v[N], w[N], f[N], cnt;
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
int a, b, s;
cin >> a >> b >> s; //vms
int k = 1;
while(k <= s)
{
cnt ++ ;
v[cnt] = k * a;
w[cnt] = k * b;
s -= k;
k *= 2;
}
if(s > 0)
{
cnt ++ ;
v[cnt] = s * a;
w[cnt] = s * b;
}
}
for(int i = 1; i <= cnt; i ++ )
for(int j = m; j >= v[i]; j -- )
f[j] = max(f[j], f[j - v[i]] + w[i]);
cout << f[m] << endl;
return 0;
}