cf Educational Codeforces Round 45 E. Post Lamps

原题:

E. Post Lamps
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Adilbek’s house is located on a street which can be represented as the OX axis. This street is really dark, so Adilbek wants to install some post lamps to illuminate it. Street has n positions to install lamps, they correspond to the integer numbers from 0 to n−1 on the OX axis. However, some positions are blocked and no post lamp can be placed there.

There are post lamps of different types which differ only by their power. When placed in position x, post lamp of power l
illuminates the segment [x;x+l]. The power of each post lamp is always a positive integer number.

The post lamp shop provides an infinite amount of lamps of each type from power 1 to power k. Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power l cost ai each.

What is the minimal total cost of the post lamps of exactly one type Adilbek can buy to illuminate the entire segment
[0;n] of the street? If some lamps illuminate any other segment of the street, Adilbek does not care, so, for example, he may place a lamp of power 3 in position n−1 (even though its illumination zone doesn’t completely belong to segment
[0;n]).

Input
The first line contains three integer numbers n, m and k (1≤k≤n≤10^6, 0≤m≤n) — the length of the segment of the street Adilbek wants to illuminate, the number of the blocked positions and the maximum power of the post lamp available.

The second line contains m integer numbers s1,s2,…,sm (0≤s1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000001;
const ll inf=1e18;

int n,m,k;
bool block[maxn];
ll cost[maxn];
int road[maxn];


int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m>>k)
    {
        memset(block,0,sizeof(block));
        memset(road,0,sizeof(road));
        ll res;
        for(int i=1;i<=m;i++)
        {
            cin>>res;
            block[res]=1;
        }
        for(int i=1;i<=k;i++)
            cin>>cost[i];

        if(block[0])
        {
            cout<<-1<<endl;
            continue;
        }
        int st=-1;
        for(int i=0;i<=n;i++)
        {
            if(block[i])
                road[i]=road[i-1]+1;

            st=max(st,road[i]);
        }
        ll ans=inf;
        res=0;
        int flag,tmp;
        for(int i=st+1;i<=k;i++)
        {
            tmp=flag=res=0;
            int j=0;
            while(j<n)
            {
                if(block[j])
                    j-=road[j];
                res++;
                if(tmp>0&&j<=0)
                {
                    flag=1;
                    break;
                }
                j+=i;
                tmp++;
            }

            if(!flag)
            ans=min(ans,cost[i]*res);
        }
        if(ans>=inf)
            cout<<-1<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

思路:

看到数据范围的时候一惊,非得小于nlogn不可~

考虑动态规划

设置 dp[i] d p [ i ] 表示照亮到第i点时候的最小花费, len[i] l e n [ i ] 表示第i个灯的照明范围, cost[i] c o s t [ i ] 表示第i个灯的价格。

转移方程 dp[i]=min(dp[j]+cost[k])k=ijj+1 d p [ i ] = m i n ( d p [ j ] + c o s t [ k ] ) ( k = i − j ) 且 j + 1 可 以 放 置 路 灯

然而时间复杂度是o(n^2),抓耳挠腮想不出怎么优化来,看了题解发现,原来题目中有一句话“ each customer is only allowed to order post lamps of exactly one type.(每次只能使用一种灯) ”没看见-_-

那么这道题就是暴力的解法了,求解的时候要先经过预处理,时间复杂度是n/1+n/2+..+n/2=nlogn

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值