CodeForces 990EPost Lamps

Posted fang-hao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 990EPost Lamps相关的知识,希望对你有一定的参考价值。

传送门戳这里

Luogu & CodeForces

 

题目描述

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 nn positions to install lamps, they correspond to the integer numbers from 00 to n - 1n1 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 xx , post lamp of power ll illuminates the segment [x; x + l][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 11 to power kk . Though each customer is only allowed to order post lamps of exactly one type. Post lamps of power ll cost a_lal? 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][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 33 in position n - 1n1 (even though its illumination zone doesn‘t completely belong to segment [0; n][0;n] ).

 

 

解题思路

既然数据不大,我们可以想想怎么暴力。

显然暴利的方法就是贪心,从头到尾一个一个放,遇到障碍再向前倒退,当照明距离越短时复杂度越小,应该是调和级数,差不多nlogn。

还有一点找障碍前面的能放的位置的时候千万不能循环着找,会T。

那么我们先O(n)的时间内预处理出来一个数组,表示当前位置之前包括当前位置的第一个能放路灯的地方的位置,每次查询这个数组就好了。

 

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,k;
 7 bool ba[1000010];
 8 int p[1000010],l[1000010];
 9 inline void read(register int &x){
10     x=0; register char ch=getchar();
11     while(ch<0||ch>9)ch=getchar();
12     while(ch>=0&&ch<=9)x=x*10+ch-0,ch=getchar();
13 }
14 inline long long calc(int num){
15     int pos=0;
16     register long long ans=p[num];
17     if(ba[0])return -1;
18     while(pos<n){
19         register int nepos=pos+num;
20         if(nepos>=n)return ans;
21         if(ba[nepos]){
22             if(l[nepos]==pos)return -1;
23             else nepos=l[nepos];
24         }
25         pos=nepos;
26         ans+=(long long)p[num];
27     }
28     return ans;
29 }
30 int main(){
31     read(n),read(m),read(k);
32     for(register int i=1;i<=m;i++){
33         register int pos;
34         read(pos);
35         ba[pos]=1;
36     }
37     for(register int i=1;i<=k;i++)read(p[i]);
38     for(register int i=1;i<=n;i++){
39         if(ba[i])l[i]=l[i-1];
40         else l[i]=i;
41     }
42     long long ans=9999999999999LL;
43     for(register int i=1;i<=k;i++){
44         long long tmp=calc(i);
45         if(tmp!=-1)ans=min(ans,tmp);
46     }
47     if(ans==9999999999999)cout<<-1<<endl;
48     else cout<<ans<<endl;
49 }

 

以上是关于CodeForces 990EPost Lamps的主要内容,如果未能解决你的问题,请参考以下文章

Post Lamps CodeForces - 990E(暴力出奇迹?)

Codeforces990 E. Post Lamps(复杂度分析)

Educational Codeforces Round 45 E. Post Lamps

CodeForces 990ACommentary Boxes

CodeForces990F Flow Control

Codeforces 990