Inverted Deck

As a huge fan of the popular collectible card game Numinous Wilds: the Elven Reign Chronicles (NWERC), you have a large collection of cards which you carefully organise by their rarity. One day you notice that someone has touched your collection, and that some of the cards are now out of order. The most natural suspect, of course, is your little brother Billy, who was absolutely 100% forbidden from playing with your cards. After a few minutes of interrogation Billy confesses that he indeed took a few consecutive cards from the middle of the stack, but he swears that he put them back in exactly the same order as they were. You suspect that Billy, being so young, may have simply mistakenly reversed the order of the cards that he took. Now you want to check your theory and decide if you can find the batch of cards that Billy took to play with. 

Is it possible to restore the order of the cards in to non-decreasing order of their rarity by reversing just one contiguous batch of cards?

The input consists of: 

• One line containing an integer n (1 ≤ n ≤ 10^6), the number of cards in your collection. 

• One line containing n integers v1,...,vn (1 ≤ vi ≤ 10^9 for all i), the current order of the cards’ rarity values.

If the cards can be sorted by reversing exactly one contiguous subsequence of the list, then output the 1-based start and end indices of such a subsequence. Otherwise, output “impossible”. If there are multiple valid solutions you may output any one of them

样例输入1

7
10 13 19 19 15 14 20

样例输出1

3 6

样例输入2

6
9 1 8 2 7 3

样例输出2

impossible

样例输入3

3
1 2 3

样例输出3

2 2
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 998244353;
const int N = 1e6+5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m*n / gcd(m, n);
}

int main()
{
    int n;
    cin >> n;
    vector<int> a(n),b(n),res;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        b[i] = a[i];
    }
    int fg = 0;
    sort(b.begin(), b.end());
    for (int i = 0; i < n; i++)
    {
        if (a[i] != b[i])
        {
            res.push_back(i);
        }
    }
    if(res.size())
        reverse(a.begin()+res[0], a.begin()+res.back()+1);
    for (int i = 0; i < n; i++)
    {
        if (a[i] != b[i])
        {
            fg=1;
        }
    }
    if (fg)
    {
        cout << "impossible" << endl;
        return 0;
    }

    if (res.size() != 0)
        cout << res[0]+1 << " " << res.back()+1;
    else
        cout << 1 << " " << 1 << endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/dealer/p/12902716.html