集合操作

Posted 20203246

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合操作相关的知识,希望对你有一定的参考价值。

https://ac.nowcoder.com/acm/contest/11173/C

#include <bits/stdc++.h>
using namespace std;
#define int long long
using LL = long long;
template <typename T>void read(T &x){
    x=0;T f=1;
    char ch=getchar();
    while(!isdigit(ch)){ if(ch==\'-\')f=-1;ch=getchar(); }
    while(isdigit(ch)){ x=(x<<3)+(x<<1)+ch-\'0\';ch=getchar(); }
    x*=f;
}
template <typename T>void print(T x){
    if(x<0){ putchar(\'-\');x=-x; }
    if(x>9)print(x/10);putchar(x%10+\'0\');
}
template <typename T>T Abs(T x){ return x>0?x:-x; }

const int Maxn = 1e6;
const LL Maxr = 1e18;

int n;
LL k, p;
LL a[Maxn + 5],tema[Maxn + 5];
bool check (LL x) {
    LL temp = a[n] - x * p;
    LL sum = 0;
    for (int i = 1; i <= n; i++){
        if (a[i] <= temp) continue;
        sum += (a[i] - temp) / p;
    }
    return sum >= k;
}
struct Node {
    int idx;
    LL val;
    Node () {}
    Node (int IDX,LL VAL) {
        idx=IDX;
        val=VAL;
    }
    bool operator < (const Node &x) const {
        return val > x.val;
    }
};
priority_queue <Node> q;
void solve(LL x) {
    LL temp=a[n] - x * p;
    LL sum = 0;
    for (int i = 1; i <= n; i++) {
        if (a[i] < temp) continue;
        sum += (a[i] - temp) / p;
        a[i] -= (a[i] - temp) / p * p;
        q.push (Node (i, a[i]));
    }
    for (int i=1; i <= sum - k; i++) {
        Node tem = q.top(); q.pop ();
        while(a[tem.idx] + p > tema[tem.idx]) {
            tem = q.top(); q.pop();
        }
        a[tem.idx] += p;
        tem.val += p;
        q.push (tem);
    }
    sort (a + 1, a + 1 + n);
    for(int i = 1; i <= n; i++) {
        print(a[i]); putchar(\' \');
    }
}
signed main()
{
    read (n); read (k); read (p);
    for (int i = 1; i <= n; i++) {
        read (a[i]);
    }
    sort (a + 1, a + 1 + n);
    memcpy(tema, a, sizeof a);

    if (p == 0) {
        for (int i = 1; i <= n; i++)
            printf("%lld ", a[i]);
        return 0;
    }

    LL l =0 , r = Maxr * 2 / p;
    while (l + 1 < r){
        LL mid = l + r >> 1;
        if (check (mid)) r = mid;
        else l = mid;
    }
    if(check (l)) solve (l);
    else solve (r);
    return 0;
}

  

以上是关于集合操作的主要内容,如果未能解决你的问题,请参考以下文章

金蝶handler中 collection 代码片段理解

Alfred常见使用

比较 C# 中的字符串片段并从集合中删除项目

带有红宝石集合/可枚举的酷技巧和富有表现力的片段[关闭]

VSCode自定义代码片段——git命令操作一个完整流程

VSCode自定义代码片段15——git命令操作一个完整流程