CF750E New Year and Old Subsequence
讲道理好久没有做过题了..
题目大意
给出长度为$n$的只含数字的串,有$q$个询问,每次询问一段区间,问最少删去多少个数才能变成只含2017子序列而不含2016子序列
吉爸爸好强啊..
定义$a_{i,j}$表示该区间从第$i$位匹配不了第$j$位最少要删去的数字数
这个东西用一个线段树或者st表来维护,合并是类似于矩阵乘法的..
根据吉爸爸的优化,对于一个询问区间$[l,r]$
因为一定需要一个7,所以把它分成两段,就像这样
l...最后一个7...r
那么前面一段只需要求出从第一位但是匹配不了最后一位的方案数,再加上后面那一段6的个数就是答案了..
code
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int Maxn = 200010;
const int lg = 20;
const int inf = 0x7fffffff;
struct node {
int a[4][4];
void clear (){ memset ( a, 63, sizeof (a) ); }
}f[Maxn][lg], p;
int n, m;
char s[Maxn];
int bit[Maxn], pre[Maxn], sum[Maxn];
int _min ( int x, int y ){ return x < y ? x : y; }
void merge ( node &ret, node x, node y ){
ret.clear ();
int i, j, k;
for ( i = 0; i < 4; i ++ ) for ( j = 0; j < 4; j ++ ) for ( k = 0; k < 4; k ++ ) ret.a[i][j] = _min ( x.a[i][k]+y.a[k][j], ret.a[i][j] );
}
int getans ( int l, int r ){
int len = r-l+1;
p.clear ();
p.a[0][0] = p.a[1][1] = p.a[2][2] = p.a[3][3] = 0;
while ( len > 0 ){
merge ( p, p, f[l][bit[len]] );
l += (1<<bit[len]);
len -= (1<<bit[len]);
}
return p.a[0][3];
}
int main (){
int i, j, k;
scanf ( "%d%d", &n, &m );
scanf ( "%s", s+1 );
bit[1] = 0;
for ( i = 2; i <= n; i ++ ) bit[i] = bit[i>>1]+1;
for ( i = 1; i <= n; i ++ ){
if ( s[i] == '6' ) sum[i] = sum[i-1]+1; else sum[i] = sum[i-1];
if ( s[i] == '7' ) pre[i] = i; else pre[i] = pre[i-1];
}
for ( i = 1; i <= n; i ++ ){
f[i][0].clear ();
f[i][0].a[0][0] = f[i][0].a[1][1] = f[i][0].a[2][2] = f[i][0].a[3][3] = 0;
if ( s[i] == '2' ) f[i][0].a[0][1] = 0, f[i][0].a[0][0] = 1;
if ( s[i] == '0' ) f[i][0].a[1][2] = 0, f[i][0].a[1][1] = 1;
if ( s[i] == '1' ) f[i][0].a[2][3] = 0, f[i][0].a[2][2] = 1;
if ( s[i] == '6' ) f[i][0].a[3][3] = 1;
}
for ( i = n; i >= 1; i -- ){
for ( j = 1; j <= 18 && i+(1<<(j-1)) <= n; j ++ ){
merge ( f[i][j], f[i][j-1], f[i+(1<<(j-1))][j-1] );
}
}
for ( i = 1; i <= m; i ++ ){
int l, r;
scanf ( "%d%d", &l, &r );
k = pre[r];
int ans = getans ( l, k-1 );
if ( ans > n ) printf ( "-1\n" );
else printf ( "%d\n", ans+sum[r]-sum[k] );
}
return 0;
}
哎好久没打都有点生疏了.. 加油吧..
CF750E New Year and Old Subsequence的更多相关文章
- 2021record
2021-10-14 P2577 [ZJOI2004]午餐 2021-10-13 CF815C Karen and Supermarket(小小紫题,可笑可笑) P6748 『MdOI R3』Fall ...
- DP 优化方法大杂烩 & 做题记录 I.
标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...
- 【CF750E】New Year and Old Subsequence
题目大意:给定一个长度为 N 的字符串,定义一个字串是"好的",当且仅当字串中含有一个 "2017" 的子序列,且不含有 "2016" 的子 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
随机推荐
- JS数组求最大值和最小值
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- static修饰符
static修饰符表示静态的,可修饰字段.方法.内部类,其修饰的成员属于类,也就是说static修饰的资源属于类级别,而不是对象级别. static的正真作用:用来区别字段,方法,内部类,初始化代码块 ...
- [codevs1743]反转卡片
[codevs1743]反转卡片 试题描述 [dzy493941464|yywyzdzr原创] 小A将N张卡片整齐地排成一排,其中每张卡片上写了1~N的一个整数,每张卡片上的数各不相同. 比如下图是N ...
- 腾讯开放平台web第三方登录获取信息类(包含签名)
不清楚具体参数的可以先看下第三方登录的文档: class QQ { //$appid 你的appid //$openid 获取到的唯一的用户openid //$openkey 获取到的openkey ...
- 对偶SVM
1.对偶问题的推导 为什么要求解对偶问题?一是对偶问题往往更容易求解,二是可以自然的引入核函数. 1.1 用拉格朗日函数将原问题转化为"无约束"等价问题 原问题是: 写出它的拉格朗 ...
- Struts的jar说明
- JavaBean的用法
JavaBean是一个可重复使用的软件组件,是用Java语言编写的.遵循一定标准的类. JavaBean是Java Web的重要组件,它封装了数据和操作的功能类,供JSP和Servlet调用,完成数据 ...
- Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解
Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...
- oracle11g interval(numtoyminterval())自动创建表分区
Oracle11g通过间隔分区实现按月创建表分区 在项目数据库设计过程中由于单表的数据量非常庞大,需要对表进行分区处理.由于表中的数据是历史交易,故按月分区,提升查询和管理. 由于之前对于表分区了解不 ...
- 解决springmvc报No converter found for return value of type: class java.util.ArrayList问题
一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...