这道题蒟蒻调了好久好久(大概一天),仍然没能发现错误原因。有没有哪位大佬能为蒟蒻解答疑惑?
WA on test 2代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#define double long double
using namespace std;
int n,m,p;
const int N=1e5+10;
const int P=110;
int d[N];
int time[N];
int t[N];
int h[N];
int len[N];
long long f[N][P];
long long s[N];
inline long long X(int a,int j){
return f[a][j]+s[a];
}
inline long long Y(int a){
return a;
}
inline double slope(int a,int b,int j){
return (1.*(X(a,j)-X(b,j)))/1.*(Y(a)-Y(b));
}
int q[N];
int head,tail;
signed main(){
cin>>n>>m>>p;
if(p>=m){
cout<<0<<endl;
return 0;
}
for(int i=2;i<=n;i++)
cin>>d[i];
for(int i=1;i<=n;i++)
len[i]=len[i-1]+d[i];
for(int i=1;i<=m;i++)
cin>>h[i]>>time[i];
for(int i=1;i<=m;i++)
t[i]=time[i]-len[h[i]];
sort(t+1,t+m+1);
for(int i=1;i<=m;i++)
s[i]=s[i-1]+t[i];
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
for(int i=1;i<=p;i++){
tail=head=1;
q[head]=0;
for(int j=1;j<=m;j++){
while(head<tail&&slope(q[head],q[head+1],i-1)<t[j])head++;
//f[i][j]=min{f[k][j-1]+(i-k)*t[i])-(s[i]-s[k])}
f[j][i]=f[q[head]][i-1]+(j-q[head])*(t[j])-s[j]+s[q[head]];
while(head<tail&&slope(q[tail-1],q[tail],i-1)>slope(q[tail],j,i-1))tail--;
tail++;
q[tail]=j;
}
}
for(int i=1;i<=p;i++){
for(int j=1;j<=m;j++)
cout<<f[j][i]<<" ";
cout<<endl;
}
cout<<f[m][p]<<endl;
system("pause");
return 0;
}
附带一组Hack数据
4 4 2
3 6 9
1 6
2 5
1 6
1 10
标答:4