Collecting Game

#include<bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
#define read(x) int x; cin >> x
#define pb push_back
#define ll long long
using namespace std;
void init_code() {
fast_io;
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
}
int main()
{
init_code();
int t;
cin >> t;
while (t–)
{
int n;
cin >> n;
std::vector < pair<ll, ll>> a(n);
for (int i = 0; i < n; ++i)
{
/* code */
cin >> a[i].first;
a[i].second = i;
}
sort(a.begin(), a.end());
vector<ll> pref(n, 0);
pref[0] = a[0].first;
for (int i = 1; i < n; ++i)
{
/* code */
pref[i] = pref[i – 1] + a[i].first;
}
vector<ll> ans(n, -1);
stack<int> dex;
for (int i = 0; i < n; ++i)
{
/* code */
dex.push(a[i].second);
if (i == n – 1 or pref[i] < a[i + 1].first)
{
while (!dex.empty())
{
ans[dex.top()] = i;
dex.pop();
}
}
}
for (int i = 0; i < n; ++i)
{
/* code */
cout << ans[i] << ” “;
}
cout << endl;
}
}

1 thought on “Collecting Game”

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top