题解:P13492 【MX-X14-T2】反转时光

· · 题解

这是我写的第一篇题解

我的可能不是正解

思路是我们将数组遍历一篇,看一看如果当前这个数和下一个数不是递增关系,说明要有一个新的序列,所以如果没有不是有递增关系的,就输出 1(自己就是一个),如果有一个不是递增关系的就,输出 2,否则输出 3

最后贴上代码

#include <bits/stdc++.h>
#define int long long
#define endl '\n'

using namespace std;

const int N = 1e5 + 5;
int n, a[N];

signed main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }

    int k = 0; 
    for (int i = 1; i < n; i++) {
        if ((a[i] + 1) != a[i + 1]) {
            k++; //计算有几个不是递增关系的数
        }
    }

    if (k == 0) { //分支结构
        cout << 1;
    } else if (k == 1) {
        cout << 2;
    } else {
        cout << 3;
    }

    return 0;
}