1007A - Reorder the Array - CodeForces Solution


combinatorics data structures math sortings two pointers *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
/*----------------------------------------------------------------------*/
/*||||    ||| ||||||||||| ||||||||||| |||||||||||  |||||||||  |||      |||
  |||||   |||     |||         |||         |||      |||        |||      |||
  ||||||| |||     |||         |||         |||      |||        |||      |||
  ||| |||||||     |||         |||         |||      |||||||||  ||||||||||||
  |||   |||||     |||         |||         |||            |||  |||      |||
  |||    ||||     |||         |||         |||            |||  |||      |||
  |||    |||| |||||||||||     |||     |||||||||||  |||||||||  |||      |||*/
/*--------------------------Defiination----------------------------------*/
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<long long> vl;
typedef vector<vl> vvl;
/*--------------------------------values----------------------------------*/
double eps = 1e-9;
int inf = 1000000005;
long long infl = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
/*--------------------------------Driver code-------------------------------*/
#define Author_NitishBharat \
  int t;                    \
  cin >> t;                 \
  while (t--)
#define _coding_is_fun_             \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input_.txt", "r", stdin);
// freopen("output_.txt", "w", stdout);
// #endif
/*--------------------------------function----------------------------------*/

bool odd(int a)
{
  if (a % 2)
    return true;
  else
    return false;
}

#define N 1000000007

long long exponentiation(long long base,
                         long long exp)
{
  if (exp == 0)
    return 1;

  if (exp == 1)
    return base % N;

  long long t = exponentiation(base, exp / 2);
  t = (t * t) % N;

  if (exp % 2 == 0)
    return t;
  else
    return ((base % N) * t) % N;
}
/*--------------------------------macros------------------------------*/
#define iin(n) \
  int n;       \
  cin >> n;
#define out(n) cout << n << endl;
#define outc(n) cout << n << " ";
#define sin(s) \
  string s;    \
  cin >> s;
#define vi(v, n) vector<int> v(n);
#define vin(v, n)   \
  vector<int> v(n); \
  rep(i, n) cin >> v[i];
#define vout(v, n) rep(i, n) outc(v[i]) cout << endl;
#define aout(a, n) rep(i, n) outc(a[i]) cout << endl;
/*---------------------------------------------------------------------*/
#define mapin(m, v) \
  for (auto i : v)  \
  {                 \
    m[i]++;         \
  }                 \
/*---------------------------------------------------------------------*/
#define mapout(m)                \
  for (auto i : m)               \
  {                              \
    outc(i.first) outc(i.second) \
  }                              \
  cout << endl;
/*---------------------------------------------------------------------*/
#define setout(s)                             \
  for (auto i = s.begin(); i != s.end(); i++) \
  {                                           \
    int p = *i;                               \
                cout                          \
            << p << " ";                      \
  }                                           \
  cout << endl;
/*---------------------------------------------------------------------*/
#define mi(m) map<int, int> m;
#define msi(m) map<string, int> m;
#define mci(m) map<char, int> m;
#define mex(mex) int mex = INT_MIN;
#define men(men) int men = INT_MAX;
#define int long long
#define debug(x) cerr << "The value of " << #x << " is " << x << endl;
#define see(x) cout << "The value of " << #x << " is " << x << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = n - 1; i >= 0; i--)
#define sqr(x) ((LL)(x) * (x))
#define reset(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define sz(v) (int)v.size()
#define haa cout << "YES" << endl;
#define naa cout << "NO" << endl;
#define permute next_permutation
/*---------------------------------solve-------------------------------*/
void solve()
{


iin(n);
vin(v,n);
sort(all(v));
mi(m);
mapin(m,v);
int cnt=0;
repn(i,n){
            auto it=m.rbegin();
            int p=it->first;
            if(p>v[i]){
                        m[p]--;
                        cnt++;
                        if( m[p]==0)m.erase( p);
            }
}
cout<<cnt<<endl;

}

/*---------------------------------main---------------------------------*/
signed main()
{
  _coding_is_fun_

  solve();
}
/*---------------------------------------------------------------------*/


Comments

Submit
0 Comments
More Questions

1358D - The Best Vacation
1620B - Triangles on a Rectangle
999C - Alphabetic Removals
1634C - OKEA
1368C - Even Picture
1505F - Math
1473A - Replacing Elements
959A - Mahmoud and Ehab and the even-odd game
78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing