Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <deque>
- #include <iostream>
- using namespace std;
- typedef long long ll;
- int main(){
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- vector<int> v(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- bool pre_ok = true;
- for(int i = 1; i < n; i++) {
- if(v[i] < v[i - 1]) {
- pre_ok = false;
- break;
- }
- }
- if(pre_ok) {
- cout << 0 << endl;
- return 0;
- }
- for(int rounds = 1; rounds <= n; rounds++) {
- if(rounds % 2 == 1) {
- for(int i = 0; i < n; i += 2) {
- if(i + 1 < n and v[i] > v[i + 1]) {
- swap(v[i], v[i + 1]);
- }
- }
- }
- else {
- for(int i = 1; i < n; i += 2) {
- if(i + 1 < n and v[i] > v[i + 1]) {
- swap(v[i], v[i + 1]);
- }
- }
- }
- bool ok = true;
- for(int i = 1; i < n; i++) {
- if(v[i] < v[i - 1]) {
- ok = false;
- break;
- }
- }
- if(ok) {
- cout << rounds << endl;
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment