E. Post Lamps
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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’>nn is impossible, print -1.
Examples
Input
Copy
6 2 3
1 3
1 2 3
Output
Copy
6
Input
Copy
4 3 4
1 2 3
1 10 100 1000
Output
Copy
1000
Input
Copy
5 1 5
0
3 3 3 3 3
Output
Copy
-1
Input
Copy
7 4 3
2 4 5 6
3 14 15
Output
Copy
-1
题意: 一个一维的街道 长度为n 即有n个点 现在给出k种路灯 要你选区其中一种 照亮整个街道 其中有些点被锁住了 不能放路灯 k种路灯所能照亮的长度分别为1到k 给出每种路灯的花费 问你最少花费多少钱 能使整条街道照亮。 不能照亮整条街道就输出-1
一个纯粹的暴力题 只要会估计复杂度就能写。
首先考虑什么情况下不能有满足条件的方案。
只有在连续障碍物的长度≥k时 才会出现无法照亮整个街道的情况。
那么 剩下的询问就是全部都有满足条件的情况,接下来就是要考虑 什么情况才是最优的。
这里可以贪心的考虑 对于每个路灯的摆放位置 肯定是选择能照亮更多没有被照亮的区域,那么 我们就模拟这个过程就行了。
那么 总共的遍历次数就是n/1+n/2+n/3+……..n/k; k最大为n
那么 最坏复杂度为nlogn级别的。n为1e6 完全可以接受..
实际跑了400ms 很快
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX= 1e6+10;
int a[MAX];
int pre[MAX];
int dp[MAX*2];
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
int cnt;
scanf("%d",&cnt);
dp[cnt+1]++;
}
int len=0;
int maxs=0;
pre[0]=1e9;
for(int i=1;i<=n;i++){
if(dp[i]>0){
len++;
maxs=max(maxs,len);
pre[i]=min(i,pre[i-1]);
}else{
pre[i]=1e9;
len=0;
}
}
for(int i=1;i<=k;i++){
scanf("%d",&a[i]);
}
if(dp[1]>0 || maxs>=k){
puts("-1");
return 0;
}
long long ans=1e18;
for(int i=maxs+1;i<=k;i++){
long long cnt=0;
int index=1;
while(index<=n){
cnt+=a[i];
index+=i;
if(dp[index]>0){
index=pre[index]-1;
}
}
ans=min(ans,cnt);
}
cout<<ans<<endl;
}