Codeforces Round #579 (Div. 3) F1. Complete the Projects (easy version) 排序 + 贪心
作者:互联网
文章目录
题意:
思路:
比较直观的想法就是对于 b i ≥ 0 b_i\ge0 bi≥0的项目,我们将 a i a_i ai从小到大排序,让后依次加 b i b_i bi,如果有取不到的,显然就无解。否则再看 b i < 0 b_i<0 bi<0的部分,这部分不是很好处理,我们尝试推一下狮子。
假设有两个相邻的项目 i , j i,j i,j,他们的 b < 0 b<0 b<0,当前分为 s u m sum sum,使用临项交换法,先考虑两种情况:
( 1 ) (1) (1)当 i i i在 j j j前面的时候,需要满足 s u m ≥ a i , s u m + b i ≥ a j sum\ge a_i,sum+b_i\ge a_j sum≥ai,sum+bi≥aj,即 s u m ≥ m a x ( a i , a j − b i ) sum\ge max(a_i,a_j-b_i) sum≥max(ai,aj−bi)。
( 2 ) (2) (2)当 j j j在 i i i前面的时候,需要满足 s u m ≥ a j , s u m + b j ≥ a i sum\ge a_j,sum+b_j\ge a_i sum≥aj,sum+bj≥ai,即 s u m ≥ m a x ( a j , a i − b j ) sum\ge max(a_j,a_i-b_j) sum≥max(aj,ai−bj)。
假设 i i i在 j j j前面更优的话,那么也就是 m a x ( a i , a j − b i ) ≤ m a x ( a j , a i − b j ) max(a_i,a_j-b_i)\le max(a_j,a_i-b_j) max(ai,aj−bi)≤max(aj,ai−bj),注意到 a i ≤ a i − b j , a j ≤ a j − b i a_i\le a_i-b_j,a_j\le a_j-b_i ai≤ai−bj,aj≤aj−bi,那么上面的式子也就变成 a j − b i ≤ a i − b j a_j-b_i\le a_i-b_j aj−bi≤ai−bj,移项得 a j + b j ≤ a i + b i a_j+b_j\le a_i+b_i aj+bj≤ai+bi,所以对于 b < 0 b<0 b<0的部分,我们按照 a i + b i a_i+b_i ai+bi从大到小排序即可。
// Problem: F1. Complete the Projects (easy version)
// Contest: Codeforces - Codeforces Round #579 (Div. 3)
// URL: https://codeforces.com/problemset/problem/1203/F1
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#include<random>
#include<cassert>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid ((tr[u].l+tr[u].r)>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
LL n,m;
struct Node {
int a,b;
};
vector<Node>v1,v2;
bool cmp1(Node a,Node b) {
return a.a<b.a;
}
bool cmp2(Node a,Node b) {
return a.a+a.b>b.a+b.b;
}
bool check() {
int cnt=0;
for(auto x:v1) {
if(m>=x.a) m+=x.b,cnt++;
else break;
}
if(cnt!=v1.size()) return false;
cnt=0;
for(auto x:v2) {
if(m>=x.a) m+=x.b,cnt++;
else break;
}
if(m<0) return false;
if(cnt!=v2.size()) return false;
return true;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
scanf("%lld%lld",&n,&m);
for(int i=1;i<=n;i++) {
int a,b; scanf("%d%d",&a,&b);
if(b>=0) v1.pb({a,b});
else v2.pb({a,b});
}
sort(v1.begin(),v1.end(),cmp1);
sort(v2.begin(),v2.end(),cmp2);
if(check()) puts("YES");
else puts("NO");
return 0;
}
/*
*/
标签:F1,579,Complete,ai,max,sum,aj,bi,include 来源: https://blog.csdn.net/m0_51068403/article/details/120120681