题意:给定一个字符串s 现在让你用最小的花费 覆盖所有区间

思路:dp[i]表示前i个全覆盖以后的花费 如果是0 我们只能直接加上当前位置的权值 否则 我们可以区间询问一下最小值 然后更新

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int N = 2e5+7;
typedef long long ll;
const ll mod = 998244353;
using namespace std;
ll dp[N];
ll a[N],sum[N];
struct tree{
int l,r;
ll v;
int po;
}t[N<<4];
int nico=0;
void build(int p,int l,int r){
t[p].l=l; t[p].r=r; t[p].v=inf;
if(l==r){
t[p].po=l;
return ;
}
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void update(int p,int x,ll v){
if(t[p].l==t[p].r){
t[p].v=v;
return ;
}
int mid=(t[p].l+t[p].r)>>1;
if(x<=mid) update(p<<1,x,v);
else update(p<<1|1,x,v);
if(t[p<<1].v<t[p<<1|1].v){
t[p].v=t[p<<1].v;
t[p].po=t[p<<1].po;
}else{
t[p].v=t[p<<1|1].v;
t[p].po=t[p<<1|1].po;
}
}
tree query(int p,int l,int r){
if(l<=t[p].l&&t[p].r<=r){
return t[p];
}
int mid=(t[p].l+t[p].r)>>1;
tree res1,res2;
if(l>mid){
return query(p<<1|1,l,r);
}else if(r<=mid){
return query(p<<1,l,r);
}else{
res1=query(p<<1,l,r);
res2=query(p<<1|1,l,r);
if(res1.v<res2.v){
return res1;
}else{
return res2;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,k; cin>>n>>k;
string s; cin>>s;
build(1,0,n);
update(1,0,0);
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+i;
}
int mi=inf;
for(int i=n-1;i>=0;i--){
if(s[i]=='1'&&i!=n-1){
s[min(i+k,n-1)]='1';
s[i]='0';
if(i+k>=n-1){
mi=min(mi,i)+1;
}
}
}
if(s[n-1]=='1'&&mi==inf){
mi=n;
}
// cout<<s<<endl;
for(int i=1;i<=n;i++){
if(s[i-1]=='1'){
if(i==n){
tree res=query(1,max(mi-k-1,0),i-1);
dp[i]=res.v+mi;
// dp[i][0]=min(dp[i-1][0],dp[i-1][1])+mi;
}else{
tree res=query(1,max(i-2*k-1,0),i-1);
dp[i]=res.v+i-k;
// dp[i][0]=min(dp[i-1][0],dp[i-1][1])+i-k;
}
}else{
dp[i]=dp[i-1]+i;
// dp[i][0]=inf;
}
update(1,i,dp[i]);
}
cout<<dp[n]<<endl;
return 0;
}

Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)的更多相关文章

  1. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)

    题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...

  3. Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)

    题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...

  4. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  5. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  6. [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】

    题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...

  7. Codeforces Round #674 (Div. 3) F. Number of Subsequences 题解(dp)

    题目链接 题目大意 给你一个长为d只包含字符'a','b','c','?' 的字符串,?可以变成a,b,c字符,假如有x个?字符,那么有\(3^x\)个字符串,求所有字符串种子序列包含多少个abc子序 ...

  8. Codeforces Round #359 (Div. 2) D. Kay and Snowflake 树DP

    D. Kay and Snowflake     After the piece of a devilish mirror hit the Kay's eye, he is no longer int ...

  9. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

随机推荐

  1. spark知识点_RDD

    来自官网的Spark Programming Guide,包括个人理解的东西. 这里有一个疑惑点,pyspark是否支持Python内置函数(list.tuple.dictionary相关操作)?思考 ...

  2. 如何限制电脑访问网址—使用Host限制访问网址

    如何限制电脑访问网址-使用Host限制访问网址 1. 打开C:\Windows\System32\drivers\etc 2. 打开hosts 3. 修改host内容,如下示例 127.0.0.1  ...

  3. 【SpringBoot1.x】SpringBoot1.x 消息

    SpringBoot1.x 消息 文章源码 概述 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力. 消息服务有两个重要概念,即消息代理(message broker)和目的地(des ...

  4. Jenkins Android APP 持续集成体系建设二—自动部署、执行测试任务,关联打包任务

    经过上一遍博客我们知道了怎么使用Jenkins自动打包,但打完包之后,我们还需要对新包进行回归测试,确定新包有没有问题,然后才能发布包,那么,话不多说,我们先来新建个自动化回归测试任务 新包自动化回归 ...

  5. 在项目中应该使用Boolean还是使用boolean?

    起因 在公司看代码时,看到了使用Boolean对象来完成业务逻辑判断的操作.和我的习惯不一致,于是引起了一些反思. boolean和Boolean的差别咱就不说了,我们仅探讨使用boolean与Boo ...

  6. 【Linux】CentOS7中yumbackend.py进程的结束方法

    环境: CentOS Linux release 7.3.1611 (Core) 今天启动这个不怎么用的机器,才启动,就发现后台的yum无法进行安装,持续报这个错误 Loaded plugins: f ...

  7. AQS之ReentrantReadWriteLock精讲分析上篇

    1.用法 1.1 定义一个安全的list集合 public class LockDemo { ArrayList<Integer> arrayList = new ArrayList< ...

  8. windows下部署Grafana +prometheus平台监控

      1.Prometheus简介 Prometheus基于Golang编写,编译后的软件包,不依赖于任何的第三方依赖.用户只需要下载对应平台包,解压并且添加基本的配置即可正常启Prometheus S ...

  9. Gulp4.0入门和实战

    gulp4.0入门和实战 我最近遇到需要优化web的性能的任务,然后就捣鼓了一些对资源文件优化压缩的方案.由于之前的项目中有使用到gulp,所以在需要处理的web项目中也优先使用这个技术. 先聊聊gu ...

  10. MySQL下载与安装教程

    一,下载篇 1,首先访问MySQL官网下载页,https://dev.mysql.com/downloads/mysql/ 如果是MAC系统,操作系统请选择macOS,Windows则选择Window ...