CF contest 1216 Div3. F
题目链接:Click here
Solution:
看起来是贪心,其实不然。。。
我们定义\(f[i]\)表示仅覆盖\(1\sim i\)所需要的最小代价,那么对\(i\)为0的点来说,易得\(f[i]=min(f[i],f[i-1]+i)\)
考虑当\(i\)为1时怎么办,当\(i\)为1时,根据定义,我们不转移\(i\)这个位置的值,而转移\(i+k\)这个位置的值
很显然,只要\(1 \sim p(i-k\le p\le i+k-1)\)已被覆盖,那么再选\(i\),\(1\sim i+k\)就能够被覆盖
则我们用线段树维护区间\(f\)最小值,每次转移找最小值转移即可。最后注意判断边界情况。
Code:
#include<bits/stdc++.h>
#define ls q<<1
#define rs q<<1|1
#define int long long
using namespace std;
const int N=2e5+1;
const int maxn=1e15;
char s[N];
int n,k,f[N],mn[N<<2];
int min(int a,int b){return b<a?b:a;}
int max(int a,int b){return b<a?a:b;}
void update(int q){mn[q]=min(mn[ls],mn[rs]);}
void ins(int q,int l,int r,int x,int v){
if(l==r) return mn[q]=v,void();
int mid=l+r>>1;
if(mid>=x) ins(ls,l,mid,x,v);
else ins(rs,mid+1,r,x,v);
update(q);
}
int query(int q,int l,int r,int L,int R){
if(R<L) return 1e18;
if(l>=L&&r<=R) return mn[q];
int mid=l+r>>1,re=maxn;
if(mid>=L) re=min(re,query(ls,l,mid,L,R));
if(mid<R) re=min(re,query(rs,mid+1,r,L,R));
return re;
}
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
signed main(){
n=read(),k=read();
scanf("%s",s+1);
memset(mn,127,sizeof(mn));
memset(f,127,sizeof(f));f[0]=0;
for(register int i=1;i<=n;i++){
if(s[i]=='1'){
int p=min(n,i+k);
int v=query(1,1,n,max(1,i-k-1),p-1);
if(i-k-1<=0) f[p]=min(f[p],i);
f[p]=min(f[p],v+i);ins(1,1,n,p,f[p]);
}else f[i]=min(f[i],f[i-1]+i),ins(1,1,n,i,f[i]);
}
printf("%lld\n",f[n]);
return 0;
}
CF contest 1216 Div3. F的更多相关文章
- [cf contest 893(edu round 33)] F - Subtree Minimum Query
[cf contest 893(edu round 33)] F - Subtree Minimum Query time limit per test 6 seconds memory limit ...
- CF #552(div3)F 背包问题
题目链接:http://codeforces.com/contest/1154/problem/F 题意:一个商店有n个物品,每个物品只能买一次,同时有m种优惠,即一次买够x件后,这x件中最便宜的k件 ...
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem F
Problem F Funny Car Racing There is a funny car racing in a city with n junctions and m directed roa ...
- 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...
- 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest A E F G H I K M
// 深夜补水题,清早(雾)写水文 A. Automatic Door 题意 \(n(n\leq 1e9)\)个\(employee\)和\(m(m\leq 1e5)\)个\(client\)要进门, ...
- [题解向] CF#536Div2の题解 E&F
\(0x01~~Preface\) \(emmm\)这次CF本身打的很顺畅,但是居然unrated了--咕咕咕咕 这是头一次CF有比赛我全部题目都做了--可喜可贺可喜可贺233 简单总结一下前面四道题 ...
- The 2019 China Collegiate Programming Contest Harbin Site F. Fixing Banners
链接: https://codeforces.com/gym/102394/problem/F 题意: Harbin, whose name was originally a Manchu word ...
- [AtCoder] NIKKEI Programming Contest 2019 (暂缺F)
[AtCoder] NIKKEI Programming Contest 2019 本来看见这一场的排名的画风比较正常就来补一下题,但是完全没有发现后两题的AC人数远少于我补的上一份AtCoder ...
随机推荐
- dual Oracle兼容
CREATE OR REPLACE VIEW dual ASSELECT NULL::"unknown"WHERE 1 = 1;
- Charlie's Change POJ - 1787
Time limit 1000 ms Memory limit 30000 kB description Charlie is a driver of Advanced Cargo Movement, ...
- FFmpeg4.0笔记:封装ffmpeg的解码功能类CDecode
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CDecode.h /********************** ...
- 搞懂Redis复制原理
前言 与大多数db一样,Redis也提供了复制机制,以满足故障恢复和负载均衡等需求.复制也是Redis高可用的基础,哨兵和集群都是建立在复制基础上实现高可用的.复制不仅提高了整个系统的容错能力,还可以 ...
- maven的配置以及使用
1.下载并配置 下载之后解压,并配置系统环境变量(网上的方法很多),配置maven的环境变量之前确保java的环境变量已经配置成功. 2.eclipse安装maven插件 eclipse安装maven ...
- Java中的Switch....case语句:
一.格式: switch(表达式){ case 常量表达式1: 语句1; case 常量表达式2: 语句2; … case 常量表达式n: 语句n; default: ...
- ubuntu16.04 Installing PHP 7.2
//install sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install php7.2 //C ...
- hiper、sitespeed性能工具
https://github.com/pod4g/hiper hiper: A statistical analysis tool for performance testing https: ...
- 安卓端调用h5界面js方法和ios端调用h5界面js方法
备注:本人为h5开发人员,不懂安卓和ios,这是开发小伙伴对接联调的主代码. 1.iOS端调用h5界面js方法: 2.安卓端调用h5界面js方法: @Override protect ...
- 外网访问VMware(Centos7.0,NAT模式)搭建的web服务器应用
首先参考 https://www.cnblogs.com/studyhard-cq/p/11551755.html 设置好NAT模式,能访问公网. 1.打开VMware,点击左上角编辑 ...