CF Anya and Ghosts (贪心)

Anya and Ghosts
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Anya loves to watch horror movies. In the best traditions of horror, she will be visited by m ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly t seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly t seconds and then goes out and can no longer be used.

For each of the m ghosts Anya knows the time at which it comes: the i-th visit will happen wi seconds after midnight, all wi's are distinct. Each visit lasts exactly one second.

What is the minimum number of candles Anya should use so that during each visit, at least r candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time.

Input

The first line contains three integers mtr (1 ≤ m, t, r ≤ 300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.

The next line contains m space-separated numbers wi (1 ≤ i ≤ m1 ≤ wi ≤ 300), the i-th of them repesents at what second after the midnight the i-th ghost will come. All wi's are distinct, they follow in the strictly increasing order.

Output

If it is possible to make at least r candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.

If that is impossible, print  - 1.

Sample test(s)
input
1 8 3
10
output
3
input
2 10 1
5 8
output
1
input
1 1 3
10
output
-1





一直WA,果然是题意理解错了,如果第i秒点蜡烛,那么蜡烛会在i+1到i+t的时间段亮,被我理解到i+1+t,唉还指着那句话跟队友说。
贪心策略就是尽可能晚点蜡烛,因为点早了没意义,晚点还可以为后续的幽灵提供照明,所以尽可能在靠近幽灵到达的时间来点。
 1 #include <bits/stdc++.h>
 2 using    namespace    std;
 3 
 4 const    int    INF = -100000000;
 5 int    main(void)
 6 {
 7     int    m,t,r;
 8     int    s[305];
 9     int    candle[305];
10     int    sum,cur;
11 
12     cin >> m >> t >> r;
13     for(int i = 0;i < m;i ++)
14         cin >> s[i];
15     if(t - r < 0)
16         puts("-1");
17     else
18     {
19         sum = 0;
20         fill(candle,candle + 303,INF);
21         for(int i = 0;i < m;i ++)
22         {
23             cur = 0;
24             for(int j = 0;j < r;j ++)
25                 if(candle[j] + t < s[i])
26                 {
27                     candle[j] = s[i] - cur - 1;
28                     cur ++;
29                     sum ++;
30                 }
31         }
32         printf("%d
",sum);
33     }
34 
35     return    0;
36 }
原文地址:https://www.cnblogs.com/xz816111/p/4456926.html