Complete the Projects (easy version)
传送门
题面翻译
题目描述
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version.
Polycarp is a very famous freelancer. His current rating is r r r units.
Some very rich customers asked him to complete some projects for their companies. To complete the i i i -th project, Polycarp needs to have at least a i a_i ai units of rating; after he completes this project, his rating will change by b i b_i bi (his rating will increase or decrease by b i b_i bi ) ( b i b_i bi can be positive or negative). Polycarp’s rating should not fall below zero because then people won’t trust such a low rated freelancer.
Is it possible to complete all the projects? Formally, write a program to check if such an order of the projects exists, that Polycarp has enough rating before starting each project, and he has non-negative rating after completing each project.
In other words, you have to check that there exists such an order of projects in which Polycarp will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project.
输入格式
The first line of the input contains two integers n n n and r r r ( 1 ≤ n ≤ 100 , 1 ≤ r ≤ 30000 1 \le n \le 100, 1 \le r \le 30000 1≤n≤100,1≤r≤30000 ) — the number of projects and the initial rating of Polycarp, respectively.
The next n n n lines contain projects, one per line. The i i i -th project is represented as a pair of integers a i a_i ai and b i b_i bi ( 1 ≤ a i ≤ 30000 1 \le a_i \le 30000 1≤ai≤30000 , − 300 ≤ b i ≤ 300 -300 \le b_i \le 300 −300≤bi≤300 ) — the rating required to complete the i i i -th project and the rating change after the project completion.
输出格式
Print “YES” or “NO”.
样例 #1
样例输入 #1
3 4
4 6
10 -2
8 -1
样例输出 #1
YES
样例 #2
样例输入 #2
3 5
4 -5
4 -2
1 3
样例输出 #2
YES
样例 #3
样例输入 #3
4 4
5 2
5 -3
2 1
4 -2
样例输出 #3
YES
样例 #4
样例输入 #4
3 10
10 0
10 -10
30 0
样例输出 #4
NO
提示
In the first example, the possible order is: 1 , 2 , 3 1, 2, 3 1,2,3 .
In the second example, the possible order is: 2 , 3 , 1 2, 3, 1 2,3,1 .
In the third example, the possible order is: 3 , 1 , 4 , 2 3, 1, 4, 2 3,1,4,2 .
以上来自洛谷 以上来自洛谷 以上来自洛谷
前言
先写了hard版的题解,才想起来easy版的没写,没事没事,赶紧水一篇。
解题思路:
将 b i ≥ 0 b_i\ge0 bi≥0的任务单独分出,以 a i a_i ai大小升序排列,为赚取尽可能多的rating值。即:
if (y >= 0) {//bi>=0
a1[++len1] = (Project) {
x, y
};
}
inline bool cmp1(Project x, Project y) {
return x.a < y.a;//以ai大小升序排列
}
for (int i = 1; i <= len1; i++) {//先尽可能多的赚取rating值
if (r >= a1[i].a) {//该任务可以完成
r += a1[i].b;
} else {//该任务可以不完成
cout << "NO" << endl;
return;//没有继续的必要了
}
}
再将 b i ≥ 0 b_i\ge0 bi≥0的任务单独分出,以 a i + b i a_i+b_i ai+bi大小升序排列,并逐个完成。即:
inline bool cmp2(Project x, Project y) {
return x.a + x.b > y.b + y.a;//以ai+bi降序排列
}
for (int i = 1; i <= len2; i++) {
if (r >= max(a2[i].a, -a2[i].b)) {//该任务可以不完成
r += a2[i].b;
} else {
cout << "NO" << endl;
return;//没有继续的必要了
}
}
附上贪心证明:
总之,十分简单。
但是,某OJ的评级是
显然,评级者*长脑子。
AC Code
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <Licenses - GNU Project - Free Software Foundation>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif//=#include<bits/stdc++.h>
using namespace std;
#define int long long//防止被出题人暗算
const int Maxn = 6e4 + 5;
int n, r, x, y;
int len1, len2;
struct Project {
int a, b;
} a1[Maxn], a2[Maxn];
inline bool cmp1(Project x, Project y) {
return x.a < y.a;//以ai大小升序排列
}
inline bool cmp2(Project x, Project y) {
return x.a + x.b > y.b + y.a;//以ai+bi降序排列
}
inline void work() {
cin >> n >> r;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
if (y >= 0) {//bi>=0
a1[++len1] = (Project) {
x, y
};
} else {
a2[++len2] = (Project) {
x, y
};
}
}
sort(a1 + 1, a1 + len1 + 1, cmp1);
sort(a2 + 1, a2 + len2 + 1, cmp2);
for (int i = 1; i <= len1; i++) {//先尽可能多的赚取rating值
if (r >= a1[i].a) {//该任务可以完成
r += a1[i].b;
} else {//该任务可以不完成
cout << "NO" << endl;
return;//没有继续的必要了
}
}
for (int i = 1; i <= len2; i++) {
if (r >= max(a2[i].a, -a2[i].b)) {//该任务可以不完成
r += a2[i].b;
} else {
cout << "NO" << endl;
return;//没有继续的必要了
}
}
cout << "YES" << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
work();
return 0;
}
什么?写完了?快去看看hard版
hard版不会戳我