cdoj第13th校赛初赛L - Lovely princess

http://acm.uestc.edu.cn/#/contest/show/54

 

L - Lovely princess

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

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 InputSample 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\n",ans);
46     return 0;
47 }
View Code

 

转载于:https://www.cnblogs.com/jiu0821/p/4378856.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值