【忏悔】【stack】Codeforces Round #771 (Div. 2)
前言
Dashboard - Codeforces Round #771 (Div. 2) - Codeforces
这场CF打的稀烂,AB一开始都读错了题,属实是很影响心态。。。
其实本身ABC三题都不是很难,但思维固化了
怎么说呢,从哪里摔倒就从哪里站起来好吧
A. Reverse
题目
You are given a permutation p1,p2,…,pnp1,p2,…,pn of length nn. You have to choose two integers l,rl,r (1≤l≤r≤n1≤l≤r≤n) and reverse the subsegment [l,r][l,r] of the permutation. The permutation will become p1,p2,…,pl−1,pr,pr−1,…,pl,pr+1,pr+2,…,pnp1,p2,…,pl−1,pr,pr−1,…,pl,pr+1,pr+2,…,pn.
Find the lexicographically smallest permutation that can be obtained by performing exactly one reverse operation on the initial permutation.
Note that for two distinct permutations of equal length aa and bb, aa is lexicographically smaller than bb if at the first position they differ, aa has the smaller element.
A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).
思想
签到题,但读错题
看样例以为是两点交换位置,结果题目说的是reverse,是区间翻转好吧,交换是swap!!!!!!!
可恶
代码(真的需要这东西吗)
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define SF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int inf = 0x3f3f3f3f;
int a[1000], b[1000];
int main() {
SF;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
bool flag = 1;
for (int i = 1; i <= n; ++i) {
if (a[i] != i) {
for (int j = i; j <= n; ++j) {
b[j] = a[j];
if (a[j] == i) {
for (int k = j; k >= i; --k) {
a[i + j - k] = b[k];
}
break;
}
}
break;
}
}
for (int i = 1; i <= n; ++i) {
cout << a[i] << " ";
}
cout << endl;
}
return 0;
}
B. Odd Swap Sort
题目
You are given an array a1,a2,…,ana1,a2,…,an. You can perform operations on the array. In each operation you can choose an integer ii (1≤i<n1≤i<n), and swap elements aiai and ai+1ai+1 of the array, if ai+ai+1ai+ai+1 is odd.
Determine whether it can be sorted in non-decreasing order using this operation any number of times.
思想
看到没,这题说的就是swap 交换
结果还是读错题了,以为是任意两点交换,后来发现是相邻两个数才行。。。。
这题主要是分奇数和偶数两块处理,假如两个数都是奇数,且后面那个数比较小,那不管怎么交换,这个比较小的奇数也不能换到另一个比较大的奇数前面,偶数同理。
O(n)
代码
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define SF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10;
int a[N];
int main() {
SF;
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
int opos = -1, dpos = -1, flag = 1;
for (int i = 1; i <= n; ++i) {
if (a[i] & 1) {
if (opos == -1) {
opos = i;
continue;
}
if (a[i] < a[opos]) {
flag = 0;
break;
}
opos = i;
} else {
if (dpos == -1) {
dpos = i;
continue;
}
if (a[i] < a[dpos]) {
flag = 0;
break;
}
dpos = i;
}
}
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
C. Inversion Graph
题目
You are given a permutation p1,p2,…,pnp1,p2,…,pn. Then, an undirected graph is constructed in the following way: add an edge between vertices ii, jj such that i<ji
Two vertices uu and vv belong to the same connected component if and only if there is at least one path along edges connecting uu and vv.
A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).
思想
这题一看像并查集,但如果真用并查集会t,因为要遍历逆序对。
但其实不用并查集,而是用栈。
从第二组数据可以发现,维护一个最大值,在遍历的时候当前位置总是会和当前最大值的位置联通,于是我们只需要维护一个最大值,将其放在栈顶。
但这个时候看第五组数据,本来应该是 3 1 一组 5 2 4 一组,共两组,但会发现第四个位置的2把3和5连起来了。
所以我们除了要判断当前位置是否会和当前最大值联通,还要判断是否会和之前的最大值联通,而栈这个数据结构就完美满足了需求,因为它可以将之前的最大值储存在栈中。
如果当前位置可以和之前的最大值联通,那就相当于之前的最大值和当前的最大值联通了,所以就可以把之前的最大值弹出,最后栈中剩下的元素数量就是块的数量。
代码
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#define SF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const int inf = 0x3f3f3f3f;
int main() {
SF;
int T;
cin >> T;
while (T--) {
stack<int> s;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
int x;
cin >> x;
int y;
if (!s.empty() && s.top() > x) {
y = s.top(), s.pop();
while (!s.empty() && s.top() > x) s.pop();
s.push(y);
} else {
s.push(x);
}
}
cout << s.size() << endl;
}
return 0;
}