D. Productive Meeting
https://codeforces.com/contest/1579/problem/D
题意:n个人,每个人有个能量。能量>0时,可以跟另一个有能量的人交谈,交谈完后两个人的能量都减少1。问,所有人一共最多可以交谈多少次。
思路:典型的套路,先看最大值是否大于剩下的值,如果是的话最大次数就是剩下的值,否则,应该是总和的一半。
总结:40看懂题目,思考3分钟45,为什么ac的时候已经57了呢,代码为什么12分钟才能写完?
1、写错了,每个人一个数值,并不是给定数值,然后加到指定的人身上
2、写优先队列的时候,居然去写lambda函数了。。直接写less出来就是大顶堆。
3、输出的时候,取出了俩堆顶,但是cout没有输出这些变量,而是继续去堆顶输出了,wtf。
inline void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int p = max_element(a.begin(), a.end()) - a.begin();
long long s = accumulate(a.begin(), a.end(), 0ll);
if (a[p] >= (s - a[p])) {
cout << s - a[p] << '\n';
for (int i = 0; i < n; ++i) {
if (i == p) {
continue;
}
while (a[i] --) {
cout << i + 1 << ' ' << p + 1<< '\n';
}
}
}
else {
priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> pq;
for (int i = 0; i < n; ++i) {
if (a[i]) {
pq.push({a[i], i + 1});
}
}
cout << s / 2 << '\n';
while (pq.size() > 1) {
auto tp = pq.top();
pq.pop();
auto stp = pq.top();
pq.pop();
cout << tp.second << ' ' << stp.second << '\n';
if (--tp.first) pq.push(std::move(tp));
if (--stp.first) pq.push(std::move(stp));
}
}
}

浙公网安备 33010602011771号