Codeforces Round 914 (Div. 2) 1904B. Collecting Game Solution
Problem Link: https://codeforces.com/problemset/problem/1904/B
Solution in C++:
- /// Author : AH_Tonmoy
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int t;
- cin >> t;
- while (t--) {
- int n ; cin >> n ;
- vector<pair<int,int>>a(n) ;
- for ( int i = 0 ; i < n ; i++){
- cin >> a[i].first ;
- a[i].second = i ;
- }
- sort(a.begin(),a.end()) ;
- vector<long long > p(n,0) ;
- p[0] = a[0].first ;
- for ( int i = 1 ; i < n ; i++){
- p[i] = p[i - 1 ] + a[i].first ;
- }
- stack<int> st ;
- vector<int>ans(n) ;
- for ( int i = 0 ; i < n ; i++){
- st.push(a[i].second) ;
- if( p[i] < a[i+1].first or i == n - 1 ){
- while(!st.empty()){
- ans[st.top()] = i ;
- st.pop() ;
- }
- }
- }
- for ( auto i : ans ){
- cout << i <<" ";
- }
- cout <<'\n';
- }
- return 0 ;
- }
No comments