B. Monsters | Codeforces Solution | Educational Codeforces Round 152 (Rated for Div. 2)
Monocarp is playing yet another computer game. And yet again, his character is killing some monsters. There are π monsters, numbered from 1 to π , and the π -th of them has ππ health points initially.
B. Monsters
time limit per test: 2 seconds | memory limit per test: 256 megabytes
Monocarp is playing yet another computer game. And yet again, his character is killing some monsters. There are n monsters, numbered from 1 to n, and the i-th of them has ai health points initially.
Monocarp's character has an ability that deals k damage to the monster with the highest current health. If there are several of them, the one with the smaller index is chosen. If a monster's health becomes less than or equal to 0 after Monocarp uses his ability, then it dies.
Monocarp uses his ability until all monsters die. Your task is to determine the order in which monsters will die.
Input
The first line contains a single integer t (1 β€ t β€ 104) β the number of test cases.
The first line of each test case contains two integers n and k (1 β€ n β€ 3β 105; 1 β€ k β€ 109) β the number of monsters and the damage which Monocarp's ability deals.
The second line contains n integers a1, a2, β¦, an (1 β€ ai β€ 109) β the initial health points of monsters.
The sum of n over all test cases doesnβt exceed 3β 105.
Output
For each test case, print n integers β the indices of monsters in the order they die.
Example
Input
3 3 2 1 2 3 2 3 1 1 4 3 2 8 3 5
Output
2 1 3 1 2 3 1 2 4
Note
In the first example, the health points change as follows: [1, 2, 3] β [1, 2, 1] β [1, 0, 1] β [β1, 0, 1] β [β1, 0, β1]
. The monster that is going to take damage the next time Monocarp uses his ability is underlined.
In the second example, the health points change as follows: [1, 1] β [β2, 1] β [β2, β2]
.
In the third example, the health points change as follows: [2, 8, 3, 5] β [2, 5, 3, 5] β [2, 2, 3, 5] β [2, 2, 3, 2] β [2, 2, 0, 2] β [β1, 2, 0, 2] β [β1, β1, 0, 2] β [β1, β1, 0, β1]
.
struct Compare
{
static bool cmp(const pii &a, const pii &b)
{
if (a.first == 0)
return false;
if (b.first == 0)
return true;
if (a.first == b.first)
return a.second > b.second;
return a.first < b.first;
}
bool operator()(const pii &a, const pii &b) const
{
return cmp(a, b);
}
};
void solve()
{
int n, k;
cin >> n >> k;
vi v(n);
priority_queue<pair<int, int>, vector<pii>, Compare> q;
for (int i = 0; i < n; i++)
{
cin >> v[i];
int rem = v[i] % k;
if (rem == 0)
rem += k;
q.push({rem, i});
}
while (!q.empty())
{
pii f = q.top();
q.pop();
cout << f.second + 1 << " ";
}
cout << endl;
}
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define F first
#define S second
#define pb push_back
#define si set<int>
#define vi vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define vpp vector<pair<int, pii>>
#define mii map<int, int>
#define mpi map<pii, int>
#define spi set<pii>
#define endl "\n"
#define sz(x) ((int)x.size())
#define all(p) p.begin(), p.end()
#define double long double
#define que_max priority_queue<int>
#define que_min priority_queue<int, vi, greater<int>>
#define bug(...) __f(#__VA_ARGS__, __VA_ARGS__)
#define print(a) \
for (auto x : a) \
cout << x << " "; \
cout << endl
#define print1(a) \
for (auto x : a) \
cout << x.F << " " << x.S << endl
#define print2(a, x, y) \
for (int i = x; i < y; i++) \
cout << a[i] << " "; \
cout << endl
#define scanit(a, n) \
for (ll indexaa = 0; indexaa < n; indexaa++) \
cin >> a[indexaa];
inline int power(int a, int b)
{
int x = 1;
while (b)
{
if (b & 1)
x *= a;
a *= a;
b >>= 1;
}
return x;
}
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...);
}
const int N = 200005;
struct Compare
{
static bool cmp(const pii &a, const pii &b)
{
if (a.first == 0)
return false;
if (b.first == 0)
return true;
if (a.first == b.first)
return a.second > b.second;
return a.first < b.first;
}
bool operator()(const pii &a, const pii &b) const
{
return cmp(a, b);
}
};
void solve()
{
int n, k;
cin >> n >> k;
vi v(n);
priority_queue<pair<int, int>, vector<pii>, Compare> q;
for (int i = 0; i < n; i++)
{
cin >> v[i];
int rem = v[i] % k;
if (rem == 0)
rem += k;
q.push({rem, i});
}
while (!q.empty())
{
pii f = q.top();
q.pop();
cout << f.second + 1 << " ";
}
cout << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
clock_t z = clock();
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
Raunit Verma
Technical Writer at CodingKaro