传送门
题目描述
给定任务个数 n n n和初始值 r r r,完成每个任务需要有 a i a_{i} ai的 r r r值,完成后 r r r值会改变 b i b_{i} bi,问最多能完成多少任务(要保证最后 r > = 0 r>=0 r>=0)
分析
分情况讨论一下,把
a
i
≥
0
a_{i} \ge 0
ai≥0 he
a
i
<
0
a_{i} < 0
ai<0的情况分开讨论
如果
a
i
≥
0
a_{i} \ge 0
ai≥0,把
a
i
a_{i}
ai从小到大排序,然后去判断能完成多少任务
如果
a
i
<
0
a_{i} < 0
ai<0,按照
a
i
+
b
i
a_{i} + b_{i}
ai+bi 从大到小进行排序,如果如果能完成任务,丢进队列,如果不能完成,就在队列中删去一个花费最多的任务即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){return (b>0)?gcd(b,a%b):a;}
int n,s;
struct Node{
int a,b;
bool operator < (const Node &t) const{
return a < t.a;
}
};
vector<Node> r,l;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
read(n),read(s);
for(int i = 1;i <= n;i++) {
int x,y;
read(x),read(y);
if(y < 0) l.pb({x,y});
else r.pb({x,y});
}
sort(all(r));
int res = 0;
for(auto t:r)
if(s >= t.a)
s += t.b,res++;
for(auto &t:l){
t.a += t.b;
t.a = max(t.a,0);
}
sort(all(l));
reverse(all(l));
for(auto t:l){
s += t.b;
q.push(t.b);
if(s < t.a){
s -= q.top();
q.pop();
}
}
res += q.size();
di(res);
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/