我一开始的想法就是递推,维护一个cover值,表示能用第二阶段等待的时间来帮下一组数据cover掉多少时间。
但是我竟然没有考虑到可以不用等待下一组的第二段时间(即 t 时间)完成而马上进行第三组的 d 时间。这里wa了。
改后艰难AC....残了....
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100005
struct node{ int d, t; }a[MAXN];
int n;
int solve()
{
int ret=0;
int cover=0;
for(int i=0; i<n; i++)
{
//debug
//printf("go (%d, %d), cover=%d\n", a[i].d, a[i].t, cover);
int all = a[i].d+a[i].t;
int x = a[i].d+a[i].t - cover;
if(x<0) //ȫcover
{
cover = max(cover-a[i].d, a[i].t);
x=0;
}
else if(cover>a[i].d)//
{
cover = max(cover-a[i].d, a[i].t);
}
else
{
cover = a[i].t;
}
ret+=x;
}
return ret;
}
int cmp(node x, node y)
{
if(x.t>y.t) return 1;
else if(x.t==y.t && x.d<y.d) return 1;
else return 0;
}
int main()
{
int t; scanf("%d", &t);
for(int T=0; T<t; T++)
{
printf("Case %d: ", T+1);
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d%d", &a[i].d, &a[i].t);
}
sort(a, a+n, cmp);
printf("%d\n", solve());
}
}