社区讨论
AC了,但不明白过程
P3467[POI 2008] PLA-Postering参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhjo8pyq
- 此快照首次捕获于
- 2025/11/04 05:48 4 个月前
- 此快照最后确认于
- 2025/11/04 05:48 4 个月前
我的第一版
CPP#include <iostream>
#include <stack>
using namespace std;
int n, ans = 0;
stack<int> s;
int main() {
cin >> n;
for (int i = 1, d, x; i <= n; i++) {
cin >> d >> x;
if(!s.empty()&&s.top()>=x) ans++;
//我以开始以为试验样例时,输出为2是因为其本身没有加入答案
while (!s.empty() && s.top() >= x) {
if (s.top() != x)
ans++;
s.pop();
}
s.push(x);
}
cout << ans;
return 0;
}
但是这样只过了两个点。
但当我改为
CPP#include <iostream>
#include <stack>
using namespace std;
int n, ans = 0;
stack<int> s;
int main() {
cin >> n;
for (int i = 1, d, x; i <= n; i++) {
cin >> d >> x;
while (!s.empty() && s.top() >= x) {
if (s.top() != x)
ans++;
s.pop();
}
s.push(x);
}
ans += s.size();
cout << ans;
return 0;
}
为什么就对了
回复
共 3 条回复,欢迎继续交流。
正在加载回复...