P2920 [USACO08NOV]时间管理Time Management
显然的贪心。
按deadline从大到小排序,然后依次填充时间。
最后时间为负的话那么就是无解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define re register 6 using namespace std; 7 int max(int a,int b){return a>b?a:b;} 8 struct data{ 9 int t,dl; 10 bool operator < (const data &tmp) const{return dl>tmp.dl;} 11 }a[1002]; 12 int n,tt,ans; 13 int main(){ 14 scanf("%d",&n); 15 for(re int i=1;i<=n;++i) scanf("%d%d",&a[i].t,&a[i].dl); 16 sort(a+1,a+n+1); tt=a[1].dl; 17 for(re int i=1;i<=n;++i){ 18 tt=min(tt,a[i].dl); tt-=a[i].t;//注意在deadline前完成所以要取min 19 }tt<0 ? printf("-1"):printf("%d",tt); 20 return 0; 21 }