codeforces 750E New Year and Old Subsequence(线段树+矩阵建模)
2017-01-17 16:01
225 查看
750E New
Year and Old Subsequence
题意:s[l~r]减少最少的字符数,使得得到的字符串含有2017子序列,不含2016子序列。一共q次查询。
题解:
个人感觉矩阵建模的题目都有一种状态转移的思想。
本题的状态是:
0:初始状态
1:2
2:20
3:201
4:2017
mat[i][j]表示状态i转移到j的最小代价
Year and Old Subsequence
题意:s[l~r]减少最少的字符数,使得得到的字符串含有2017子序列,不含2016子序列。一共q次查询。
题解:
个人感觉矩阵建模的题目都有一种状态转移的思想。
本题的状态是:
0:初始状态
1:2
2:20
3:201
4:2017
mat[i][j]表示状态i转移到j的最小代价
#include<cstdio> #include<algorithm> using namespace std; const int N=200005; char s ; struct Node { int mat[5][5]; Node operator + (Node x) { Node ans; for(int i=0;i<5;++i) { for(int j=0;j<5;++j) { ans.mat[i][j]=N; for(int k=0;k<5;++k) { ans.mat[i][j]=min(ans.mat[i][j],mat[i][k]+x.mat[k][j]); } } } return ans; } }a[N<<2]; void build(int l,int r,int x) { if(l==r) { for(int i=0;i<5;++i) { for(int j=0;j<5;++j) { a[x].mat[i][j]=(i==j)?0:N; } } if(s[l]=='2') a[x].mat[0][1]=0,a[x].mat[0][0]=1; if(s[l]=='0') a[x].mat[1][2]=0,a[x].mat[1][1]=1; if(s[l]=='1') a[x].mat[2][3]=0,a[x].mat[2][2]=1; if(s[l]=='7') a[x].mat[3][4]=0,a[x].mat[3][3]=1; if(s[l]=='6') a[x].mat[3][3]=1,a[x].mat[4][4]=1; return ; } int m=l+r>>1; build(l,m,x<<1);build(m+1,r,x<<1|1); a[x]=a[x<<1]+a[x<<1|1]; } Node query(int l,int r,int L,int R,int x) { if(L<=l&&r<=R) return a[x]; int m=l+r>>1; if(m<L) return query(m+1,r,L,R,x<<1|1); if(m>=R) return query(l,m,L,R,x<<1); return query(l,m,L,R,x<<1)+query(m+1,r,L,R,x<<1|1); } int main() { int n,q; while(~scanf("%d%d%s",&n,&q,s+1)) { build(1,n,1); while(q--) { int l,r; scanf("%d%d",&l,&r); int ans=query(1,n,l,r,1).mat[0][4]; if(ans==N) ans=-1; printf("%d\n",ans); } } return 0; }
相关文章推荐
- codeforces 227/B
- Codeforces 235E Number Challenge (神定理+莫比乌斯反演)
- CodeForces 609C Load Balancing
- codeforces 725D Conteset Balloons 贪心
- codeforces 622C
- Codeforces 1B. Spreadsheets
- CodeForces 407B Long Path
- codeforces 27E E. Number With The Given Amount Of Divisors(dp+数论)
- CodeForces 660F Bear and Bowling 4
- Codeforces-697B Barnicle(模拟)
- [随机 Hash] Codeforces 799F Round #413 F. Beautiful fountains rows
- Codeforces 869 E. The Untended Antiquity 二维树状数组
- Codeforces 392B Tower of Hanoi(DP)
- CodeForces 443B Kolya and Tandem Repeat
- CodeForces-672C-Recycling Bottles
- codeforces 730C (二分)
- Codeforces 810B Summer sell-off 题解
- codeforces contest 551 problem E(分块)
- codeforces 230D Planets 最短路
- CodeForces 12C Fruits