Codeforces Round #519 by Botan Investments D
程序员文章站
2022-03-02 23:24:53
...
D. Mysterious Crime
题解:因为是个不同的数,所以先预处理出第一行每个数字的下一个数字是什么,然后去剩下的行去寻找满足,然后,这样最后找到这第一行的个数字在行中出现了次的数,然后考虑连续子串的情况,方案数就为(比如都出现了次,可行子序列为)。统计一下即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 100100;
struct node{
int nxt, cnt;
}loc[N];
int num[13][N];
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
#endif
int n,m;
cin>>n>>m;
for(int i = 1; i <= m; ++i) {
for(int j = 1; j <= n; ++j) {
scanf("%d",&num[i][j]);
}
}
for(int i = 1; i < n; ++i) {
loc[num[1][i]].nxt = num[1][i + 1];
loc[num[1][i]].cnt = 1;
}
for(int i = 2; i <= m; ++i) {
for(int j = 1; j < n; ++j) {
if(loc[num[i][j]].nxt == num[i][j + 1]) loc[num[i][j]].cnt++;
}
}
long long sum = 1, ans = 0;
for(int i = 1; i < n; ++i) {
if(loc[num[1][i]].cnt == m) sum++;
else ans += sum * (sum + 1) / 2, sum = 1;
}
if(sum > 0) ans += sum * (sum + 1) / 2;
cout<< ans << endl;
return 0;
}
推荐阅读
-
Codeforces Round #595 (Div. 3)D1D2 贪心 STL
-
Codeforces Round #656 (Div. 3)D. a-Good String(递归+dfs)
-
Codeforces Global Round 9(A~D题解)
-
Educational Codeforces Round 97 (Rated for Div. 2) D. Minimal Height Tree
-
Codeforces Round #665 (Div. 2) D. Maximum Distributed Tree(dfs)
-
构造思维+树形结构 Codeforces Round #612 (Div. 2) D题 Numbers on Tree
-
Codeforces Round #619 (Div. 2) D. Time to Run
-
Codeforces Round #674 D.Non-zero Segments【前缀和 | set】
-
Codeforces Round #583 (Div. 1 + Div. 2,) D. Treasure Island(dfs+思维)
-
Codeforces Round #656 (Div. 3) (C、D题)