Quotations
First of all, please attach the quotation of Mr. Deng. If there is anything you haven't collected, please let me know.
- "Competition in the same field"
- "The advantage is in me"
- I'm a fan of coconut juice. I'm good at typesetting.
- Don't look at \ (\ tt steam \), it's boring.
stars
Title Description
A star can be abstracted as a whole point in \ (k \) dimensional space. It is wonderful to call a set \ (s \) composed of several stars. If and only if there is a whole point \ (P \) in \ (k \) dimensional space, each star in \ (P \) and \ (s \) has at least one-dimensional coordinates.
There is A sequence of stars \ (A \) with A length of \ (n \). Please find the sum of the numbers of all qimiaoji intervals.
\(1\leq n\leq 10^5,1\leq k\leq 5\)
solution
First, consider how to determine that the sequence \ (s \) is wonderful. Because \ (k \) is very small, we can directly use the enumeration method. Since each point needs to be covered, we can consider enumerating how to solve the first point of the sequence \ (s \), which is actually the location of enumerating \ (P \) to solve it, and then push back to see how many points have been solved. When encountering the next point that cannot be solved, we can enumerate a location of \ (P \).
It is not difficult to find that the legitimacy can be determined only by enumerating all permutations with length \ (k \), but the violent determination is still not feasible. We consider using \ (dp \) to optimize this process. Because the permutation is very small, we can directly plug it into the state. Let \ (dp[i][s] \) represent the furthest extension distance of the last \ (I \) points arranged by position (where the single element of \ (s \) is vividly called a brocade bag by Mr. Deng), and consider how to transfer.
Writing transfer requires strong observation ability. Here, Mr. Deng observed the similarity between the problems. We consider \ (dp[i][s] \) and \ (dp[i+1][s'] \) (where \ (s' \) represents the position arrangement of \ (s \) after removing the first brocade bag, and set it to \ (x_0 \). Only for the first position of \ (dp[i+1][s'] \) where brocade bags need to be added but can be solved by \ (x_0 \), there is no need to add brocade bags. Then we can let \ (x_0 \) solve it, so we insert \ (x_0 \) into the next position of the current last brocade bag to get the equivalent subproblem of \ (i+1 \), that is, the influence on the future will be passed on equivalently.
Implementation details: \ (dp[i][s] \) is opened into a \ (k \) dimensional structure, and the farthest extension distance obtained by using the first \ (I \) brocade bag is recorded in each dimension.
summary
The key to finding sub problems: observe the similarity between problems.
If an element has a long-term impact on the future, but only a small step transfer can be considered, then finding the equivalent subproblem of the current problem can pass on the impact and complete the transfer.
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; const int M = 100005; int read() { int x=0,f=1;char c; while((c=getchar())<'0' || c>'9') {if(c=='-') f=-1;} while(c>='0' && c<='9') {x=(x<<3)+(x<<1)+(c^48);c=getchar();} return x*f; } int n,m,cnt,a[M][5],b[M];long long ans=1; struct node { int p[5]; int &operator[](int x) {return p[x];} int Hash() { int hs=0; for(int i=0;i<m;i++) hs=hs*5+p[i]; return hs; } }w,dp[2][125]; signed main() { n=read();m=read(); for(int i=1;i<=n;i++) for(int j=0;j<m;j++) a[i][j]=read(); for(int i=0;i<m;i++) w.p[i]=i; do {b[w.Hash()]=++cnt;} while(next_permutation(w.p,w.p+m)); for(int i=0;i<=120;i++) for(int j=0;j<m;j++) dp[n&1][i][j]=n+1; for(int i=n-1;i>=1;i--) { int mx=0,o=(i+1)&1;cnt=0; for(int j=0;j<m;j++) w.p[j]=j; do { cnt++; if(a[i][w[0]]==a[i+1][w[0]]) { dp[i&1][cnt]=dp[o][cnt]; mx=max(mx,dp[o][cnt][m-1]); continue; } node t;int p=m; for(int j=0;j<m;j++) t[j]=w[(j+1)%m]; node nw=dp[o][b[t.Hash()]]; for(int j=0;j<m-1;j++) { if(a[i][w[0]]==a[nw[j]][w[0]]) { for(int k=m-1;k>=j+1;k--) t[k]=t[k-1]; t[j+1]=w[0];p=j+1;break; } } dp[i&1][cnt]=dp[o][b[t.Hash()]]; for(int j=p-1;j;j--) dp[i&1][cnt][j]=dp[i&1][cnt][j-1]; dp[i&1][cnt][0]=i+1; mx=max(mx,dp[i&1][cnt][m-1]); }while(next_permutation(w.p,w.p+m)); ans+=mx-i; } printf("%lld\n",ans); }