Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)
第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对。。
然后就gun取看题解了, 用线段树维护dp的值, 然后区间合并求答案。 每个节点保存dp[ i ][ j ]表示, 把当前管理的区间删到
s{2017}中的 s[ i + 1 ] - s[ j - 1 ],最少删几个, 然后合并的时候5 ^ 3合并。
#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < ) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
struct info {
int a[][];
info() {
memset(a, inf, sizeof(a));
}
void go(char c) {
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
a[i][j] = i == j ? : inf;
if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
else if(c == '') a[][] = , a[][] = ;
}
};
info operator + (const info& x, const info& y) {
info z;
for(int i = ; i < ; i++)
for(int j = i; j < ; j++)
for(int k = i; k < ; k++)
chkmin(z.a[i][j], x.a[i][k] + y.a[k][j]);
return z;
}
info a[N << ];
void build(char* s, int l, int r, int rt) {
if(l == r) {
a[rt].go(s[l]);
return;
}
int mid = l + r >> ;
build(s, lson); build(s, rson);
a[rt] = a[rt << ] + a[rt << | ];
}
info query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) return a[rt];
int mid = l + r >> ;
if(R <= mid) return query(L, R, lson);
else if(L > mid) return query(L, R, rson);
else return query(L, R, lson) + query(L, R, rson);
} int n, q;
char s[N]; int main() {
scanf("%d%d", &n, &q);
scanf("%s", s + );
build(s, , n, );
while(q--) {
int L, R;
scanf("%d%d", &L, &R);
info ans = query(L, R, , n, );
printf("%d\n", ans.a[][] == inf ? - : ans.a[][]);
}
return ;
} /*
*/
Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)的更多相关文章
- Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- codeforces 629D D. Babaei and Birthday Cake (线段树+dp)
D. Babaei and Birthday Cake time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)
[题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
- [Codeforces 750E]New Year and Old Subsequence
Description 题库链接 给出一个长度为 \(n\) 的仅包含数字的字符串. \(q\) 次询问,每次询问该串 \([a,b]\) 段内删去几个数能够使其不含 \(2016\) 的子串,但存在 ...
- Codeforces 750E 线段树DP
题意:给你一个字符串,有两种操作:1:把某个位置的字符改变.2:询问l到r的子串最少需要删除多少个字符,使得这个子串含有2017子序列,并且没有2016子序列? 思路:线段树上DP,我们设状态0, 1 ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
随机推荐
- 关于base64转码解码
刚好涉及到记录一下 1.JS BASE64 解码和编码 js代码: /** * * Base64 encode / decode * * @author haitao.tu * @date 2010- ...
- [BZOJ 2242] [SDOI 2011] 计算器
Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...
- mongoDB 集合(表)操作
mongoDB 集合(表)操作 集合命名规则 使用 utf8 字符(通常不会起中文名字) 不能含有 "\0" 字符 不要以 system. 开头(否咋会覆盖系统集合开头) 不要和关 ...
- tomcat在windows及linux环境下安装
下载tomcat 下载地址: https://tomcat.apache.org/download-90.cgi 7,8,9的版本都可以下,这里下载最新版本 注意:Binary是编译好的,可以直接使用 ...
- NowCoder -- 牛客小白月赛10
A--勘测 推下公式 a[i] = a[i-1] + a[i-2] +2 #include<stdio.h> #include<string.h> ]; int main() ...
- TensorFlow深度学习,一篇文章就够了
http://blog.jobbole.com/105602/ 作者: 陈迪豪,就职小米科技,深度学习工程师,TensorFlow代码提交者. TensorFlow深度学习框架 Google不仅是大数 ...
- python 第一课 helloworld
#!/usr/bin/env python #-*-coding:utf-8-*- #以上是配置编写环境的开始 #第一行env表示运行当前环境变量内的python版本(2.x or 3.x) #第二行 ...
- Hibernate报错,关于配置的SessionFactory找不到问题
最近写项目使用hibernate默认的dtd,在启动项目时经常会出现这个问题,hibernate报错,配置factory的id找不到,找不到mapping配置文件, 不能读取配置的xml文件 Coul ...
- KNN算法的实现
K近邻(KNN)算法简介 KNN是通过测量不同特征值之间的距离进行分类.它的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别,其 ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...