ZOJ4104 Sequence in the Pocket

题意 给出n个数 每次将一个数移到第一位 问最少多少次能使这n个数递增

思维题 开一个数组排序为其最终状态 然后从后往前遍历 如果不相同的话 就说明调换到第一个位置去过 之后的话就要考虑之前移动的数对其的影响 举个例子 移动了第四个数到首位 那么 原来的第二个数就变成了第三个数

AC代码:

 1 #include<bits/stdc++.h>
 2 #define pi acos(-1)
 3 #define INF 0x3f3f3f3f
 4 typedef long long ll;
 5 typedef unsigned long long ull;
 6 using namespace std;
 7 
 8 namespace io {
 9     const int SIZE = 1e7 + 10;
10     char inbuff[SIZE];
11     char *l, *r;
12     inline void init() {
13         l = inbuff;
14         r = inbuff + fread(inbuff, 1, SIZE, stdin);
15     }
16     inline char gc() {
17         if (l == r) init();
18         return (l != r) ? *(l++) : EOF;
19     }
20     void read(int &x) {
21         x = 0; char ch = gc();
22         while(!isdigit(ch)) ch = gc();
23         while(isdigit(ch)) x = x * 10 + ch - '0', ch = gc();
24     }
25 } using io::read;
26 
27 bool cmp(const int &a, const int &b){
28     return a > b;
29 }
30 
31 int t, n;
32 int a[100005], b[100005];
33 
34 int main(){
35     ios::sync_with_stdio(false);
36     cin>>t;
37     while (t--){
38         cin>>n;
39         memset(a, 0, sizeof(a));
40         memset(b, 0, sizeof(b));
41         for (int i = 1; i <= n; i++){
42             cin>>a[i];
43             b[i] = a[i];
44         }
45         sort(b + 1, b + n + 1);
46         int ans = 0;
47         for (int i = n; i >= 1; i--)
48             if (a[i] != b[i + ans])
49                 ans++;
50         cout<<ans<<endl;
51     }
52     return 0;
53 }
原文地址:https://www.cnblogs.com/Misuchii/p/10983677.html