当前位置:网站首页>2019 Nanchang Internet Competition Question C, Hello 2019

2019 Nanchang Internet Competition Question C, Hello 2019

2022-08-09 07:40:00 swust_fang

The meaning of the question: Find the minimum number of deletions that includes 9012, but does not include 8012

Solution idea: first invert the string, and maintain the matrix for the line segment tree according to the problem solving idea

We represent each interval of the line segment tree as a matrix, and the matrix mat[5][5] maintains the situation of the 2019 sequence~


First clarify the meaning of the matrix mat: we need to form a sequence of length 4

mat[0][1] represents the minimum cost required for "2"

mat[0][2] represents the minimum cost of "20"

mat[0][3] represents the minimum cost required by "201"

mat[0][4] means "2019" and there is no minimum cost required for "2018"


Encounter a '2', then we use this '2', mat[0][1]=0, if not mat[0][1]=1

When a '0' is encountered, then we use this '0', mat[1][2]=0, if not mat[1][1]=1

Encounter a '1', then we use this '1', mat[2][3]=0, if not mat[2][2]=1

Encounter a '9', then we use this '9', mat[3][4]=0, if not mat[3][3]=1

In 2019, we can choose to transfer or not transfer our numbers, but in the case of 8, whether it is 3 or 4, it will cost 1 to delete him~

Encounter an '8', then we don't need this '8', mat[3][3]=1, mat[4][4]=1

From the case of i->j, it is obvious that the optimal can be found by shifting i->k,k->j

dp transfer equation: ans.mat[i][j]=min(ans.mat[i][j],a.mat[i][k]+b.mat[k][j]);

If you want to merge, you can directly use the line segment tree interval to merge.

This question is really difficult. After the game, I found that more than 200 teams beat the tourist~(wu

//#pragma comment (linker, "/STACK:102400000,102400000")#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#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 pairtypedef 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;}

原网站

版权声明
本文为[swust_fang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090700344613.html