Luogu P3853 Problem Solving Report

P3853 Road Sign Settings

topic background

There is a long highway between city B and city T. There are road signs in some parts of this highway, but everyone feels that there are too few road signs, and there are often quite long distances between adjacent road signs. some distance. In order to facilitate the study of this problem, we define the maximum distance between adjacent road signs on the road as the "empty index" of the road.

Topic description

Now the government has decided to add some road signs to the road to minimize the "empty index" of the road. They ask you to design a program to calculate what the minimum value can be. Note that the start and end of the road are guaranteed to be signposted, the length of the road is an integer, and both the old signpost and the new signpost must be an integer number of units away from the starting point.

Input and output format

Input format:

The first line includes three numbers L, N, K, which represent the length of the road, the number of existing road signs, and the maximum number of road signs that can be added.

The second line includes N integers arranged in increments, which respectively represent the positions of the original N road signs. The location of the road sign is represented by the distance from the starting point, and must be in the interval [0,L].

Output format:

Output 1 line, containing an integer that represents the minimum "empty index" value that can be achieved after adding road signs.


I was wrong at first

Greedy: Take the maximum interval interpolation point each time.


Correct solution: two-point answer, add points when checking

CODE:

#include <cstdio>
#include <algorithm>
#define mid (l+r>>1)
using namespace std;
const int N=100010;
int L,n,k,a[N];

bool check(int d)
{
    int last=0,cnt=k;
    for(int i=1;i<=n;i++)
    {
        while(a[i]>last+d)
        {
            cnt--;
            last+=d;
        }
        last=a[i];
        if(cnt<0)
            return false;
    }
    return true;
}

int main()
{
    scanf("%d%d%d",&L,&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",a+i);
    sort(a+1,a+1+n);
    a[++n]=L;
    int l=1,r=L;
    while(l<r)
    {
        if(check(mid))
            r=mid;
        else
            l=mid+1;
    }
    printf("%d\n",l);
    return 0;
}

2018.5.5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325367812&siteId=291194637