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

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 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?≤?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

题意:午夜过后会有m个鬼魂来拜访,给出他们来拜访的时间点,现在有很多蜡烛,每个蜡烛能够燃烧 t 秒,Anya点燃一根蜡烛需要一秒的时间,比如Anya在p时刻点燃一根蜡烛,这根蜡烛会在p+1~p+t
时间段内燃烧,Anya能在任意时刻点蜡烛,现在要求当一个鬼魂来拜访的时候要有r 支蜡烛亮着,问Anya最少要点多少支蜡烛。

思路:用结构体表示蜡烛,里面记录这根蜡烛燃烧的时间段,然后扫描鬼魂拜访时间,在come[i] 时刻判断有哪些蜡烛在燃烧,没有燃烧的就要在come[i]时刻之前将它点燃,点燃它的时刻点离come[i]越近越好,所以从come[i]-1时刻往前推。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
using namespace std;

struct St
{
    int s,e;
}st[maxn];

int m,t,r;
int come[maxn];
int dian[maxn];

int main()
{
    while (~scanf("%d%d%d",&m,&t,&r))
    {
        memset(dian,0,sizeof(dian));
        for (int i=0;i<r;i++)
        {
            st[i].s=-1;
            st[i].e=-1;
        }
        for (int i=0;i<m;i++)
            scanf("%d",&come[i]),come[i]+=300;  //鬼魂到来的时刻,要预先加300
        int ans=0;   //最少的蜡烛数
        int ff=1;   //标记是否能达到要求
        for (int i=0;i<m;i++)   //遍历鬼魂到来的时刻点
        {
            int tt=come[i],ss=come[i];
            tt--;          //先自减,要从tt时刻从后向前推看哪个时刻能够点蜡烛,这样就能保证用最少的蜡烛
            for (int j=0;j<r;j++)    //判断r跟蜡烛
            {
                if (st[j].e<ss)    //第j跟蜡烛在ss时刻,也就是鬼魂到来之前就燃烧完了,需要再点一根
                {
                    int flag=0;   //标记能否将这根蜡烛点亮
                    while (tt+t>=ss)
                    {
                        if (!dian[tt])  //在tt时刻可以点
                        {
                            st[j].s=tt;    //重新记录第j跟蜡烛的燃烧时间段
                            st[j].e=tt+t;
                            dian[tt]=1;  //标记,以后该时刻不能点了
                            ans++;    //蜡烛数自增
                            flag=1;
                            break;
                        }
                        tt--;   //tt往前推
                    }
                    tt--;
                    if (!flag)    //不能点亮
                        ff=0;    //不符合要求,要输出-1
                }
            }
        }
        if (ff)
            printf("%d\n",ans);
        else
            printf("-1\n");
    }
    return 0;
}
时间: 2024-10-02 13:30:06

Codeforces Round #288 (Div. 2) C. Anya and Ghosts的相关文章

Codeforces Round #288 (Div. 2)

A. Pasha and Pixels 题目大意:给出n*m的棋盘,初始为全白,每次在上面使一个格子变黑,问多少次的时候使得棋盘上出现2×2的黑格 思路:模拟 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #define maxn 900000 6 #define LL long long 7 using namespace std; 8 boo

Codeforces Round #297 (Div. 2)E. Anya and Cubes

题目链接:http://codeforces.com/problemset/problem/525/E 题意: 给定n个数,k个感叹号,常数S 下面给出这n个数. 目标: 任意给其中一些数变成阶乘,至多变k个. 再任意取一些数,使得这些数和恰好为S 问有多少方法. 思路: 三进制状压,0代表不取,1代表取阶乘,2代表直接取: 中途查找,节约空间: 代码如下: #include<cstdio> #include<cstring> #include<iostream> #i

Codeforces Round #288 (Div. 2) 待续

A. Pasha and Pixels ( 暴力 ) 题意:给一个n*m的矩阵染色(初始为全白),如果在k步之内染出一个2*2的矩阵,输出最小步数,否则输出0 分析:brute force #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int M[ 1010 ][ 1010 ]; int step, r, c, k

Codeforces Round #288 (Div. 2)B(字符串)

题意:交换两个数,使得交换后的数是偶数且尽可能大: KEY:分情况,1末尾数为偶数,即找到比它小的偶数交换,假如没有比它小的偶数,不用交换.2末尾数是奇数,再分情况,<1>全都是奇数(这个可以在一开始就判断掉),即输出-1,<2>有一个偶数,无论如何谁大谁小都要交换,<3>全部偶数都比奇数大,从n - 2(n是字符串长度)开始找到一个偶数交换即可,<4>如果有一些偶数比末尾数大,有些比它小,即从0开始找到比奇数小的那个偶数交换即可.其实是分类讨论.(有更好的

Codeforces Round #288 (Div. 2) ABCDE

A题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 12 #define LL

Codeforces Round #288 (Div. 2) A,B,C,D,E

A:一个一个点向图里面加,判断其所在的位置与其他的点是否可以构成小矩形就可以了. B:贪心,如果前面的偶数有比他小的就找到一个最靠前的交换,如果前面的偶数都比它小,就找一个最靠后的交换. C:贪心,把蜡烛尽可能的放在恶魔,来之前,这样可以充分利用蜡烛的时间,模拟一下,不停地向前方就可以了.如果蜡烛时间小于需要的数目,一定不可以. const int maxn = 2010; int vis[maxn]; int num[maxn]; int main() { int m, t, r; while

Codeforces Round #288 (Div. 2) A. Pasha and Pixels

A. Pasha and Pixels time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha loves his phone and also putting his hair up... But the hair is now irrelevant. Pasha has installed a new game to

Codeforces Round #288 (Div. 2) B. Anton and currency you all know

B. Anton and currency you all know time limit per test 0.5 seconds memory limit per test 256 megabytes input standard input output standard output Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/