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

题意:

在这里插入图片描述

解法:
暴力跳的复杂度是O(n/1+n/2+....)=O(n*log).

但是有m个位置被禁止了,每次遇到被禁止的位置,我们需要回退到左边第一个未被禁止的位置(可以预处理),
然后放路灯,回退造成的额外复杂度大概是O(m/1+m/2....)=O(m*log)?

总复杂度在可接受范围内.
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxm=2e6+5;
int mark[maxm];
int l[maxm];
int a[maxm];
int n,m,k;
int cal(int x){
    int now=0;
    int cnt=0;
    while(now<n){
        if(!mark[now]){
            now+=x;
            cnt++;
        }else{
            if(l[now]<x&&now-l[now]>=0){
                now=now-l[now]+x;
                cnt++;
            }else{
                return -1;
            }
        }
    }
    return cnt;
}
void solve(){
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++){
        int x;cin>>x;
        mark[x]=1;
    }
    for(int i=1;i<=k;i++){
        cin>>a[i];
    }
    if(mark[0])l[0]=1;
    for(int i=1;i<n;i++){
        if(mark[i]){
            l[i]=l[i-1]+1;
        }else{
            l[i]=0;
        }
    }
    int ans=1e18;
    for(int i=1;i<=k;i++){
        int cnt=cal(i);
        if(cnt!=-1){
            ans=min(ans,cnt*a[i]);
        }
    }
    if(ans==1e18)ans=-1;
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    solve();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值