http://acm.uestc.edu.cn/#/contest/show/54
L - Lovely princess
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
There are n jobs you need to complete. However, complete the ith job you capability must be no less than vi. If you have completed the ith job, your capability will increase ai.
Then the question is coming, what is the minimum initial capability value if you are required to complete all of the n jobs.
Note that there is no restriction on the order you complete them. That is to say, you can decide the order by your own.
Input
The first line contains a single integer n, which is the number of jobs you need to complete.
Then each of the following n lines contains 2 integers vi and ai, which are described above.
1≤n≤1000,0≤vi≤1000000,0≤ai≤1000
Output
Print the answer in one line.
Sample input and output
Sample Input | Sample Output |
---|---|
1 2 1 |
2 |
思路:运用贪心策略,对v排序。(之前一直tle,因为读错了题,以及v是消耗值。。。囧)
官方题解:
代码:
1 #include <fstream> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstdio> 5 #include <cstring> 6 #include <cmath> 7 #include <cstdlib> 8 #include <vector> 9 10 using namespace std; 11 12 #define PI acos(-1.0) 13 #define EPS 1e-10 14 #define lll __int64 15 #define ll long long 16 #define INF 0x7fffffff 17 18 const int N=1005; 19 20 struct node{ 21 int v,a; 22 }yu[N]; 23 int n,ans,cans; 24 25 bool cmp(const node &r1,const node &r2){ 26 return r1.v<r2.v; 27 } 28 29 int main(){ 30 //freopen("D:\input.in","r",stdin); 31 //freopen("D:\output.out","w",stdout); 32 int t1,t2; 33 scanf("%d",&n); 34 for(int i=0;i<n;i++){ 35 scanf("%d %d",&yu[i].v,&yu[i].a); 36 } 37 sort(yu,yu+n,cmp); 38 for(int i=0;i<n;i++){ 39 if(cans<yu[i].v){ 40 ans+=yu[i].v-cans; 41 cans=yu[i].v; 42 } 43 cans+=yu[i].a; 44 } 45 printf("%d ",ans); 46 return 0; 47 }