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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值