I install water dispenser China University of petroleum freshman training competition #10

Question I: installation of water dispenser

time limit:   one   Sec    Memory limit:   128 MB
Submit state

Title Description

In order to advocate a low-carbon life in the city, the municipal Civilization Office plans to hold a marathon, and set up some observation points along the way to ensure the safety of the race. One observer is stationed at each observation point. Due to the hot weather, some water dispensers need to be installed along the way so that observers can get water and drink. It takes one unit of physical strength for each unit of distance the observer moves. Each observer's physical strength is limited. He can only get water and drink within the range of his physical strength, or he will die of thirst or fatigue.
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

There are several lines of input data..
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

At least several drinking fountains shall be installed at the output.

sample input  

4
6 3
12 2
1 5
14 5

sample output  

2

Tips

He can install the water dispenser 6 and 12 away from the starting point so that all observers can drink water. There are many schemes, which only need to output at least a few drinking fountains.
 
Idea:
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.
And the carrier is a straight line positive integer number axis, it is difficult not to think of using the method of binary answer to solve the problem.
The second is the number of drinking fountains, and the special state is the minimum. Select the dichotomous template that returns the answer directly:
    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;    
}

 

 

 

 

 

Posted by KPH71 on Fri, 03 Dec 2021 12:39:38 -0800