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

题目大意:给出n个鬼访问的时间,和每个蜡烛可以持续的时间,每次访问最少需要的点燃的蜡烛数。问一共最少需要几个蜡烛。

将每个蜡烛的点燃的时间尽量靠近访问的时间,所以,统计第i个访问时点燃的蜡烛,如果少于r,那么要访问的时间开始,向前遍历,找出最近的可以点燃蜡烛的时刻,这样可以保证使用的蜡烛最少。

注意

1、可以在子夜之前点蜡烛,所以要将时间右移。

2、蜡烛持续的时间t和点燃的时间j的关系为,j时刻点蜡烛,j+1到j+t时刻蜡烛亮着。


用结构体表示蜡烛,里面记录这根蜡烛燃烧的时间段,然后扫描鬼魂拜访时间,在w[i] 时刻判断有哪些蜡烛在燃烧,没有燃烧的就要在w[i]时刻之前将它点燃,点燃它的时刻点离w[i]越近越好,所以从w[i]-1时刻往前推。时间复杂度为O(N^2)

当然也可以用BIT做做优化将时间复杂度降为O(NlogN)


//      whn6325689
//		Mr.Phoebe
//		http://blog.youkuaiyun.com/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int MAXN=1010; 

struct St  
{  
    int s,e;  
}ca[MAXN];  
  
int m,t,r;  
int w[MAXN];  
int vis[MAXN];  
  
int main()  
{  
    while(read(m)&&read(t)&&read(r))  
    {  
        CLR(vis,0);  
        CLR(ca,0);
        for(int i=0;i<m;i++)  
            read(w[i]),w[i]+=300; 
        int ans=0;
        int ff=1;
        for(int i=0;i<m;i++)
        {  
            int tt=w[i],ss=w[i];     
            tt--;
            for(int j=0;j<r;j++)
            {  
                if(ca[j].e<ss)
                {  
                    int flag=0;
                    while(tt+t>=ss)  
                    {  
                        if(!vis[tt])
                        {  
                            ca[j].s=tt; 
                            ca[j].e=tt+t;  
                            vis[tt]=1;
                            ans++;  
                            flag=1;     
                            break;  
                        }  
                        tt--;
                    }  
                    tt--;  
                    if(!flag)  
                        ff=0; 
                }  
            }  
        }  
        if(ff)  
            printf("%d\n",ans);  
        else  
            printf("-1\n");  
    }  
    return 0;  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值