Sequence in the Pocket

题意:给出一串序列,一个操作,把序列随便一个数移到最前面,问使这个序列有序要移动多少次。

思路:算是给思维题吧,比赛竟没有做出来,可能当时是发烧烧晕了,其实手动列几个就能发现规律。

#include <bits/stdc++.h>
#define MAXN 100000
using namespace std;
priority_queue <int> q;
int a[MAXN + 5];
int T;
int n;
int main() {
    int ans;
    scanf("%d", &T);
    for (int t = 1; t <= T; ++t) {
        ans = 0;
        while (!q.empty())
            q.pop();
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            q.push(a[i]);
        }
        int last = n + 1;
        for (int i = n; i; --i) {
            if (a[i] == q.top()) {
                ans += last - i - 1;
                last = i;
                q.pop();
            }
        }
        ans += last - 1;
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Endeavor_G/article/details/89633600