题目
[codeforces1203F1]Complete the Projects (easy version)
题意
有一些任务要做,做任务有前提,就是你的rating要大于等于这个任务要求的rating,而做了这个任务后,你的rating会变化bi,bi可正可负。问能否完成给定的所有任务呢?
思路
分两种情况讨论,一种是bi大于等于0的,对于这种我们优先完成ai小的,也就是优先完成rating要求小的任务,维护r值,每次做任务都判断 r 值是否大于rating的要求。
然后再来看bi小于 0 的,对于这些任务,我们按它的ai + bi的值从大到小的顺序来完成任务,每次做任务都看r是否大于rating。
最后再判断一下r是否大于等于0即可。
代码写的比较冗余,可以优化。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2 + 5;
const ll mod = 1e9 + 7;
struct node{
int a, b;
}p[maxn], new_p[maxn];
bool cmp1(node a, node b){
return a.b > b.b;
}
bool cmp2(node a, node b){
return a.a < b.a;
}
bool cmp3(node a, node b){
return a.a + a.b > b.a + b.b;
}
int main()
{
int n, r;
scanf("%d %d", &n, &r);
for(int i = 1; i <= n; i++)
{
scanf("%d %d", &p[i].a, &p[i].b);
}
sort(p + 1, p + 1 + n, cmp1);
int pos = 1;
while(pos <= n && p[pos].b >= 0){
pos++;
}
pos--;
sort(p + 1, p + 1 + pos, cmp2);
for(int i = 1; i <= pos; i++)
{
if(p[i].a > r)
return 0 * printf("NO\n");
r += p[i].b;
}
sort(p + 1 + pos, p + 1 + n, cmp3);
int cnt = 0;
for(int i = pos + 1; i <= n; i++)
{
cnt++;
new_p[cnt] = p[i];
}
for(int i = 1; i <= n; i++)
{
if(r >= new_p[i].a)
r += new_p[i].b;
else
return 0 * printf("NO\n");
}
if(r >= 0)
printf("YES\n");
else
printf("NO\n");
return 0;
}
/*
5 5
5 2
6 3
8 -3
7 -2
5 -1
5 5
5 2
6 3
8 -3
7 -2
6 -1
5 5
5 2
6 3
8 -3
7 -2
5 -5
5 5
5 2
6 3
8 -3
7 -2
5 -6
2 5
4 -100
2 -50
*/