如果你用堆CE

P1168 中位数

@[zhangzirui66](/user/1137373) 啊?不用吧 你用的是 Java?……(胡乱猜想)
by Carroty_cat @ 2024-02-18 13:04:03


我用的C++ CE: https://www.luogu.com.cn/record/147288353 AC: https://www.luogu.com.cn/record/147288439
by zhangzirui66 @ 2024-02-18 13:16:54


@[Carroty_cat](/user/912750)
by zhangzirui66 @ 2024-02-18 13:18:07


@[zhangzirui66](/user/1137373) 代码发一下
by Carroty_cat @ 2024-02-18 13:29:14


@[Carroty_cat](/user/912750) 上面的网址
by zhangzirui66 @ 2024-02-18 13:41:24


@[zhangzirui66](/user/1137373) 看不了。
by Carroty_cat @ 2024-02-18 13:42:03


CE: ```cpp #include<bits/stdc++.h> using namespace std; priority_queue<int,vector<int> > q1; priority_queue<int,vector<int>,greater<int> > q2; int main(){ int n; scanf("%d", &n); int a; scanf("%d", &a); q1.push(a); cout << q1.top() << "\n"; for(int i = 2; i <= n; i ++){ int input; scanf("%d", &input); if(input > q1.top()) q2.push(input); else q1.push(input); while(abs(q1.size() - q2.size()) > 1)//这里 if(q1.size() > q2.size()){ q2.push(q1.top()); q1.pop(); } else{ q1.push(q2.top()); q2.pop(); } if(i % 2) cout << (q1.size() > q2.size() ? q1.top() : q2.top()) << "\n"; } return 0; } ``` AC: ```cpp #include<bits/stdc++.h> using namespace std; priority_queue<int,vector<int> > q1; priority_queue<int,vector<int>,greater<int> > q2; int main(){ int n; scanf("%d", &n); int a; scanf("%d", &a); q1.push(a); cout << q1.top() << "\n"; for(int i = 2; i <= n; i ++){ int input; scanf("%d", &input); if(input > q1.top()) q2.push(input); else q1.push(input); while(abs((int)(q1.size() - q2.size())) > 1)//修改后 if(q1.size() > q2.size()){ q2.push(q1.top()); q1.pop(); } else{ q1.push(q2.top()); q2.pop(); } if(i % 2) cout << (q1.size() > q2.size() ? q1.top() : q2.top()) << "\n"; } return 0; } ```
by zhangzirui66 @ 2024-02-18 13:52:04


@[Carroty_cat](/user/912750)
by zhangzirui66 @ 2024-02-18 13:52:24


@[zhangzirui66](/user/1137373) 啊啊啊对不起,我还以为您说的是 `size_t` 和 `int` 的大小比较,`abs()` 的确是有多重匹配会 CE 呃呃呃
by Carroty_cat @ 2024-02-18 14:00:41


|