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

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int inf = 0x3f3f3f3f;
  4. const double eps = 1e-6;
  5. const int N = 2e5+7;
  6. typedef long long ll;
  7. const ll mod = 998244353;
  8. using namespace std;
  9. ll dp[N];
  10. ll a[N],sum[N];
  11. struct tree{
  12. int l,r;
  13. ll v;
  14. int po;
  15. }t[N<<4];
  16. int nico=0;
  17. void build(int p,int l,int r){
  18. t[p].l=l; t[p].r=r; t[p].v=inf;
  19. if(l==r){
  20. t[p].po=l;
  21. return ;
  22. }
  23. int mid=(l+r)>>1;
  24. build(p<<1,l,mid);
  25. build(p<<1|1,mid+1,r);
  26. }
  27. void update(int p,int x,ll v){
  28. if(t[p].l==t[p].r){
  29. t[p].v=v;
  30. return ;
  31. }
  32. int mid=(t[p].l+t[p].r)>>1;
  33. if(x<=mid) update(p<<1,x,v);
  34. else update(p<<1|1,x,v);
  35. if(t[p<<1].v<t[p<<1|1].v){
  36. t[p].v=t[p<<1].v;
  37. t[p].po=t[p<<1].po;
  38. }else{
  39. t[p].v=t[p<<1|1].v;
  40. t[p].po=t[p<<1|1].po;
  41. }
  42. }
  43. tree query(int p,int l,int r){
  44. if(l<=t[p].l&&t[p].r<=r){
  45. return t[p];
  46. }
  47. int mid=(t[p].l+t[p].r)>>1;
  48. tree res1,res2;
  49. if(l>mid){
  50. return query(p<<1|1,l,r);
  51. }else if(r<=mid){
  52. return query(p<<1,l,r);
  53. }else{
  54. res1=query(p<<1,l,r);
  55. res2=query(p<<1|1,l,r);
  56. if(res1.v<res2.v){
  57. return res1;
  58. }else{
  59. return res2;
  60. }
  61. }
  62. }
  63. int main(){
  64. ios::sync_with_stdio(false);
  65. cin.tie(0); cout.tie(0);
  66. int n,k; cin>>n>>k;
  67. string s; cin>>s;
  68. build(1,0,n);
  69. update(1,0,0);
  70. for(int i=1;i<=n;i++){
  71. sum[i]=sum[i-1]+i;
  72. }
  73. int mi=inf;
  74. for(int i=n-1;i>=0;i--){
  75. if(s[i]=='1'&&i!=n-1){
  76. s[min(i+k,n-1)]='1';
  77. s[i]='0';
  78. if(i+k>=n-1){
  79. mi=min(mi,i)+1;
  80. }
  81. }
  82. }
  83. if(s[n-1]=='1'&&mi==inf){
  84. mi=n;
  85. }
  86. // cout<<s<<endl;
  87. for(int i=1;i<=n;i++){
  88. if(s[i-1]=='1'){
  89. if(i==n){
  90. tree res=query(1,max(mi-k-1,0),i-1);
  91. dp[i]=res.v+mi;
  92. // dp[i][0]=min(dp[i-1][0],dp[i-1][1])+mi;
  93. }else{
  94. tree res=query(1,max(i-2*k-1,0),i-1);
  95. dp[i]=res.v+i-k;
  96. // dp[i][0]=min(dp[i-1][0],dp[i-1][1])+i-k;
  97. }
  98. }else{
  99. dp[i]=dp[i-1]+i;
  100. // dp[i][0]=inf;
  101. }
  102. update(1,i,dp[i]);
  103. }
  104. cout<<dp[n]<<endl;
  105. return 0;
  106. }

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. Spring Boot -- 外部配置的属性使用

    Spring Boot允许使用propertities文件.yaml文件或者命令行参数作为外部配置. 命令行参数配置 Spring Boot可以基于jar包运行,打成jar包的程序可以直接通过下面的命 ...

  2. Debian9 升级至 Debian10

    前言 目前国内云服务商提供的镜像最新只有 9 , 本文讲解升级至 10 的方法 正文 查看当前版本 lsb_release -a No LSB modules are available. Distr ...

  3. LeetCode700 二叉搜索树中搜索

    给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 4 / \ 2 7 / ...

  4. 通用寄存器_MOV_ADD_SUB_AND_OR_NOT

    通用寄存器 MOV指令 注意:目标操作数与操作数宽度必须一样 MOV 目标操作数,源操作数 作用:拷贝源操作数到目标操作数 1.源操作数可以是立即数.通用寄存器.段寄存器.或者内存单元. 2.目标操作 ...

  5. 技术基础 | Cassandra RBAC助你打击“虚拟海盗”,让他们对数据“战利品”望而不得

    现如今,我们称虚拟世界里的海盗们为"黑客",他们所追寻的战利品就是在你数据库某处的数据.   而我们能够保证你的数据安全的工具之一,就是"Cassandra基于角色的访问 ...

  6. docker 删除和拉取镜像

    删除镜像 # docker rmi -f 镜像id # 删除指定镜像 docker rmi -f 25d5f6s564 # docker rmi -f 镜像id 镜像id # 删除多个镜像 docke ...

  7. show engine innodb status

    TRANSACTIONS------------Trx id counter 2003909(当前事务号)Purge done for trx's n:o < 2003905 (清理线程完成到了 ...

  8. [oracle] exp-00091

    产生原因: 在数据库的服务器端和客户端字符集不同的情况下,导出(dump)数据库表时,会产生这个错误.虽然产生这个错误,但好像对导入没有影响. 解决办法: 查看服务器端字符集: 打开SQLPLUS,执 ...

  9. Ice系列--基于IceGrid的部署方案

    前言 前一篇文章介绍了IceGrid的简单应用.这篇文章来介绍一下它的高端玩法-如何将模板,复制组,知名对象应用于部署方案及其作用. 基于模板的部署方案 之前介绍了xml格式的配置文件通过各种描述符如 ...

  10. 如何讲清楚 Java 面向对象的问题与知识?(类与对象,封装,继承,多态,接口,内部类...)

    写在最前面 这个项目是从20年末就立好的 flag,经过几年的学习,回过头再去看很多知识点又有新的理解.所以趁着找实习的准备,结合以前的学习储备,创建一个主要针对应届生和初学者的 Java 开源知识项 ...