社区讨论

站外题 WA 求调(紧急)!!!

学术版参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mj49vgl8
此快照首次捕获于
2025/12/13 20:28
2 个月前
此快照最后确认于
2025/12/16 11:55
2 个月前
查看原帖
🎁 题目描述:庆生会 (Birthday Party)
题目描述 (Problem Description)
你正在为一位名叫 Wu_Mr 的朋友挑选生日礼物。礼物种类与数量: 共有 nn 种不同的礼物,每种礼物都有两份可供购买。购买价格:对于第 ii 种礼物:购买第一份的花费是 aia_i。购买第二份的花费使得两份礼物的总花费为 bib_i。(这意味着购买第二份的额外花费是 biaib_i - a_i)。
目标: 你想要购买总共 mm 份礼物。你需要找到一个购买方案,使得你的总花费最小。
输入格式 (Input Format)
输入文件名为 birthday.in。
第一行: 两个整数 nnmm,分别表示礼物的种类数和所需购买的礼物总份数。接下来的 nn 行: 每行包含两个整数 aia_ibib_i,分别表示第 ii 种礼物的第一份花费 aia_i 和两份的总花费 bib_i
示例输入 (Input 1):
6 8
66 113
81 90
69 107
18 19
50 57
2 146
📤 输出格式 (Output Format)
输出文件名为 birthday.out。
输出: 仅一个整数,表示购买 mm 份礼物所需的最小总花费。
示例输出 (Output 1):
234
数据范围 (Constraints)
n3×105n \le 3 \times 10^5, 0ai1090 \le a_i \le 10^9, 0bi1090 \le b_i \le 10^9, m2nm \le 2n
注意:bib_i 可能小于 aia_i
WA 代码(样例输出 286):
CPP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300010;
const ll INF = 1e18;
ll a[N], b[N];
int tag[N], n, m;
int main() {
    freopen("birthday.in", "r", stdin);
    freopen("birthday.out", "w", stdout);
    ios::sync_with_stdio(0); 
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) cin >> a[i] >> b[i];
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> q1, q2, q3;
    priority_queue<pair<ll, int>> q4;
    for (int i = 1; i <= n; ++i) {
        q1.emplace(a[i], i);
        q3.emplace(b[i], i);
    }
    ll ans = 0;
    for (int k = 0; k < m; ++k) {
        while (!q1.empty() && tag[q1.top().second]) q1.pop();
        while (!q2.empty() && tag[q2.top().second] != 1) q2.pop();
        while (!q3.empty() && tag[q3.top().second]) q3.pop();
        while (!q4.empty() && !tag[q4.top().second]) q4.pop();
        ll o1 = INF, o2 = INF, o3 = INF;
        int i1 = -1, i2 = -1, i3n = -1, i3o = -1;
        if (!q1.empty()) { o1 = q1.top().first; i1 = q1.top().second; }
        if (!q2.empty()) { o2 = q2.top().first; i2 = q2.top().second; }
        if (!q3.empty() && !q4.empty()) {
            o3 = q3.top().first - q4.top().first;
            i3n = q3.top().second;
            i3o = q4.top().second;
        }
        if (o1 <= o2 && o1 <= o3) {
            ans += o1;
            tag[i1] = 1;
            q4.emplace(o1, i1);
            q2.emplace(b[i1] - a[i1], i1);
        } else if (o2 <= o3) {
            ans += o2;
            tag[i2] = 2;
            q4.emplace(o2, i2);
        } else {
            ans += o3;
            if(tag[i3o]==2)
                q4.emplace(a[i3o], i3o);
            tag[i3o]--;
            tag[i3n] = 2;
            q4.emplace(b[i3n] - a[i3n], i3n);
        }
    }
    cout << ans << '\n';
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...