您的位置:首页 > 其它

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

2015-02-05 22:26 281 查看
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 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 numbers
wi (1 ≤ i ≤ m,
1 ≤ 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 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces