[2019] Nanjing tournament

hello 2019; meaning of the questions:

Given interval, you have to be in the query section 9102, and there is no sequence of 8012, the number of digits to be deleted.

I asked many times, so give our tree line.

This question has the original title;

We dp i represents the number of operations from state to state j i,

We define 0,1,2,3,4 to "", the state 2,20,201,2017. For each state is represented by a matrix:

The dp [1] [2] from the state 2 to 20;

For example, we look at one of the places {

if(s[l]=='2') p[cur].a[0][0]=1,p[cur].a[0][1]=0;

2 represents the moment met, then the 0 state "" to "" take 1

Why, what we "," encounter 2 strain of 2, but you want to keep, "" then we have to delete 2. Other empathy,

But then came the time to 6, 201-201 needs to be removed to maintain 6, takes the value of 1. 2016 because it can not appear, so it needs to be removed from a 2017 transfer value.

}

 

Code to the:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
struct node{
int a[5][5];
node operator+(const node &b)const//重载加法
{
node c;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
c.a[i][j]=inf;
for(int k=0;k<5;k++)
c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[k][j]);
}
}
return c;
}
}p[maxx<<2];
char s[maxx];
int n,m;

inline void pushup(int cur)
{
p[cur]=p[cur<<1]+p[cur<<1|1];
}
inline void build(int l,int r,int cur)
{
if(l==r)
{
for(int i=0;i<5;i++)for(int j=0;j<5;j++) p[cur].a[i][j]=(i==j)?0:inf;
if(s[l]=='2') p[cur].a[0][0]=1,p[cur].a[0][1]=0;
else if(s[l]=='0') p[cur].a[1][1]=1,p[cur].a[1][2]=0;
else if(s[l]=='1') p[cur].a[2][2]=1,p[cur].a[2][3]=0;
else if(s[l]=='7') p[cur].a[3][3]=1,p[cur].a[3][4]=0;
else if(s[l]=='6') p[cur].a[3][3]=1,p[cur].a[4][4]=1;
return ;
}
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
inline node query(int L,int R,int l,int r,int cur)
{
if(l<=L&&R<=r) return p[cur];
int mid=L+R>>1;
if(r<=mid) return query(L,mid,l,r,cur<<1);
else if(l>mid) return query(mid+1,R,l,r,cur<<1|1);
else return query(L,mid,l,mid,cur<<1)+query(mid+1,R,mid+1,r,cur<<1|1);
}
int main()
{
int l,r;
while(~scanf("%d%d",&n,&m))
{
scanf("%s",s+1);
build(1,n,1);
while(m--)
{
scanf("%d%d",&l,&r);
node ans=query(1,n,l,r,1);
if(ans.a[0][4]==inf) printf("-1\n");
else printf("%d\n",ans.a[0][4]);
}
}
return 0;
}

This question will Sasa water and then 2019 to 2019 to find the sequence reversed to become, not 2018, so he gives the range, you have to reverse it and then ask,

code show as below:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
struct node{
int a[5][5];
node operator+(const node &b)const
{
node c;
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
c.a[i][j]=inf;
for(int k=0;k<5;k++)
c.a[i][j]=min(c.a[i][j],a[i][k]+b.a[k][j]);
}
}
return c;
}
}p[maxx<<2];
char s[maxx],ss[maxx];
int n,m;

inline void pushup(int cur)
{
p[cur]=p[cur<<1]+p[cur<<1|1];
}
inline void build(int l,int r,int cur)
{
if(l==r)
{
for(int i=0;i<5;i++)for(int j=0;j<5;j++) p[cur].a[i][j]=(i==j)?0:inf;
if(s[l]=='2') p[cur].a[0][0]=1,p[cur].a[0][1]=0;
else if(s[l]=='0') p[cur].a[1][1]=1,p[cur].a[1][2]=0;
else if(s[l]=='1') p[cur].a[2][2]=1,p[cur].a[2][3]=0;
else if(s[l]=='9') p[cur].a[3][3]=1,p[cur].a[3][4]=0;
else if(s[l]=='8') p[cur].a[3][3]=1,p[cur].a[4][4]=1;
return ;
}
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
pushup(cur);
}
inline node query(int L,int R,int l,int r,int cur)
{
if(l<=L&&R<=r) return p[cur];
int mid=L+R>>1;
if(r<=mid) return query(L,mid,l,r,cur<<1);
else if(l>mid) return query(mid+1,R,l,r,cur<<1|1);
else return query(L,mid,l,mid,cur<<1)+query(mid+1,R,mid+1,r,cur<<1|1);
}
int main()
{
int l,r;
while(~scanf("%d%d",&n,&m))
{
scanf("%s",ss+1);
for(int i=1;i<=n;i++) s[i]=ss[n-i+1];
build(1,n,1);
while(m--)
{
scanf("%d%d",&l,&r);
node ans=query(1,n,n-r+1,n-l+1,1);
if(ans.a[0][4]==inf) printf("-1\n");
else printf("%d\n",ans.a[0][4]);
}
}
0 return;
}
// thank big brother to blog: https: //blog.csdn.net/starlet_kiss/article/details/100694910

Guess you like

Origin www.cnblogs.com/hgangang/p/11516056.html