Codeforces Round #288 (Div. 2) C. Anya and Ghosts(贪心)

来源:互联网 发布:斜面小车 能测几组数据 编辑:程序博客网 时间:2024/05/16 18:32

Anya loves to watch horror movies. In the best traditions of horror, she will be visited bym ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactlyt 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 exactlyt 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: thei-th visit will happen wi seconds after midnight, allwi'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 leastr 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 m,t, r (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 numberswi (1 ≤ i ≤ m,1 ≤ wi ≤ 300), thei-th of them repesents at what second after the midnight thei-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 310
Output
3
Input
2 10 15 8
Output
1
Input
1 1 310
Output
-1

-----------------------------------------------

题意:

m个鬼,每个鬼到来持续一秒,需要r盏蜡烛,求出每个鬼到来的时候都有r盏蜡烛的最少需蜡烛数,无法实现时输出-1。

思路:

蜡烛在鬼到来的前一秒点燃最佳,如果每一个鬼到来的时候没有足够的蜡烛,则从那一时刻往前面增加蜡烛。

CODE:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int g[10005], cnt[10005];int main(){//freopen("in", "r", stdin);    int m, t, r;    while(~scanf("%d %d %d", &m, &t, &r)) {        memset(cnt, 0, sizeof(cnt));        for(int i = 0; i < m; ++i) {            scanf("%d", &g[i]);        }        if(t < r) {            printf("-1\n");            continue;        }        int ans = 0;        for(int i = 0; i < m; ++i) {            for(int j = g[i] - 1; cnt[g[i]] < r; --j) {                for(int k = j + 1; k <= j + t; ++k) {                    if(k >= 0) cnt[k]++;                }                ans++;            }        }        printf("%d\n", ans);    }    return 0;}




0 0
原创粉丝点击