传送门

题意:

给出一个长度为\(n\)的串,现在有\(q\)个询问,每个询问是一个区间\([l,r]\),要回答在区间\([l,r]\)中,最少需要删多少个数,满足区间中包含\(2017\)的子序列而不包含\(2016\)的子序列。

思路:

  • 先不考虑多个询问,那么这个问题区间\(dp\)可以解决,状态定义中要附加状态转移的代价。
  • 比如当前数字为\(7\),那么显然从状态\(201\)转移过来需要\(0\)的花费;但如果不要\(7\),那么从状态\(201\)到状态\(201\)则需要\(1\)的花费。
  • 同理,若数字为\(6\)时,若前面处于状态\(201\)或者\(2017\),显然此时的状态只能保持不能发生其它转移,但是保持也需要\(1\)的代价才行。
  • 以上我们定义的是将\(0,1,2,3,4\)分别表示状态\(\empty,2,20,201,2017\),对于每个数的状态转移都有确定的代价,这好像叫有限状态自动机?
  • 考虑多个询问,因为每个询问是多个区间的问题,所以我们可以想到用线段树来保存区间信息,也就是说直接把\(dp\)挂树上去就行了。(区间\(dp\)支持区间合并)

妙啊。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 200005; int n, q;
char s[N]; struct node{
int a[5][5];
node() { memset(a, INF, sizeof(a)); }
node operator + (const node &other) const {
node res;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 5; k++) {
res.a[i][j] = min(res.a[i][j], a[i][k] + other.a[k][j]);
}
}
}
return res;
}
}tr[N << 2]; void build(int o, int l, int r) {
if(l == r) {
tr[o] = node();
for(int i = 0; i < 5; i++) tr[o].a[i][i] = 0;
if(s[l] == '2') tr[o].a[0][1] = 0, tr[o].a[0][0] = 1;
if(s[l] == '0') tr[o].a[1][2] = 0, tr[o].a[1][1] = 1;
if(s[l] == '1') tr[o].a[2][3] = 0, tr[o].a[2][2] = 1;
if(s[l] == '7') tr[o].a[3][4] = 0, tr[o].a[3][3] = 1;
if(s[l] == '6') tr[o].a[3][3] = tr[o].a[4][4] = 1;
return;
}
int mid = (l + r) >> 1;
build(o << 1, l, mid); build(o << 1|1, mid + 1, r);
tr[o] = tr[o << 1] + tr[o << 1|1];
}
node query(int o, int l, int r, int L, int R) {
if(L <= l && r <= R) return tr[o];
int mid = (l + r) >> 1;
if(R <= mid) return query(o << 1, l, mid, L, R);
if(L > mid) return query(o << 1|1, mid + 1, r, L, R);
return query(o << 1, l, mid, L, R) + query(o << 1|1, mid + 1, r, L, R);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> q;
cin >> s + 1;
build(1, 1, n);
while(q--) {
int l, r; cin >> l >> r;
node ans = query(1, 1, n, l, r);
cout << (ans.a[0][4] > n ? -1 : ans.a[0][4]) << '\n';
}
return 0;
}

Codeforces Good Bye 2016 E. New Year and Old Subsequence的更多相关文章

  1. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  2. Codeforces Good Bye 2016 题解

    好久没有fst题了...比赛先A了前4题然后发现room里有人已经X完题了没办法只能去打E题,结果差一点点打完...然后C题fst掉了结果就掉rating 了...下面放题解 ### [A. New ...

  3. Codeforces Good Bye 2016 D 模拟搜索?

    给出烟花的爆炸方式和爆炸次数 问最后有多少个格子会被炸到 如果dfs的话会超时... 利用模拟每一层来搜索..? 思想就是一开始有一个爆炸点向上 然后模拟完第一段 会产生一个爆炸点 朝两个方向 就用v ...

  4. CodeForces Good Bye 2016

    A题,水题略过. B题,也水,但是想复杂了.只要运动超出[0,20000]的范围就算不可能了. C题,我自己的方法是解不等式,然后取最大的答案即可.代码如下: #include <stdio.h ...

  5. Codeforces Good Bye 2015 A. New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  6. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

  7. Good Bye 2016 - D

    题目链接:http://codeforces.com/contest/750/problem/D 题意:新年烟花爆炸后会往两端45°差分裂.分裂完后变成2部分,之后这2部分继续按这种规则分裂.现在给你 ...

  8. Good Bye 2016 - C

    题目链接:http://codeforces.com/contest/750/problem/C 题意:在CF中,每个人都有个Rank值. 当Rank>=1900时,为DIV1.Rank< ...

  9. Good Bye 2016 - B

    题目链接:http://codeforces.com/contest/750/problem/B 题意:地球的子午线长度为40000,两极点的距离为20000.现在你从北极出发,按照题目输入方式来走. ...

随机推荐

  1. c语言线程中传输多个参数

    前言:c语言中创建一条线程,但是需要传送多个参数给线程的话我们自然会想到通过传送数组或者结构体来实现,下面我们来看看如何在创建线程的时候传送结构体和数组. #include <stdio.h&g ...

  2. Web协议详解与抓包实战:HTTP1协议-如何用Chrome的Network面板分析HTTP报文(1)

    一.Chrome 抓包:Network 面板 1. Network 面板 • 控制器:控制面板的外观与功能 • 过滤器:过滤请求列表中显示的资源 • 按住 Command (Mac)或 Ctrl (W ...

  3. K8s 集群安装(一)

    01,集群环境 三个节点   master node1 node2 IP 192.168.0.81 192.168.0.82 192.168.0.83 环境 centos 7 centos 7 cen ...

  4. Tomcat启用HTTPS协议配置过程

    Article1较为简洁,Article2较为详细,测试可行. Article1 概念简介 Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问 ...

  5. 微信JSSDK 扫描二维码

    <?php require_once('wxjssdk.class.php'); $weixin = new class_weixin(); $signPackage = $weixin-> ...

  6. springboot kafka 消费者

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  7. What IS MPI

    一.MPI message passing interface A specification for the developers and users of message passing libr ...

  8. $scope里的$watch方法

    一.$watch的作用 监听model,如果model发生变化,则触发某些事情. 二.$watch的格式 $scope. $watch(watchFn,watchAction,deepWatch); ...

  9. 利用SQL计算两个地理坐标(经纬度)之间的地表距离

    两个地理坐标(经纬度)地表距离计算公式: 公式解释如下: Long1,Lat1表示A点经纬度,Long2,Lat2表示B点经纬度: a=Lat1–Lat2 为两点纬度之差,b=Long1-Long2为 ...

  10. mybatis + mysql 批量插入、删除、更新

    mybatis + mysql 批量插入.删除.更新 Student 表结构 批量插入 public int insertBatchStudent(List<Student> studen ...