Time Limits: 2000 ms Memory Limits: 524288 KB
Description
Input
Output
Sample Input
Sample Output
Data Constraint
Solution
很自然的想法,首先一定会按照di排序。
设fi表示前i个物品,第i个物品一定购买的时候最大的魂力。
转移的时候,就枚举上一个购买的物品j,
计算这一段时间j物品的收益。
fi=fj-pj+rj+(di−dj−1)*gj
这样子是O(n*n)的。
整理一下式子,就变成了:
fi=fj-pj+rj-(dj+1)gj+di*gj
可以看出,对于每一个不同的i,相同的j的影响是一个一次函数,
也就是一条直线,斜率为gj
因为gi并没有单调性,所以就不能用单调队列或者栈来维护。
用线段树来维护。
线段树的一个区间,表示一条线段,
如果新插入的线段完全大于原有的线段,
就整个区间直接用新的线段去替换。
如果新旧线段有交点,
那么就分别递归到下面一层。
查询的时候,就将包含这个点的每个区间的线段取个max就可以了。
Code
30%
#include<algorithm>
#include<cstring>
#include<cstdio>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define LL long long
#define N 100010
using namespace std;
LL n,o,i,j,c,d,q,num,f[N];
struct node
{
LL d,p,r,g;
}a[N];
bool cmp(node x,node y)
{
return x.d<y.d;
}
int main()
{
freopen("practice.in","r",stdin);
freopen("practice.out","w",stdout);
scanf("%lld%lld",&num,&q);
fo(o,1,q)
{
scanf("%lld%lld%lld",&n,&c,&d);
fo(i,1,n) scanf("%lld%lld%lld%lld",&a[i].d,&a[i].p,&a[i].r,&a[i].g);
sort(a+1,a+1+n,cmp);
memset(f,0,sizeof(f));
f[0]=c,a[n+1].d=d+1;
fo(i,1,n+1)
fo(j,0,i-1) if(f[j]>=a[j].p)
f[i]=max(f[i],f[j]-a[j].p+a[j].r+a[j].g*(a[i].d-a[j].d-1));
printf("%lld\n",f[n+1]);
}
}
100%
#include<algorithm>
#include<cstring>
#include<cstdio>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define ll long long
#define N 100010
using namespace std;
ll n,i,j,c,d,q,tp,tot,f[N];
struct node{ll d,p,r,g;}a[N];
struct data{ll l,r,k,b;}t[4*N],w;
bool cmp(node x,node y){return x.d<y.d;}
ll calc(data e,ll x){return e.k*x+e.b;}
ll get(ll x,ll l,ll r,ll v)
{
if(x==0) return 0;;
if(l==r) return calc(t[x],v);
ll m=(l+r)>>1;
if(v<=m) return max(calc(t[x],v),get(t[x].l,l,m,v));
else return max(calc(t[x],v),get(t[x].r,m+1,r,v));
}
void ins(ll x,ll l,ll r)
{
ll t1=calc(w,l),t2=calc(w,r),w1=calc(t[x],l),w2=calc(t[x],r);
if(t1>=w1&&t2>=w2) {t[x].k=w.k,t[x].b=w.b; return;}
if(t1<w1&&t2<w2||l==r) return;
ll m=(l+r)>>1;
if(!t[x].l) t[x].l=++tot;
if(!t[x].r) t[x].r=++tot;
ins(t[x].l,l,m);
ins(t[x].r,m+1,r);
}
int main()
{
freopen("practice.in","r",stdin);
freopen("practice.out","w",stdout);
scanf("%lld%lld",&q,&q);
while(q--)
{
scanf("%lld%lld%lld",&n,&c,&d),d++;
fo(i,1,n) scanf("%lld%lld%lld%lld",&a[i].d,&a[i].p,&a[i].r,&a[i].g);
sort(a+1,a+1+n,cmp);
t[tot=1].b=c;
fo(i,1,n)
{
tp=get(1,1,d,a[i].d);
if(tp<a[i].p) continue;
w.k=a[i].g,w.b=tp-a[i].p+a[i].r-a[i].g*(a[i].d+1);
ins(1,1,d);
}
printf("%lld\n",get(1,1,d,d));
memset(t,0,sizeof(t));
}
}