Question:
There are n tasks to be processed on the machine, which constitute a sequence to divide these tasks into several batches
Starting from time 0, these tasks are processed in batches. The time required for completing the I-th task is Ti.
Before each batch of tasks starts, the machine must start at S, and the time required to complete these tasks is the total time required for each task.
Note that the same batch of tasks will be completed at the same timeThe cost of each task is its completion time multiplied by a fee coefficient fi
Determine a group scheme to minimize the total cost
Ideas:
Set the DP array to indicate the minimum cost of the first I.
DP I = DPJ + (sumtime I-sumtime J + S) * (sumcost N-sumcost J)
Slope Optimization after disassembling
Note that the time may be negative, so the slope does not need to be monotonous.
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<algorithm> 7 #include<vector> 8 #include<queue> 9 #include<map>10 #define inf 213906214311 #define ll long long12 #define MAXN 100100013 using namespace std;14 inline int read()15 {16 int x=0,f=1;char ch=getchar();17 while(!isdigit(ch)) {if(ch==‘-‘) f=-1;ch=getchar();}18 while(isdigit(ch)) {x=x*10+ch-‘0‘;ch=getchar();}19 return x*f;20 }21 ll st[MAXN],sc[MAXN],f[MAXN];22 int n,s,tim,hd,tl,q[MAXN];23 ll Y(int i) {return f[i]-sc[n]*st[i]+sc[i]*st[i]-sc[i]*tim;}24 int main()25 {26 n=read(),tim=read();27 for(int i=1;i<=n;i++) st[i]=st[i-1]+read(),sc[i]=sc[i-1]+read();28 for(int i=1;i<=n;i++)29 {30 int l=0,r=tl-1,res=tl,j;31 while(l<=r)32 {33 int mid=(l+r)>>1;34 if(1ll*(sc[q[mid+1]]-sc[q[mid]])*st[i]<=Y(q[mid+1])-Y(q[mid])) res=mid,r=mid-1;35 else l=mid+1;36 }37 j=q[res],f[i]=f[j]+(sc[n]-sc[j])*(st[i]-st[j]+tim);38 while(hd<tl&&1ll*(Y(q[tl])-Y(q[tl-1]))*(sc[i]-sc[q[tl]])>=(Y(i)-Y(q[tl]))*(sc[q[tl]]-sc[q[tl-1]])) tl--;39 q[++tl]=i;40 }41 printf("%lld\n",f[n]);42 }View code
Bzoj 2726 Task Arrangement