1515C - Phoenix and Towers - CodeForces Solution


constructive algorithms data structures greedy *1400

Please click on ads to support us..

Python Code:

import heapq
for _ in range(int(input())):
    n,m,x = map(int,input().split())
    h = list(map(int,input().split()))

    tower = [[0,i] for i in range(m)]
    heapq.heapify(tower)
    res = [0]*n
    for j in range(n):
        height, idx = heapq.heappop(tower)
        height += h[j]
        res[j] = idx+1
        heapq.heappush(tower,[height,idx])

    if abs(tower[0][0]-tower[-1][0])<=x:
        print("YES")
        print(*res)
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
using namespace std;

#define ll long  long
#define loop(i,n) for(ll i=1;i<=n;i++)
const ll M=1e9+7;
const ll N=1e6+1;
ll dp[N];

void solve(){
ll n,m,x;
cin>>n>>m>>x;
vector<ll> h(n+1);
loop(i,n) cin>>h[i];
set<pair<ll,ll>> ht;
for(ll i=1;i<=m;i++){
    ht.insert(make_pair(0,i));
}
cout<<"YES"<<endl;
for(ll i=1;i<=n;i++){
    auto it=*(ht.begin());
    ht.erase(it);
    cout<<it.second<<" ";
    ht.insert(make_pair(it.first+h[i],it.second));
}
cout<<endl;
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin>>t;
while(t--){
solve();
}
return 0;
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push