Title Description
Smart Nannan also participated in the preparations for the competition. His task is to design an ideal scheme for installing drinking fountains to minimize the number of drinking fountains installed, but to ensure that all observers can get water to drink.
input
The first line, only an integer, indicates that there are n (0 < n < = 1000) observation points.
Next, there are N lines, each line has two integers S (0 < S < = 100000) and w (0 < w < = 50000), where S represents the distance from an observation point to the starting point, and W represents the physical strength of the resident observer in the observation point.
output
sample input
4 6 3 12 2 1 5 14 5
sample output
2
Tips
int l = 0, r = n; while(l < r) { int mid = l + r >> 1; if(check(mid)) l = mid + 1; else r = mid; } cout<<l;
Each data in the input data has two related data, which are related to each point, so consider creating a structure for calculation and input.
After that, you need to write the check function. The first consideration is to sort from small to large according to its position, and then greedily traverse each element. If the current nearest water dispenser position is within the range, it will be skipped, otherwise it will be placed at the right end of the current element (because it is sorted by position)
However, this greedy + sorting method can not ac, for example, a group of hack data:
5 1 9999 2 1 3 8888 4 1 5 7777
If this group of data is traversed according to the above greed, the first water dispenser location will be updated at 10000, and then the second will be placed at 3. In fact, it only needs to be placed at 3 to meet all needs
Therefore, it is necessary to change a greedy way to observe this group of hack data. It can be found that if you sort from small to large according to a[i] + b[i], you can finish each situation greedily
Therefore, after entering data, sort the structure array from small to large according to a[i] + b[i], which can be realized by rewriting sort.
Remember to initialize the water dispenser position to 0x3f3f3f3f (large enough)
Code: 2021.12.3
/* ************************************ ***********emu^w^*********** */ #include <bits/stdc++.h> using namespace std; #define x first #define y second const int P = 13131; #define ll long long const int mod = 1E6 + 7; const int INF = 0x3f, sINF = 0x3f3f3f3f; typedef unsigned long long ULL; typedef pair<int, int> PII; typedef pair<long long, long long> PLL; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; // const int N = 1010; int n; struct answ{ int a, b; }q[N]; bool cmp(const answ &x, const answ &y) { if(x.a + x.b != y.a + y.b) return x.a + x.b < y.a + y.b; return x.a < y.a; } bool check(int x) { int nums = x; int loc = 0x3f3f3f3f; for(int i = 1; i <= n; i++) { if(loc > q[i].a + q[i].b || loc < q[i].a - q[i].b) { nums--; loc = q[i].a + q[i].b; } if(nums < 0) return true; } return false; } int main() { cin>>n; for(int i = 1; i <= n; i++) cin>>q[i].a>>q[i].b; sort(q + 1, q + 1 + n, cmp); int l = 0, r = n; while(l < r) { int mid = l + r >> 1; if(check(mid)) l = mid + 1; else r = mid; } cout<<l; }