CF1515-C. Phoenix and Towers

题意

给出\(n\)块积木,对于每块积木都有一个高度\(h_i(h_i\le x)\),现在让你将这\(n\)块积木分成\(m\)堆,使得任意两堆积木的高度差不超过\(x\).

思路

先将积木按照高度从大到小排序,将前\(m\)个积木加入到集合中,每次选出集合中高度最小的堆,将当前积木放进该队中,这样就能保证最终能构造出符合要求的答案。

证明

题目中提到每块积木的高度都小于等于\(x\),那么最一开始将\(m\)块积木放进优先队列中,就可以保证这\(m\)块积木的最大高度和最小高度的高度差不超过\(x\)

现在设优先队列中高度最小的堆的高度为\(h_0\),次小高的堆的高度为\(h_1\),易得到\(h_0 \le h_1\),那么当\(h_0\)加上任意一个高度\(h(h\le x)\),有\((h_0+h)-h_1<=x\)

\(h_0+h\)可能比优先队列中最大的元素要大,也可能小,也可能相等,但是这个并不是关键。

代码

#include <iostream>
#include <queue>
 
const int N = 100005;
 
int a[N], res[N];
 
struct Node {
    int p, h;
    Node(){}
    Node(int p, int h):p(p), h(h){}
    bool operator < (const Node &x) const {
        return h > x.h;
    }
};
 
void solve() {
    int n, m, x;
    std::cin >> n >> m >> x;
    for (int i = 0; i < n; i++) {
        std::cin >> a[i];
    }
    std::priority_queue<Node> q;
    for (int i = 1; i <= m; i++) {
        q.push(Node(i, 0));
    }
    for (int i = 0; i < n; i++) {
        Node f = q.top();
        q.pop();
        res[i] = f.p;
        f.h += a[i];
        q.push(f);
    }
    while (q.size() > 1) {
        Node f = q.top();
        q.pop();
        if (q.top().h - f.h > x) {
            puts("NO");
            return;
        }
    }
    puts("YES");
    for (int i = 0; i < n; i++) {
        std::cout << res[i] << " ";
    }
    std::cout << std::endl;
}
 
int main() {
    int T;
    std::cin >> T;
    while (T--) solve();
    return 0;
}
posted @ 2021-10-01 18:23  牟翔宇  阅读(61)  评论(0编辑  收藏  举报