1849B - Monsters - CodeForces Solution


greedy sortings

Please click on ads to support us..

C++ Code:

/*
NAME : SUBHADEEP DAS
USERNAME : subhadeep2001 
INSTITUTION : IIEST SHIBPUR(2020-2024)
*/
// Optimizations
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("expensive-optimizations")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
//Libraries
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define fo2(i,start,end) for(i=start;i<=end;i++)
#define rfo(i,n) for(i=n-1;i>=0;i--)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define eb emplace_back
#define ef emplace_front
#define sz(x) ((ll)(x).size())
#define INF 2e18
#define F first
#define S second
#define nl "\n"
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define setv(s,x) memset(s,x,sizeof(s))
#define sortall(x) sort(all(x))
#define isin(s,x) (s.find(x)!=s.end())
#define isinv(s,x) (find(all(s),x)!=s.end())
#define isins(s,x) (s.find(x)!=string::npos)
#define popcount(x) __builtin_popcountll(x)
#define czr(x) __builtin_ctzll(x)
#define czl(x) __builtin_clzll(x)
#define subhadeep2001
#define maxi(x) *max_element(all(x))
#define mini(x) *min_element(all(x))
#define sp(x) cout<<fixed<<setprecision(x)
#define ub(v,x) upper_bound(all(v),x)
#define lb(v,x) lower_bound(all(v),x)
#define ubi(v,x) ub(v,x)-v.begin()
#define lbi(v,x) lb(v,x)-v.begin()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 1e-9;
const ll mod = 1000000007;
const ll modx = 998244353;
const int N = 3e5, M = N;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
//vl adj[N+1]; // change N according to number of nodes given in question
//ll fact[N+1];//change N according to question
//int m = unique(s.begin(), s.end()) - s.begin(); calculate blocks of consecutive same elements Like 100111 --- 1, 00, 111 so m=3.
// No of digits in N = floor(log10(N))+1 

#ifdef subhadeep2001
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

typedef unsigned long long ull;
typedef long double lld;
template<typename T> using oset =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;/*  *find_by_order() , order_of_key() */

// Operator overloads
template<typename T1, typename T2> // cin >> pair<T1, T2>
istream& operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); }
 
template<typename T> // cin >> vector<T>
istream& operator>>(istream &istream, vector<T> &v)
{
    for (auto &it : v)
        cin >> it;
    return istream;
}
 
template<typename T1, typename T2> // cout << pair<T1, T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); }
template<typename T> // cout << vector<T>
ostream& operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) cout << it << " "; return ostream; }
 
template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " = " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
    const char* comma = strchr (names + 1, ',');
    cout.write (names, comma - names) << " = " << arg1 << " | "; __f (comma + 1, args...);
}

template <typename T>
T gmax(T t) 
{
    return t;
}

template<typename T, typename... Args>
T gmax(T t, Args... args) // recursive variadic function
{
    return max(t, gmax(args...)) ;
}

template <typename T>
T gmin(T t) 
{
    return t;
}

template<typename T, typename... Args>
T gmin(T t, Args... args) // recursive variadic function
{
    return min(t, gmin(args...)) ;
}

 
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.F);cerr << ","; _print(p.S); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

void disp(vl v){ for (auto u : v) cout << u << " "; cout <<"\n"; }
ll binpow(ll a, ll b) { ll res = 1; while (b > 0) { if (b & 1) res = res * a;a = a * a;b >>= 1;}return res;}/*Without mod*/
ll mpow(ll a, ll b, ll m) { a %= m; ll res = 1; while (b > 0) {if (b & 1) res = res * a % m;a = a * a % m;b >>= 1;}return res;}/*With mod*/
ll logf(ll base, ll num){if(num==1)return 0;ll c=0;while(num>1){num/=base;c++;}return c;}/*log*/
bool prime(ll n){ll c=0;for(ll i=2;i<=sqrt(n);i++){if(n%i==0)return false;}return true;}/*Prime check*/
ll gcd (ll a, ll b){ while (b){a %= b;swap(a, b);}return a;}/*GCD*/
ll lcm (ll a, ll b) {return a / gcd(a, b) * b;}/*LCM*/
ll mminvprime(ll a, ll b) {return mpow(a, b - 2, b);}/*Modular Inverse*/
void google(int t) {cout << "Case #" << t << ": ";}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll mod_div(ll a, ll b, ll m) {a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m;}  //only for prime m
ll phin(ll n) {ll number = n; if (n % 2 == 0) {number /= 2; while (n % 2 == 0) n /= 2;} for (ll i = 3; i <= sqrt(n); i += 2) {if (n % i == 0) {while (n % i == 0)n /= i; number = (number / i * (i - 1));}} if (n > 1)number = (number / n * (n - 1)) ; return number;} //O(sqrt(N))
ll binsearch(vector<ll>&v , ll l, ll r, ll x){if(l<=r){ll mid= l+(r-l)/2;if(x>v[mid])return binsearch(v,mid+1,r,x);else if(x<v[mid])return binsearch(v,l,mid-1,x); else return mid;}return -1;}/*binary search*/
ll rng(ll lim){uniform_int_distribution<int> uid(0, lim - 1);return uid(rang);}

/*void ipgraph(ll n, ll m)
{
    ll i, u, v;
    while(m--)
    {
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
}*/
/*void dfs(ll node, ll par)
{
    //cout<<node<<" ";
    for(auto it: adj[node])
    {
        if (it != par)
        dfs(it, node);
    }
}*/
bool pair_comp_dsc(pair<ll,ll> &a, pair<ll,ll> &b)
{
    // sort vect of pairs in ascending order wrt v.first
    // if 2 elements in v.first become equal then sort corr. v.second in desc order
    return (a.F < b.F || (a.F == b.F && a.S > b.S));
}
bool pair_comp_asc(pair<ll,ll> &a, pair<ll,ll> &b)
{
    // sort vect of pairs in desc order wrt v.first
    // if 2 elements in v.first become equal then sort corr. v.second in asc order
    return (a.F > b.F || (a.F == b.F && a.S < b.S));
}
/*vector<ll> decimalToBinary(ll number){
    //10 will become -> 00000000000001010
    vector<ll> binary;
    while(number){
        binary.push_back(number % 2);
        number /= 2;
    }
    //ll left = 18 - binary.size();
    return binary;
}*/
/*void initfact() { // Precompute factorial
    fact[0] = 1;
    for(ll i = 1; i < N; i++) {
        fact[i] = (fact[i-1] * i);
        fact[i] %= mod;
    }
}*/
/*ll nCr(ll n, ll r) //if n is very large or n>>>r
{
  ll num =fact[n];
  ll den = fact[n-r]*fact[r];
  den%= mod;
  den= mpow(den,mod-2,mod);
  return (num*den)%mod;
}*/
/*void SieveOfEratosthenes(ll n)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (ll p = 2; p * p <= n; p++)
    {
        if (prime[p] == true)
        {
            for (ll i = p * p; i <= n; i += p)
                prime[i] = false;
        }
    }
    for (ll p = 2; p <= n; p++)
        if (prime[p])
            cout << p << " ";
}*/

void solve(int t) {
   ll i, j, n, k;
   /*vl v(n);
   fo(i,n)
   cin>>v[i];*/
   cin>>n>>k;
   vl v(n);
   vpl ans;
   for(ll i=0;i<n;i++)
   {
    cin>>v[i];
    if(v[i]%k==0)
    {
        ans.pb({k,i+1});
    }
    else
    {
        ans.pb({v[i]%k,i+1});
    }
   }
   sort(all(ans),pair_comp_asc);
   for(auto it: ans)
   cout<<it.S<<" ";
   cout<<nl;
}

int main() {
    #ifdef subhadeep2001
    freopen("Error.txt", "w", stderr);
    #endif
    fastio();
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    auto start1 = high_resolution_clock::now();
    int tt = 1;
    cin >> tt;
    for(int t=1;t<=tt;t++) 
    {
      solve(t);
    }
    auto stop1 = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop1 - start1);
    #ifdef subhadeep2001
    cerr << "Time: " << duration . count() / 1000 << endl;
    #endif
     
}


Comments

Submit
0 Comments
More Questions

287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant
978B - File Name
1426B - Symmetric Matrix
732B - Cormen --- The Best Friend Of a Man
1369A - FashionabLee
1474B - Different Divisors
1632B - Roof Construction
388A - Fox and Box Accumulation
451A - Game With Sticks
768A - Oath of the Night's Watch
156C - Cipher
545D - Queue
459B - Pashmak and Flowers
1538A - Stone Game
1454C - Sequence Transformation
165B - Burning Midnight Oil
17A - Noldbach problem
1350A - Orac and Factors