Problem 1 : Inversions Organize
Code :
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
void solve()
{
int n;
cin>>n;
char mat[2*n+1][2*n+1];
for(int i=1;i<=2*n;i++)
{
string s;
cin>>s;
for(int j=1;j<=2*n;j++)
{
mat[i][j]=s[j-1];
}
}
int up=0,down=0,right=0,left=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=2*n;j++)
{
if(mat[i][j]=='I')
up++;
}
}
for(int i=n+1;i<=2*n;i++)
{
for(int j=1;j<=2*n;j++)
{
if(mat[i][j]=='I')
down++;
}
}
for(int i=1;i<=2*n;i++)
{
for(int j=1;j<=n;j++)
{
if(mat[i][j]=='I')
left++;
}
}
for(int i=1;i<=2*n;i++)
{
for(int j=n+1;j<=2*n;j++)
{
if(mat[i][j]=='I')
right++;
}
}
cout<<max(abs(left-right),abs(up-down))<<'\n';
}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
int T=1;
cin>>T;
int indx=1;
while(T--)
{
cout<<"Case #"<<indx<<": ";
solve();
indx++;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}