Reversi
There are N stones arranged in a row. The i-th stone from the left is painted in the color Ci.
Snuke will perform the following operation zero or more times:
Choose two stones painted in the same color. Repaint all the stones between them, with the color of the chosen stones.
Find the number of possible final sequences of colors of the stones, modulo 109+7.
Constraints
·1≤N≤2×105
·1≤Ci≤2×105(1≤i≤N)
·All values in input are integers.
输入
Input is given from Standard Input in the following format:
N
C1
:
CN
输出
Print the number of possible final sequences of colors of the stones, modulo 109+7.
样例输入
5
1
2
1
2
2
样例输出
3
提示
We can make three sequences of colors of stones, as follows:
·(1,2,1,2,2), by doing nothing.
·(1,1,1,2,2), by choosing the first and third stones to perform the operation.
·(1,2,2,2,2), by choosing the second and fourth stones to perform the operation.
ps:可以发现,对于任意操作,如果两次操作区间相交,那么只有一次操作有用。颜色都相同,则最后这两段区间的并全是这个颜色。
如果区间包含区间,则被含的无用。因为多次操作。最后全是大区间的颜色。
如果不相交,则都有用。
所以最终,求不相交的区间。先把重复颜色去重。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
const int maxn = 2e5+10;
int c[maxn];
int a[maxn];
long long d[maxn],dd[maxn];
int main(int argc, char *argv[])
{
std::ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>c[i];
}
int m=1;
a[1]=c[1];
for(int i=2;i<=n;i++){
if(c[i]!=c[i-1]) a[++m]=c[i];
}
dd[a[1]]=1;//原始序列初始为1
for(int i=1;i<=m;i++){
d[i]=dd[a[i]];
dd[a[i+1]]=(dd[a[i+1]]+d[i])%mod;//累加
}
cout<<d[m]<<endl;
return 0;
}
8032

被折叠的 条评论
为什么被折叠?



