1902C - Insert and Equalize - CodeForces Solution


brute force constructive algorithms greedy math number theory shortest paths

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back
int main()
{
int tc;
cin>>tc;
while(tc--){
  ll n;cin>>n;
  vll v(n);
  ll sum=0;
  f(i,0,n){
    cin>>v[i];    
  }
  if(n==1){
    cout<<"1\n";
    continue;
  }
  sort(v.begin(),v.end());
  ll gcd=0,ans=0;
  for(ll i=0;i<n-1;++i){
    gcd=__gcd(gcd,v[n-1]-v[i]);
  }
  for(ll i=0;i<n;++i){
    sum+=(v[n-1]-v[i])/gcd;
  }
  ans=v[n-1]-gcd;
  ll cnt=1;
  for(int i=n-2;i>=0;--i){
    if(ans!=v[i]){
      break;
    }
      cnt++;
      ans-=gcd;
  }
  cout<<sum+cnt<<"\n";
}
return 0;
}


Comments

Submit
0 Comments
More Questions

1633C - Kill the Monster
1611A - Make Even
1030B - Vasya and Cornfield
1631A - Min Max Swap
1296B - Food Buying
133A - HQ9+
1650D - Twist the Permutation
1209A - Paint the Numbers
1234A - Equalize Prices Again
1613A - Long Comparison
1624B - Make AP
660B - Seating On Bus
405A - Gravity Flip
499B - Lecture
709A - Juicer
1358C - Celex Update
1466B - Last minute enhancements
450B - Jzzhu and Sequences
1582C - Grandma Capa Knits a Scarf
492A - Vanya and Cubes
217A - Ice Skating
270A - Fancy Fence
181A - Series of Crimes
1638A - Reverse
1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness