【题解】lugu P4095 Eden的新背包问题

总结:
1.

50分的代码(因为由多个数据,所以不能改变num[]数组)

#include<bits/stdc++.h>
using namespace std;
struct node{
    int pos, value;
}que[1005];
int c[1005], w[1005], num[1005], f[1005], vis[1005];
int n, head, tail, V, q, h, z[1005];
int main()
{
    cin >> n;
    for(int i = 0; i < n; i++)
        cin >> c[i] >> w[i] >> num[i];
    cin >> q;
    for(int cnt = 1; cnt <= q; cnt++)
{    
    memset(f, 0, sizeof(f));
    cin >> h >> V;
    vis[h] = 1;
    for(int i=0;i<n;i++) 
{
    if(!vis[i])
    {
    z[i] = min(num[i], V/c[i]);  //用z[]数组代表能用多少件物品
    for(int mo=0;mo<c[i];mo++) 
    {
        head=tail=0;
        for(int k=0;k<=(V-mo)/c[i];k++) 
        {
            int x=k;
            int y=f[k*c[i]+mo]-k*w[i];
            while(head<tail && que[head].pos<k-z[i])head++;
            while(head<tail && que[tail-1].value<=y)tail--;
            que[tail].value=y,que[tail].pos=x;
            tail++;
            f[k*c[i]+mo]=que[head].value+k*w[i];
        }
    }
    }    
}
cout << f[V] << endl;
vis[h] = 0;
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/lovezxy520/p/11374675.html