//能不用迭代器就别用,只有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;
}
CCF-CSP认证 2023年3月02 垦田计划
于 2024-06-01 09:40:16 首次发布