题目描述
Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all nn dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals ss .
If Kirito starts duelling with the ii -th ( 1<=i<=n1<=i<=n ) dragon and Kirito's strength is not greater than the dragon's strength x_{i}xi , then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by y_{i}yi .
Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss.
输入格式
The first line contains two space-separated integers ss and nn ( 1<=s<=10^{4}1<=s<=104 , 1<=n<=10^{3}1<=n<=103). Then nn lines follow: the ii -th line contains space-separated integers x_{i}xi and y_{i}yi ( 1<=x_{i}<=10^{4}1<=xi<=104, 0<=y_{i}<=10^{4}0<=yi<=104 ) — the ii -th dragon's strength and the bonus for defeating it.
输出格式
On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't.
输入输出样例
输入 #1
2 2 1 99 100 0
输出 #1
YES
输入 #2
10 1 100 100
输出 #2
NO
说明/提示
In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2+99=1012+99=101 . Now he can defeat the second dragon and move on to the next level.
In the second sample Kirito's strength is too small to defeat the only dragon and win.
解题思路:巨水的贪心,由于每条龙的顺序没有固定。所以可以先按从小到大的顺序把龙的攻击排个序。然后挨个累加附加值就行。如果出现攻击力不匹配的情况直接跳出就行。
代码实现:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
struct pows{
int x;
int y;
};
bool cmp(pows a,pows b){
return a.x<b.x;
}
int main(){
ios::sync_with_stdio(false);
ll s,n;
ll x,y;
struct pows p[maxn];
cin>>s>>n;
for(int i=0;i<n;i++){
cin>>p[i].x>>p[i].y;
}
sort(p,p+n,cmp);
for(int i=0;i<n;i++){
if(s<=p[i].x){
cout<<"NO"<<endl;
return 0;
}
s+=p[i].y;
}
cout<<"YES"<<endl;
return 0;
}