Zjnu-2094-d-ghosts are here.

Source: Internet
Author: User

D-Ghosts are coming!!
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 39 Accepted: 11

Description

Lele read a lot of ghost stories, he suspected this evening (0:00-6:00 this time, the following 0-360 for ghost time) will have n ghost to find him. He now knows the time of every ghost come, if the ghost came, you need to light the room with candles, each candle can burn t minutes, at least need K candles to scare away ghosts.

Lele wants to know how many candles he needs at least to spend the night safely.

Input

The first line of 3 integers n, T, K, meaning such as the title description.

The second row n integers, indicating the time each ghost comes, the time is monotonically increasing.

Output

The output requires a minimum number of candles, if not the ghost, all scare away, output-1.

Sample Input

1 8 3102 10 15 81 1 310

Sample Output

31-1

Hint

If a candle is lit at point x, then this candle will illuminate the room at [X+1,x+t] this time.

Candles can be lit at any time (only one), when the time of the candle is negative, expressed before midnight.

For 100% of the data, the range of N, K, T [1,300], the time of the Ghost [1,360];


This problem I think for a long time, but the main card is still the code implementation aspects.

The title means: Now there are n ghosts, and then tells you that the duration of the candle is only TS, and tells you to destroy a monster to have a K-candle, and then the second line gives the occurrence of n ghosts, time is in ascending order. Ask you how many candles are needed to destroy these monsters at least?

Greedy thought it is easy to think of, we want to make the number of candles as little as possible, then we will be the candle as far as possible from the place near the monster start point.

Note that the time here may start from before midnight, so it will produce negative, for convenience, we directly add a lot of time, so it is all positive.

Then we first have to determine whether in the current ghost moment is safe, for a time to see if the number of candles is greater than or equal to the quantity required, if not to reach the level of safety, then from the time of the ghost appears the nearest place to start to insert candles, and finally judge the good, you can know that can meet to destroy all the ghosts, and the minimum number of candles.


#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define N 1005 using namespace Std; BOOL light[2*n]={0};//Indicates whether the current moment is dot candle int main () {int n,m,t,r,i,j,k,x,c;scanf ("%d%d%d", &m,&t,&r); int ans=0;/ /x for each ghost's time;//c records how many candles are currently lit, for (i=1;i<=m;i++) {scanf ("%d", &x), and/or where x plus n is to prevent the time of the candle from being negative, then it will be inaccurate; x +=n;c=0;//from the first second of the current moment, for (j=x-1;j>=0;j--)//To determine whether the current moment is safe if (j+t>=x) {c+=light[j];} else break;//If the number of candles is greater than or equal to the required quantity, then the next loop is carried out directly, and if (c>=r) Continue;//start: Indicates that the security level has not been reached at this time;//This is a greedy algorithm, starting from where the Ghost is near; for (j= x-1;j>=0;j--) {//If it is unsafe, go from the back to the candle if (j+t<x) break;//when the point of the candle can not touch the ghost, then jump out of the loop, and output-1; if (!light[j]) {//If the candle is not lit at J at this time , then the electric light it, and marked 1, the number of candles plus 1; c++;ans++;light[j]=1;} If the number of candles already is greater than or equal to the number of requirements, then the ghost can be eliminated, jumping out of the loop; if (c>=r) break;} End if (c<r) break;} if (i<=m) puts ("-1"); else printf ("%d\n", ans); return 0;}

Look at the code, the idea has become open up.

Come on!

Zjnu-2094-d-Ghost is coming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.