CodeForces - 1203F2 Complete the Projects (hard version)

Description

传送门

Solution

这题分成两个部分来做。

  • b [ i ] ≥ 0 b[i]\geq0 b[i]0。贪心。将 a [ i ] a[i] a[i] 从小到大排序,然后依次做工作,如果不行就算了。

  • b [ i ] < 0 b[i] < 0 b[i]<0。发现不能直接贪 (废话直接贪就不是 DP 了)

    假设 i i i 选完后能选 j j j,那么就有 a [ i ] + b [ i ] ≥ a [ j ] a[i]+b[i]\ge a[j] a[i]+b[i]a[j]。你可能会疑惑为什么这里是 a [ i ] a[i] a[i] 而不是 r r r 这种东西,其实如果 a [ i ] a[i] a[i] 成立,那么 r r r 一定成立,因为选择了 i i i,就有 r ≥ a [ i ] r\ge a[i] ra[i]

    这时我们反着来看,假设 j j j 选完能选 i i i,同理有 a [ j ] + b [ j ] ≥ a [ i ] a[j]+b[j]\ge a[i] a[j]+b[j]a[i]

    把两个柿子放在一起看:

    a [ i ] + b [ i ] ≥ a [ j ] ≥ a [ i ] − b [ j ] a[i]+b[i]\ge a[j]\ge a[i]-b[j] a[i]+b[i]a[j]a[i]b[j]

    你会发现这个柿子除了 a [ i ] = a [ j ] , b [ i ] = b [ j ] = 0 a[i]=a[j],b[i]=b[j]=0 a[i]=a[j],b[i]=b[j]=0 的情况是无法成立的(然而这种情况 b [ i ] ≥ 0 b[i]\ge 0 b[i]0,我们在上文就已经考虑过了)。

    我们把 a [ i ] + b [ i ] a[i]+b[i] a[i]+b[i] 从大到小排序,那么如果 i < j i<j i<j,就有 a [ i ] + b [ i ] ≥ a [ j ] + b [ j ] a[i]+b[i]\ge a[j]+b[j] a[i]+b[i]a[j]+b[j]。虽然不能保证 i i i 选了后面一定可以选,但是先选 j j j 一定不行,因为 a [ j ] + b [ j ] ≤ a [ i ] + b [ i ] ≤ a [ i ] a[j]+b[j]\le a[i]+b[i]\le a[i] a[j]+b[j]a[i]+b[i]a[i]

    然后 01 01 01 背包即可。

Code

#include <cstdio>

#define rep(i, _l, _r) for(register signed i = (_l), _end = (_r); i <= _end; ++ i)
#define fep(i, _l, _r) for(register signed i = (_l), _end = (_r); i >= _end; -- i)
#define erep(i, u) for(signed i = head[u], v = to[i]; i; i = nxt[i], v = to[i])
#define print(x, y) write(x), putchar(y)

template <class T> inline T read(const T sample) {
	T x = 0; int f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}
template <class T> inline void write(const T x) {
	if(x < 0) return (void) (putchar('-'), write(-x));
	if(x > 9) write(x / 10);
	putchar(x % 10 ^ 48);
}
template <class T> inline T Max(const T x, const T y) {return x > y ? x : y;}
template <class T> inline T Min(const T x, const T y) {return x < y ? x : y;}
template <class T> inline T fab(const T x) {return x > 0 ? x : -x;}
template <class T> inline T Gcd(const T x, const T y) {return y ? Gcd(y, x % y) : x;}

#include <cstring>
#include <algorithm>
using namespace std;

int n, r, ans, f[105], pc, tot;
struct node {int a, b;} p[105], q[105];

bool cmp(const node x, const node y) {return x.a < y.a;}

bool Cmp(const node x, const node y) {return x.a + x.b > y.a + y.b;}

int main() {
	int x, y;
	n = read(9), r = read(9);
	rep(i, 1, n) {
		x = read(9), y = read(9);
		if(y >= 0) p[++ pc] = (node) {x, y};
		else q[++ tot] = (node) {x, y};
	}
	sort(p + 1, p + pc + 1, cmp);
	rep(i, 1, pc) {
		if(r < p[i].a) break;
		r += p[i].b; ++ ans;
	}
	sort(q + 1, q + tot + 1, Cmp);
	memset(f, -1, sizeof f);
	f[0] = r;
	rep(i, 1, tot)
		fep(j, tot, 1)
			if(f[j - 1] >= q[i].a)
				f[j] = Max(f[j], f[j - 1] + q[i].b);
	fep(i, tot, 1) if(~ f[i]) {ans += i; break;}
	print(ans, '\n');
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值