题意:
给出一个2e5的数组,有两种操作
1.对【 1 , X 】升序排列
2.对【 1 , X 】降序排列
求最终的数组
思路:
单调栈维护有效操作区间后,按序填入每个位置上的数即可。
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 7;
const int INF = 0x3f3f3f3f;
int a[MAXN],b[MAXN],c[MAXN],aa[MAXN];
int main()
{
int n, m, top;
while (cin >> n >> m){
for (int i = 1; i <= n; i++) cin >> a[i], aa[i] = a[i];
b[0] = INF, top = 0;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
while (b[top] <= y) {
top--;
}
top++;
b[top] = y;
c[top] = x;
}
b[top + 1] = 0;
sort(aa + 1, aa + b[1] + 1);
int pos = b[1], l = 1, r = b[1];
for (int i = 1; i <= top; i++) {
int len = b[i] - b[i + 1];
if (c[i] == 1) {
for (int j = 0; j < len; j++) {
a[pos--] = aa[r--];
}
}
else {
for (int j = 0; j < len; j++) {
a[pos--] = aa[l++];
}
}
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ';
cout << endl;
}
//system("pause");
}
该博客详细介绍了Codeforces 631C题目,涉及数组排序操作。通过使用单调栈来维护有效的操作区间,对每个位置依次填入正确的数值,从而得到最终有序的数组。文章提供了清晰的解题思路和代码实现。
345

被折叠的 条评论
为什么被折叠?



