zhangzirui66 @ 2024-02-18 12:54:00
abs是int类型,size()是无符号,要强转.
by CarrotMeow @ 2024-02-18 13:04:03
@zhangzirui66 啊?不用吧
你用的是 Java?……(胡乱猜想)
by zhangzirui66 @ 2024-02-18 13:16:54
我用的C++ CE: https://www.luogu.com.cn/record/147288353 AC: https://www.luogu.com.cn/record/147288439
by zhangzirui66 @ 2024-02-18 13:18:07
@Carroty_cat
by CarrotMeow @ 2024-02-18 13:29:14
@zhangzirui66 代码发一下
by zhangzirui66 @ 2024-02-18 13:41:24
@Carroty_cat 上面的网址
by CarrotMeow @ 2024-02-18 13:42:03
@zhangzirui66 看不了。
by zhangzirui66 @ 2024-02-18 13:52:04
CE:
#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:
#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:24
@Carroty_cat
by CarrotMeow @ 2024-02-18 14:00:41
@zhangzirui66 啊啊啊对不起,我还以为您说的是 size_t
和 int
的大小比较,abs()
的确是有多重匹配会 CE 呃呃呃