zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    flask 使用Flask-SQLAlchemy管理数据库(连接数据库服务器、定义数据库模型、创建库和表) --
    flask 操作数据库(分类) --
    flask渲染模板时报错TypeError: 'UnboundField' object is not callable --
    flask用宏渲染表单模板时,表单提交后,如果form.validate_on_submit()返回的是false的可能原因 --
    flask 单个页面多个表单(单视图处理、多视图处理) --
    flask 单个表单多个提交按钮 --
    jython 2.7.1 版本开发历史
    TP v5中Url Compat模式
    乱弹
    改改"坏"代码
  • 原文地址:https://www.cnblogs.com/dealer/p/12902716.html
Copyright © 2011-2022 走看看