Official
B - Discord Editorial by en_translator
For all pairs \((x,y)\), check if any of \(M\) photos contains contiguous occurrence of \(x\) and \(y\).
Since we do not care about the order of \(x\) and \(y\), we can impose ourself a rule like \(x \lt y\), or first count pairs such that \(x \neq y\) and divide the count by two to avoid double-counts for \(x\lt y\) and \(y\lt x\). In sample code, the second outermost loop has a clever index initialization that realizes the rule where \(x \lt y\).
Sample code (C++)
#include <bits/stdc++.h>
using namespace std;
int main() {
int N,M;
cin>>N>>M;
vector a(M,vector<int>(N));
for(int i=0;i<M;i++){
for(int j=0;j<N;j++){
cin>>a[i][j];
}
}
int ans = 0;
for(int i=1;i<=N;i++){
for(int j=i+1;j<=N;j++){
bool flag = false;
for(int k=0;k<M;k++){
for(int l=0;l<N-1;l++){
if(a[k][l]==i&&a[k][l+1]==j)flag = true;
if(a[k][l]==j&&a[k][l+1]==i)flag = true;
}
}
if(!flag)ans++;
}
}
cout<<ans<<endl;
return 0;
}
posted:
last update: