Codeforces Round #587 (Div. 3) F Wi-Fi(线段树+dp)

题意:给定一个字符串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)的更多相关文章
- 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 ...
- Codeforces Round #587 (Div. 3) F. Wi-Fi(单调队列优化DP)
题目:https://codeforces.com/contest/1216/problem/F 题意:一排有n个位置,我要让所有点都能联网,我有两种方式联网,第一种,我直接让当前点联网,花费为i,第 ...
- Codeforces Round #479 (Div. 3) F. Consecutive Subsequence (简单dp)
题目:https://codeforces.com/problemset/problem/977/F 题意:一个序列,求最长单调递增子序列,但是有一个要求是中间差值都是1 思路:dp,O(n)复杂度, ...
- 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 ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
- [Codeforces Round #296 div2 D] Clique Problem 【线段树+DP】
题目链接:CF - R296 - d2 - D 题目大意 一个特殊的图,一些数轴上的点,每个点有一个坐标 X,有一个权值 W,两点 (i, j) 之间有边当且仅当 |Xi - Xj| >= Wi ...
- Codeforces Round #674 (Div. 3) F. Number of Subsequences 题解(dp)
题目链接 题目大意 给你一个长为d只包含字符'a','b','c','?' 的字符串,?可以变成a,b,c字符,假如有x个?字符,那么有\(3^x\)个字符串,求所有字符串种子序列包含多少个abc子序 ...
- 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 ...
- Codeforces Round #546 (Div. 2) E 推公式 + 线段树
https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...
随机推荐
- 【C++】《C++ Primer 》第七章
第七章 类 一.定义抽象数据类型 类背后的基本思想:数据抽象(data abstraction)和封装(encapsulation). 数据抽象是一种依赖于接口(interface)和实现(imple ...
- LeetCode142 环形链表 II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...
- mysqlbinlog :unknown variable 'default-character-set=gbk'
mysqlbinlog :unknown variable 'default-character-set=gbk' 解决方法: 在命令行中加入--no-defaults开关,使用mysqlbinlo ...
- 彻底搞懂MySQL为什么要使用B+树索引
目录 MySQL的存储结构 表存储结构 B+树索引结构 B+树页节点结构 为什么要用B+树索引 二叉树 多叉树 B树 B+树 搞懂这个问题之前,我们首先来看一下,MySQL表的存储结构 MySQL的存 ...
- 【Oracle】创建用户配额总是不足的解决问题 quota
在oracle中,正常创建的用户是没有配额限制的,也就是默认的是unlimited on tablespace的,但是在有些时候,没有设置相关的配额,用户总是会报错用户配额严重不足,查看表空间,也有很 ...
- VBA调用数独求解器
我开发了一个用于求解数独的dll文件,只需要双击一下注册表文件,就可以在VBA中调用这个功能了.具体步骤如下: 下载:https://share.weiyun.com/5dpcNqx 找到ExcelS ...
- DC-DC变换器,24v转5v稳压芯片,3A输出电流
在24V输入中,比较合适的LDO可以选择:PW6206,输出电压3V,3.3V,5V 输入电压最高40V,功耗也低4uA左右,采用SOT23-3封装. PW6206系列是一个高精度,高输入电压低静态电 ...
- 干电池升压IC
1, 干电池升压IC 升压输出3V,3,3V,5V等3V-5V可调 2, 单节锂电池升压IC 升压输出4.2 ...
- Angular入门到精通系列教程(11)- 模块(NgModule),延迟加载模块
1. 摘要 2. NgModule举例.说明 3. Angular CLI生成模块 4. 延迟加载模块 5. 总结 环境: Angular CLI: 11.0.6 Angular: 11.0.7 No ...
- Redis 核心篇:唯快不破的秘密
天下武功,无坚不摧,唯快不破! 学习一个技术,通常只接触了零散的技术点,没有在脑海里建立一个完整的知识框架和架构体系,没有系统观.这样会很吃力,而且会出现一看好像自己会,过后就忘记,一脸懵逼. 跟着「 ...