nyyyh @ 2024-02-11 15:42:57
为什么显示abs函数重载,编译错误?就是一个对顶堆,样例是对的......脑袋要炸
#include<bits/stdc++.h>
using namespace std;
priority_queue <int> pq1;
priority_queue<int, vector<int>, greater<int>> pq2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
int x;
cin >> x;
string a, b;
pq1.push(x);
cout << pq1.top() << '\n';
a += "0";
int y;
for (int i = 2; i <= n; i++) {
cin >> y;
if (y > pq1.top()) {
pq2.push(y);
b += "0";
} else {
pq1.push(y);
a += "0";
}
while (abs(a.size() - b.size()) > 1)
if (a.size() > b.size()) {
pq2.push(pq1.top());
b += "0";
pq1.pop();
a.pop_back();
} else {
pq1.push(pq2.top());
a += "0";
pq2.pop();
b.pop_back();
}
if (i % 2) {
if (a.size() > b.size()) {
cout << pq1.top() << '\n';
} else {
cout << pq2.top() << '\n';
}
}
}
return 0;
}
神马情况?
by whhsteven @ 2024-02-11 16:06:59
size 的返回值我记得是无符号类型。
by Special_Tony @ 2024-02-11 16:07:04
@nyyyh 嗯,类型不对,abs是int的,.size()的是unsigned long的,另外你直接abs也没用,因为unsigned
by zhangzirui66 @ 2024-02-18 12:50:48
强转int