CCF-CSP认证 2023年3月02 垦田计划

​
//能不用迭代器就别用,只有for遍历的时候再用,其他用不明白。
#pragma GCC optimize(2, 3, "Ofast", "inline")
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, m, k;
unordered_map<int, int> Index;
struct quarter
{
    int per;
    int total;
    bool operator<(const quarter &q) const
    {
        return q.total < total;
    }
};
multiset<quarter> Q;
void print_Q()
{
    for (auto it1 = Q.begin(); it1 != Q.end(); it1++)
    {
        cout << (*it1).total << " " << (*it1).per << endl;
    }
}
void work()
{
    cin >> n >> m >> k;
    quarter temp, temp1;
    for (int i = 0; i < n; i++)
    {
        int ti, ci;
        cin >> ti >> ci;
        Index[ti] += ci;
    }
    for (auto it = Index.begin(); it != Index.end(); it++)
    {
        temp.total = (*it).first;
        temp.per = (*it).second;
        Q.insert(temp);
    }
    print_Q();
    auto it = Q.begin();
    auto next=++Q.begin();
    temp = (*it);
    temp1 = (*next);

    cout<<temp.total<<" "<<temp1.total;
    // 遍历完剩下只有一个quarter
    bool left_one = true;
    while (next != Q.end())
    {
        print_Q();
        temp = (*it);
        temp1 = (*next);
        Q.erase(it);
        Q.erase(next);
        cout<<temp.total<<" "<<temp1.total;
        int tempm = m - temp.per * (temp.total - temp1.total);
        // 能把最长天数缩短到第二长天数
        if (tempm >= 0)
        {
            // 两项合并
            temp.total = temp1.total;
            temp.per += temp1.per;
        }
        // 不能
        else
        {
            left_one = false;
            temp.total -= m / temp.per;
            Q.insert(temp);
            break;
        }
        Q.insert(temp);
        m = tempm;
        auto it = Q.begin();
        next = ++Q.begin();;
    }
    if (left_one && m > (*it).per)
    {
        temp = (*it);
        int temptotal = temp.total - m / temp.per;
        if (temptotal > k)
        {
            temp.total = temptotal;
        }
        else
        {
            temp.total = k;
        }
        Q.erase(it);
        Q.insert(temp);
    }
    cout << (*Q.begin()).total;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    work();
    return 0;
}

​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值