如果你用三目WA了并且0pts

P1168 中位数

creation_hy @ 2022-09-23 19:09:15

三目闭坑指南

三目运算符的优先级比逗号还高!!!所以如果你是这么写的:

#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int>> gq;
priority_queue<int, vector<int>, less<int>> lq;
int n, x;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> x;
    lq.push(x);
    cout << x << '\n';
    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        x > lq.top() ? gq.push(x) : lq.push(x);
        while (labs(lq.size() - gq.size()) > 1)
            lq.size() > gq.size() ? gq.push(lq.top()), lq.pop() : lq.push(gq.top()), gq.pop();
        if (i & 1)
            cout << (lq.size() > gq.size() ? lq.top() : gq.top()) << '\n';
    }
    return 0;
}

请改成:

#include <bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int>, greater<int>> gq;
priority_queue<int, vector<int>, less<int>> lq;
int n, x;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> x;
    lq.push(x);
    cout << x << '\n';
    for (int i = 2; i <= n; i++)
    {
        cin >> x;
        x > lq.top() ? gq.push(x) : lq.push(x);
        while (labs(lq.size() - gq.size()) > 1)
            lq.size() > gq.size() ? (gq.push(lq.top()), lq.pop()) : (lq.push(gq.top()), gq.pop()); //加括号!!
        if (i & 1)
            cout << (lq.size() > gq.size() ? lq.top() : gq.top()) << '\n';
    }
    return 0;
}

因为如果这么写:

cmp ? a, b: c, d;

实际上和这个是等效的:

cmp ? a;
b: c;
d;

未定义行为了属于是(


by Sprague_Garundy @ 2022-09-23 19:17:59

@creation_hy 逗号优先级在所有运算符中是最低的不是常识吗。。。


by creation_hy @ 2022-09-23 19:20:41

@Sprague_Garundy 我甚至曾经认为三目优先级比分号还低(((

但是三目确实是除了逗号分号赋值以外最低的了吧(


by luoyx @ 2022-09-23 19:23:42

估计只有你会这么错了吧


by creation_hy @ 2022-09-23 20:58:23

貌似是的,应该很少有人很多句话还用三目


by xuekaiwen_emmm @ 2023-01-02 21:20:31

我连在这之前连三目是啥都不知道,现在才知道原来?:是叫三目


by AKPC @ 2023-01-11 14:19:14

chy


|