题意:给定一个字符串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. linux + svn提交日志不能显示 日期一直都是1970-01-01

    网上很多都是说将svn安装目录下的svnserve.conf文件中的anon-access 设置为read,但是 经查阅并测试, 设置为: anon-access = none  是正确的,设置成 r ...

  2. 机器学习算法-PCA降维技术

    机器学习算法-PCA降维 一.引言 在实际的数据分析问题中我们遇到的问题通常有较高维数的特征,在进行实际的数据分析的时候,我们并不会将所有的特征都用于算法的训练,而是挑选出我们认为可能对目标有影响的特 ...

  3. 十二:SQL注入之简要注入

    SQL注入漏洞将是重点漏洞,分为数据库类型,提交方法,数据类型等方式.此类漏洞是WEB漏洞中的核心漏洞,学习如何的利用,挖掘,和修复是重要的. SQL注入的危害 SQL注入的原理 可控变量,带入数据库 ...

  4. 【Linux】1、命令行及命令参数

    命令行及命令参数 文章目录 命令行及命令参数 1.命令行提示符 2.命令和命令参数 简单的命令 date ls 命令参数 短参数(一个字母) 长参数(多个字母) 参数的值 其它参数 3.小结 4.参考 ...

  5. 【葵花宝典】kolla部署OpenStack-AllinOne

    1.关闭防火墙以及内核安全机制 systemctl stop firewalld systemctl disable firewalld ##永久性关闭 setenforce 0 sed -i 's/ ...

  6. WMIC 查看bios配置信息

    如何查看我们的主板上的BIOS信息呢?有办法,不用安装任何软件,只需要windows自带的命令提示符就行,哈哈 WMIC BIOS LIST FULL /FORMAT:VALUE 如果第一次使用wmi ...

  7. spring mvc + mybaties + mysql 完美整合cxf 实现webservice接口 (服务端、客户端)

    spring-3.1.2.cxf-3.1.3.mybaties.mysql 整合实现webservice需要的完整jar文件 地址:http://download.csdn.net/detail/xu ...

  8. tf

    第2章 Tensorflow keras实战 2-0 写在课程之前 课程代码的Tensorflow版本 大部分代码是tensorflow2.0的 课程以tf.kerasAPI为主,因而部分代码可以在t ...

  9. Spring Security,没有看起来那么复杂(附源码)

    权限管理是每个项目必备的功能,只是各自要求的复杂程度不同,简单的项目可能一个 Filter 或 Interceptor 就解决了,复杂一点的就可能会引入安全框架,如 Shiro, Spring Sec ...

  10. SuperUpdate.sh 一键更换Linux软件源脚本

    一.前言 有时候会遇到 Linux 的源更新速度非常的缓慢,特别是在国内使用默认的源,因为国内的网络环境,经常会出现无法更新,更新缓慢的情况.在这种情况下,更换一个更适合或者说更近,更快的软件源,会为 ...