01背包TLE
查看原帖
01背包TLE
245959
Ender_NaCl楼主2021/7/30 19:38

我知道枚举的确实太多了,但怎么优化呢?

#include <iostream>

#include <map>

using namespace std;

struct P
{
	long long w,v;
}a[110];

map<long long,long long>f;

int main()
{
	long long i,j,n,bag;
	cin>>n>>bag;
	for(i = 1;i <= n;i++) cin>>a[i].w>>a[i].v;
    for(i = 1;i <= n;i++)
    {
    	for(j = bag;j >= a[i].w;j--)
    	{
    		f[j] = max(f[j - a[i].w] + a[i].v,f[j]);
    	}
    }
    cout<<f[bag];
    return 0;
}

求大佬指点

2021/7/30 19:38
加载中...