当前位置:网站首页>2019南昌网络赛 C题,Hello 2019
2019南昌网络赛 C题,Hello 2019
2022-08-09 07:01:00 【swust_fang】
题意:求包含9012,但是不包含8012的最小删除次数
解题思路:首先将字符串反转,按照题解思路为线段树维护矩阵即可
我们将线段树的每个区间用矩阵表示,矩阵mat[5][5]维护2019这个序列的情况~
首先明确矩阵mat表示的意义:我们需要构成一个长度为4的序列
mat[0][1]表示"2"所需要的最小花费值
mat[0][2]表示"20"所需要的最小花费值
mat[0][3]表示"201"所需要的最小花费值
mat[0][4]表示"2019"并且没有"2018"所需要的最小花费值
遇到一个'2',那么我们用到这个'2',mat[0][1]=0,如果不用mat[0][1]=1
遇到一个'0',那么我们用到这个'0',mat[1][2]=0,如果不用mat[1][1]=1
遇到一个'1',那么我们用到这个'1',mat[2][3]=0,如果不用mat[2][2]=1
遇到一个'9',那么我们用到这个'9',mat[3][4]=0,如果不用mat[3][3]=1
2019我们的数字我们都可以选择转移或者不转移,但是遇到8无论是3还是4的情况都要花费1来删除他~
遇到一个'8',那么我们不用到这个'8',mat[3][3]=1,mat[4][4]=1
从i->j的情况明显可以通过i->k,k->j来转移来找到最优
dp转移方程:ans.mat[i][j]=min(ans.mat[i][j],a.mat[i][k]+b.mat[k][j]);
合并的话直接用线段树区间合并就可以了
这题着实好难,比赛后发现,200多队暴打tourist~(wu
//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#include<bitset>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
#define pii make_pair
#define pr pair<int,int>
typedef unsigned long long ull;
typedef long long ll;
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-6;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int maxn=2e5+5;
int n,q;
string s;
struct matrix
{
int mat[5][5];
matrix() {memset(mat,0,sizeof(mat));}
}tree[maxn<<2];
matrix mul(matrix a,matrix b)
{
matrix ans;
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
{
ans.mat[i][j]=inff;
for(int k=0;k<5;k++)
ans.mat[i][j]=min(ans.mat[i][j],a.mat[i][k]+b.mat[k][j]);
}
return ans;
}
void build(int tr,int l,int r)
{
if(l==r)
{
for(int i=0;i<=4;i++)
for(int j=0;j<=4;j++)
if(i==j) tree[tr].mat[i][j]=0;
else tree[tr].mat[i][j]=inff;
switch(s[l])
{
case '2':tree[tr].mat[0][1]=0,tree[tr].mat[0][0]=1;break;
case '0':tree[tr].mat[1][2]=0,tree[tr].mat[1][1]=1;break;
case '1':tree[tr].mat[2][3]=0,tree[tr].mat[2][2]=1;break;
case '9':tree[tr].mat[3][4]=0,tree[tr].mat[3][3]=1;break;
case '8':tree[tr].mat[3][3]=1,tree[tr].mat[4][4]=1;break;
}
return;
}
int mid=half;
build(tr<<1,l,mid),build(tr<<1|1,mid+1,r);
tree[tr]=mul(tree[tr<<1],tree[tr<<1|1]);
}
matrix query(int i,int l,int r,int ql,int qr)
{
if(ql<=l&&qr>=r) return tree[i];
int mid=half;
if(qr<=mid) return query(Lson,ql,qr);
else if(ql>=mid+1) return query(Rson,ql,qr);
else return mul(query(Lson,ql,qr),query(Rson,ql,qr));
}
int main()
{
cin>>n>>q;
cin>>s;
reverse(s.begin(),s.end());
s=" "+s;
build(1,1,n);
int l,r;
while(q--)
{
scanf("%d %d",&l,&r);
l=n-l+1,r=n-r+1;
swap(l,r);
matrix ans=query(1,1,n,l,r);
if(ans.mat[0][4]==inff) puts("-1");
else printf("%d\n",ans.mat[0][4]);
}
return 0;
}
边栏推荐
- The water problem of leetcode
- P7阿里面试题2020.07 之滑动窗算法(阿里云面试)
- MySQL高级特性之分布式(XA)事务的介绍
- 字节跳动面试题之镜像二叉树2020
- APP product source data interface (taobao, jingdong/spelling/suning/trill platform details a lot data analysis interface) code and docking tutorial
- Distributed id generator implementation
- Fragments
- SIGINT, SIGKILL, SIGTERM signal difference, summary of various signals
- Reverse Engineering
- XILINX K7 FPGA+RK3399 PCIE驱动调试
猜你喜欢
INSTALL_RPATH and BUILD_RPATH problem in CMake
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
字节跳动面试题之镜像二叉树2020
XILINX K7 FPGA+RK3399 PCIE驱动调试
Variable used in lambda expression should be final or effectively final报错解决方案
Tkinter可以选择的颜色
力扣第 305 场周赛复盘
db.sqlite3 has no "as Data Source" workaround
unity第一课
Leetcode 70 stairs issues (Fibonacci number)
随机推荐
我入职阿里后,才知道原来简历这么写
dp学习笔记
学习小笔记---机器学习
sklearn数据预处理
The singleton pattern
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
Service
Rsync常见错误
Explain the wait() function and waitpid() function in C language in detail
MongDb query method
The water problem of leetcode
codeforces Valera and Elections (这思维题是做不明白了)
【nuxt】服务器部署步骤
Distributed id generator implementation
Zero shift of leetcode
顺序表删除所有值为e的元素
字节跳动笔试题2020 (抖音电商)
ByteDance Written Exam 2020 (Douyin E-commerce)
常用测试用例设计方法之正交实验法详解
多米诺骨牌