Market

题目大意

每个物品有三个权值:代价、价值、从哪个时间后可以购买
现在多次询问,每次询问给出你的钱数、购买时间,每个物品至多买一次,问最大价值。
代价<=10^9,价值<=300

DP

先离线做,枚举时间。每个询问挂在时间上。
现在我们不能用传统01背包,因为代价超大。
可以考虑设f[j]表示价值为j时最小的代价。
为了方便得到答案,f[j]表示为价值>=j时最小的代价,后缀min一发就好了。
然后每次给定钱数,在f上二分即可。

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define fd(i,a,b) for(i=a;i>=b;i--)
using namespace std;
const int maxn=300+10,maxm=100000+10,maxv=90000,inf=1000000050;
struct dong{
    int c,v,t;
} a[maxn];
int f[maxv+10],h[maxn],go[maxm],dis[maxm],next[maxm],b[maxn],ans[maxm],sta[100];
int i,j,k,l,r,mid,t,n,m,tot,top;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9'){
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
bool cmp(dong a,dong b){
    return a.t<b.t;
}
void add(int x,int y,int z){
    go[++tot]=y;
    dis[tot]=z;
    next[tot]=h[x];
    h[x]=tot;
}
void insert(int c,int v){
    int i;
    fd(i,maxv,v)
        f[i]=min(f[i],f[i-v]+c);
    fd(i,maxv-1,0) f[i]=min(f[i],f[i+1]);
}
int query(int x){
    int l=0,r=maxv,mid;
    while (l<r){
        mid=(l+r+1)/2;
        if (f[mid]<=x) l=mid;else r=mid-1;
    }
    return l;
}
void write(int x){
    if (!x){
        putchar('0');
        putchar('\n');
        return;
    }
    top=0;
    while (x){
        sta[++top]=x%10;
        x/=10;
    }
    while (top){
        putchar('0'+sta[top]);
        top--;
    }
    putchar('\n');
}
int main(){
    freopen("market.in","r",stdin);freopen("market.out","w",stdout);
    n=read();m=read();
    fo(i,1,n) a[i].c=read(),a[i].v=read(),a[i].t=read();
    sort(a+1,a+n+1,cmp);
    fo(i,1,n) b[i]=a[i].t;
    fo(i,1,m){
        t=read();k=read();
        if (t<b[1]) continue;
        l=1;r=n;
        while (l<r){
            mid=(l+r+1)/2;
            if (b[mid]<=t) l=mid;else r=mid-1;
        }
        add(l,i,k);
    }
    fo(i,0,90000) f[i]=inf;
    f[0]=0;
    fo(i,1,n){
        insert(a[i].c,a[i].v);
        t=h[i];
        while (t){
            ans[go[t]]=query(dis[t]);
            t=next[t];
        }
    }
    fo(i,1,m) write(ans[i]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值