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;
}

  

posted @ 2020-10-14 10:36  YukiRinLL  阅读(113)  评论(0编辑  收藏  举报