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.
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.
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.
1 8 3 10
3
2 10 1 5 8
1
1 1 3 10
-1
题意:一个人一秒钟可以点一根蜡烛,每个魔鬼来的时候需要点至少r根蜡烛,问他至少需要点多少蜡烛。
思路:首先t<r时不能完成。然后每次如果蜡烛不够的时候,尽可能在最晚的时间点蜡烛。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
struct node
{
int val;
bool operator<(const node a)const
{
return a.val<val;
}
};
node A;
priority_queue<node> qu;
int T,t,n,m,w[1010],maxn,light[1010],num,sum;
void solve()
{
int i,j,k;
scanf("%d%d%d",&n,&t,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&w[i]);
}
sort(w+1,w+1+n);
if(t<m)
{
printf("-1\n");
return;
}
for(i=1;i<=n;i++)
w[i]+=500;
for(i=1;i<=n;i++)
{
while(!qu.empty())
{
A=qu.top();
if(A.val<w[i])
{
qu.pop();
num--;
}
else
break;
}
k=w[i]-1;
while(num<m)
{
if(light[k]==0)
{
A.val=k+t;
qu.push(A);
num++;sum++;
light[k]=1;
}
k--;
}
}
printf("%d\n",sum);
}
int main()
{
solve();
}

903

被折叠的 条评论
为什么被折叠?



