1365C - Rotation Matching - CodeForces Solution


constructive algorithms data structures greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed 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 n;cin >> n;
   int a[n],b[n];
   map<int,int>indexa,indexb;
   for (int i = 0; i < n; i++)
   {
    cin >> a[i];
    indexa[a[i]]=i+1;
   }
   for (int i = 0; i < n; i++)
   {
    cin >> b[i];
    indexb[b[i]]=i+1;
   }
  int left=1,right=1;
  vector<int>diffleft,diffright;
  for (int i = 1; i <= n; i++)
  {
    int temp_sum=0;
    if(indexb[i]>indexa[i]){
       temp_sum+=(n-indexb[i]+indexa[i]);
       diffleft.push_back(temp_sum);
    }
    else
    {
      diffleft.push_back(indexa[i]-indexb[i]);
    } 
  }
  sort(diffleft.begin(),diffleft.end());
  int count=1;
  for (int i = 1; i < diffleft.size(); i++)
  {
    if (diffleft[i]!=diffleft[i-1])
    {
     left = max(left,count);
     count=1; 
    }
    else
    {
      count++;
    }
  }
  if(count!=1)left= max(left,count);
  for (int i = 1; i <= n; i++)
  {
    if (indexa[i]>indexb[i])
    {
      diffright.push_back(n-indexa[i]+indexb[i]);
    }
    else
    {
      diffright.push_back(indexb[i]-indexa[i]);
    }
  }
  sort(diffright.begin(),diffright.end());
  count=1;
  for (int i = 1; i < diffright.size(); i++)
  {
    if(diffright[i]!=diffright[i-1]){
     right= max(count,right);
     count=1;
    }
    else
    {
      count++;
    } 
  }
  if(count!=1)right = max(right,count);
  int ans = max(left,right);
  map<int,int>diffa,diffb;
  for (int i = 0; i < diffleft.size(); i++)
  {
    diffa[diffleft[i]]++;
  }
  for (int i = 0; i < diffright.size(); i++)
  {
    diffb[diffright[i]]++;
  }
  int  mini = n;
  for (int i = 0; i < diffleft.size(); i++)
  {
    if (diffa[diffleft[i]]==ans)
    {
      mini = min(mini,diffleft[i]);
      break;
    }
    
  }
  for (int i = 0; i < diffright.size(); i++)
  {
    if (diffb[diffright[i]]==ans)
    {
      mini = min(mini,diffright[i]);
      break;
    }
    
  }
 cout << ans << endl;

  
  return 0;  
}


Comments

Submit
0 Comments
More Questions

489A - SwapSort
932A - Palindromic Supersequence
433A - Kitahara Haruki's Gift
672A - Summer Camp
1277A - Happy Birthday Polycarp
577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1