2020 Winter Holiday [gmoj2222] [chicken to save the chick] [simulation]

Title description

The chicken country has encountered a very difficult problem recently. There is often an eagle who wants to catch the chicken. After investigation by the chicken intelligence officer, the eagle intends to attack n times in total, the time of the i-th coming is ti (1≤i≤n) second.
In order to protect the chickens in the chicken country, the king of the chicken country decided to send the police of the chicken country (an infinite number of police in the chicken country) to patrol. The duration of each police patrol is t seconds. At the moment when the eagle comes to attack, at least x police officers can resist the eagle attack. In addition, the King has two principles for sending police:
(1) Only one police can be sent at a time. It is also possible to prepare to send a police in advance at the 0th second and the time before the 0th second (the negative moment in the chicken country), but only one policeman can be dispatched at a time.
(2) Delay 1 second to execute the patrol task. The police dispatched at the i-th second will perform the patrol task from the i-th to i + t seconds.
To help the King save money, please help to calculate at least how many police officers need to be dispatched to ensure that the chickens of the Chicken Kingdom are not captured by the eagles?

Input

Enter a total of 2 lines.
In line 1, enter three integers n, t, and x, respectively representing the total number of eagle attacks, the length of each police patrol, and the minimum number of police officers that can resist the eagle attack at a certain time.
In the second line, n positive integers ti arranged in strict ascending order (1≤i≤n), indicating that the eagle will launch an attack at the ti second.

Output

It outputs an integer in 1 line, indicating how many polices must be dispatched at least to defend against the eagle's n attacks. If there is an attack that cannot resist the eagle, it outputs "-1" (without the double quotes).

Sample input

Input1:

3 3 3
2 3 4

Input2:

1 2 3
3

Sample output

Output1:
5

Output2:
-1

Data range limitation

Insert picture description here

prompt

Sample1: In
sample 1, the eagle came to attack three times, at the second, third, and fourth seconds, each police patrol time was 3 seconds. When the eagle came to attack, there must be at least three policemen to resist the eagle attack . First, you can send a policeman at the three times of -1, 0, and 1 second to resist the attack of the Eagle at the second time, and then send a policeman at the second time, together with the two times of 0, 1 second The dispatched police (the police dispatched at the time of -1 second has already rested) can resist the attack of the eagle at the 3rd second, and finally dispatch a policeman at the 3rd second, together with the two dispatched at the 1st and 2nd seconds The police (at this time, the police dispatched at the 0th second has also rested) can resist the eagle's attack at the 4th second, so at least 5 policemen need to be dispatched.

Sample2: In
example 2, the eagle came to attack once. At the 3rd second, each police patrol time is 2 seconds, but when the eagle comes to attack, there must be at least 3 policemen to resist the eagle attack, according to the king's dispatch The principle of the police cannot fulfill the task of defending the eagle, and outputs "-1".

analysis

This question is also a simulation, as long as it is determined whether a police has been dispatched at that point. Note that there are 10,000 positions (with negative numbers) in front of the array .
Then figure out how many police officers are still missing in the current attack.

Code on

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int ff=10000;//在0之前空出10000个位置可以派警察qwq 
int n,t,x,ans,f,a[100010],sum;
int main()
{
    freopen("chicken.in","r",stdin);
    freopen("chicken.out","w",stdout);
    cin>>n>>t>>x;
    if(x>t)
    {
        cout<<-1;
        return 0;
    }
    for(int i=1;i<=n;i++)
    {
        cin>>f;
        sum=0;
        for(int j=f-1;j>=f-t;j--)
        {
        	if(a[j+ff]) sum++;
		}
        if(sum<x)
        {
        	for(int j=f-1;j>=f-t;j--)//有效时间 
            {
                if(!a[j+ff])//这个时间点之前不能派过 
                {
                    a[j+ff]=1;//记录这个时间点派了警察 
                    ans++;//派了一个警察 
                    sum++;//增加一个警察 
                    if(sum==x) break;//够数 
                }
            } 
		}
    }
    cout<<ans;
    fclose(stdin);
    fclose(stdout);
    return 0;
}

Published 110 original articles · 100 praises · 8010 visits

Guess you like

Origin blog.csdn.net/dglyr/article/details/105150556