现在的位置: 首页 > 综合 > 正文

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

2019年02月17日 ⁄ 综合 ⁄ 共 3363字 ⁄ 字号 评论关闭
C. 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 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 3
10
Output
-1
Note

Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.

It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.

In the first sample test three candles are enough. For example, Anya can start lighting them at the
3-rd, 5-th and
7
-th seconds after the midnight.

In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.

In the third sample test the answer is  - 1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.

贪心,点蜡烛时间尽量靠近客人来的时候,并且保证来的时候一定有r个蜡烛亮,然后模拟一下这个过程就行了

/*************************************************************************
    > File Name: cf3.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年01月28日 星期三 01时24分42秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;

queue <int> qu;
int w[400];

int main ()
{
	int m, t, r;
	while (~scanf("%d%d%d", &m, &t, &r))
	{
		int cnt = 0, ans = 0;
		for (int i = 1; i <= m; ++i)
		{
			scanf("%d", &w[i]);
		}
		while (!qu.empty())
		{
			qu.pop();
		}
		if (r > t)
		{
			printf("-1\n");
			continue;
		}
		bool flag = true;
		for (int i = w[1] - r; i <= w[1] - 1; ++i)
		{
			qu.push (i + 1);
			++cnt;
			++ans;
		}
		for (int i = 2; i <= m; ++i)
		{
			while (!qu.empty ())
			{
				int u = qu.front();
				if (u + t - 1 < w[i])
				{
					qu.pop();
					--cnt;
				}
				else
				{
					break;
				}
				if (cnt == 0)
				{
					break;
				}
			}
			if (cnt >= r)
			{
				continue;
			}
			int cur = r - cnt;
			if (cur > t || w[i] - cur < w[i - 1])
			{
				flag = false;
				break;
			}
			for (int j = w[i] - cur; j <= w[i] - 1; ++j)
			{
				qu.push (j + 1); //开始亮的时间
				++cnt;
				++ans;
			}
		}
		if (!flag)
		{
			printf ("-1\n");
		}
		else
		{
			printf("%d\n", ans);
		}
	}
	return 0;
}

抱歉!评论已关闭.