Sequence in the Pocket ZOJ - 4104 (待解决

https://vjudge.net/problem/ZOJ-4104/origin

https://vjudge.net/contest/395635#problem/E

DreamGrid has just found an integer sequence a_1, a_2, \dots, a_na1,a2,,an in his right pocket. As DreamGrid is bored, he decides to play with the sequence. He can perform the following operation any number of times (including zero time): select an element and move it to the beginning of the sequence.

What's the minimum number of operations needed to make the sequence non-decreasing?

Input

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1 \le n \le 10^51n105), indicating the length of the sequence.

The second line contains nn integers a_1, a_2, \dots, a_na1,a2,,an (1 \le a_i \le 10^91ai109), indicating the given sequence.

It's guaranteed that the sum of nn of all test cases will not exceed 10^6106.

Output

For each test case output one line containing one integer, indicating the answer.

Sample Input
2
4
1 3 2 4
5
2 3 3 5 5

Sample Output
2
0

Hint

For the first sample test case, move the 3rd element to the front (so the sequence become {2, 1, 3, 4}), then move the 2nd element to the front (so the sequence become {1, 2, 3, 4}). Now the sequence is non-decreasing.

For the second sample test case, as the sequence is already sorted, no operation is needed.

附未通过代码方便日后补题:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iomanip>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
#define debug(x) cout<<"debug:"<<#x<<" = "<<x<<endl;

#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db;

const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=1e6+10;
const int mod=1e9+7;

ll a[100005] = {0};
int main()
{
    int t = 0;
    cin >> t;
    while(t--)
    {
        int n = 0;
        cin >> n;
        int counter = 0;
        int flag = 0;
        for(int i = 0;i<n;i++)
        {
            cin >> a[i];
        }
        if(a[1]>a[0] && a[1]>a[2])
        {
            flag = 1;
            //cout <<1<< " !A\n";
        }
        for(int i = 2;i<n-2;i++)
        {
            if(a[i]>a[i-1] && a[i]>a[i+1])
            {
                if(flag < 2)
                {
                    if(a[i] == a[i-2])
                    {
                        counter -= 1;
                        if(flag == 1)counter += 1;
                        a[i-1] = a[i];
                        flag = 2;
                        //cout <<i<< " !1\n";
                        continue;
                    }
                    if(a[i] == a[i+2])
                    {
                        if(flag == 1)counter += 1;
                        a[i+1] = a[i];
                        flag = 2;
                        //cout <<i<< " !2\n";
                        continue;
                    }
                }

                if(flag < 1)
                {
                    if(a[i-1]<a[i-2] && a[i-1]<a[i+1] && a[i+1]<a[i+2])
                    {
                        counter -= 1;
                        a[i] = a[i-1];
                        flag = 1;
                        //cout <<i<< " !3\n";
                        continue;
                    }
                    if(a[i+1]<a[i+2] && a[i+1]<a[i-1] && a[i-1]<a[i-2])
                    {
                        counter -= 1;
                        a[i] = a[i+1];
                        flag = 1;
                        //cout <<i<< " !4\n";
                        continue;
                    }
                    if(a[i-1] == a[i-2] || a[i-1] == a[i+1] || a[i+1] == a[i+2])
                    {
                        a[i] = a[i-1];
                        flag = 1;
                        //cout <<i<< " !5\n";
                        continue;
                    }
                   
                }
                counter += 1;
                //cout <<i<< " !++\n";
            }
        }
        if(a[n-2]>a[n-1] && a[n-2]>a[n-3] && n!=3)
        {
            counter += 1;
            //cout <<n-2<< " !B\n";
        }
        cout << counter << endl;
    }
   
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YukiRinLL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值