1784C - Monsters (hard version) - CodeForces Solution


binary search data structures greedy

Please click on ads to support us..

C++ Code:

///The imagination can believe anything so we must lead not follow it
///we are not our thoughts
///Don't ask for permission, ask for forgiveness
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 500500
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
const ll M=1e9+7;

ll a[N],ans[N];

main()
{
    fast;

    ll i=0,j,k,n,m,idx,x,y,z;

    int tt=1;
    cin>>tt;
    while(tt--)
    {
        cin>>n;
        vector<pair<ll,ll>>b;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            b.pb({a[i],i});
        }
        ll l=0;
        ll mx=-1,cnt=0;
        sort(b.begin(),b.end());
        set<ll>st;
        for(i=0;i<n;i++)
        {
            //cout<<b[i].first<<" "<<b[i].second<<endl;

            st.insert(b[i].second);
            auto it=st.end();
            it--;
            mx=*it;
            if(l+1>b[i].first)
            {
                ans[mx]-=b[i].first;
                //ans[mx]+=mx+1;
                //cout<<mx<<ans[mx]<<endl;
                st.erase(it);
                cnt++;
            }
            else
            {
                l++;
            }
        }
        j=2;
        cnt=0;
        ans[0]+=a[0]-1;
        for(i=1;i<n;i++)
        {
            if(ans[i]!=0)
            {

            }
            else
            {
                ans[i]-=j;
                j++;
            }
            ans[i]+=ans[i-1];
            ans[i]+=a[i];
        }
        for(i=0;i<n;i++)
        {
            cout<<ans[i]<<" ";
        }
        cout<<endl;
        for(i=0;i<n;i++)
        {
            ans[i]=0;
        }


























    }
    return 0;

}
/*
1
27
23 23 21 18 10 13 24 4 26 14 9 16 1 8 9 2 20 26 13 1 16 10 19 1 16 22 26



22 43 61 75 80 87 104 100 117 121 119 123 111 105 99 85 88 96 90 90 86 75 72 72 65 63 64
*/


Comments

Submit
0 Comments
More Questions

2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets