zoukankan      html  css  js  c++  java
  • CodeForces#631Div.2B_Dreamoon Likes Permutations

    网址:https://codeforces.ml/contest/1330/problem/B
    题目描述:The sequence of m integers is called the permutation if it contains all integers from 1 to m exactly once. The number m is called the length of the permutation.
    Dreamoon has two permutations p1 and p2 of non-zero lengths l1 and l2.
    Now Dreamoon concatenates these two permutations into another sequence a of length l1+l2. First l1 elements of a is the permutation p1 and next l2 elements of a is the permutation p2.
    You are given the sequence a, and you need to find two permutations p1 and p2. If there are several possible ways to restore them, you should find all of them. (Note that it is also possible that there will be no ways.)
    输入:
    Input
    The first line contains an integer t (1≤t≤10000) denoting the number of test cases in the input.
    Each test case contains two lines. The first line contains one integer n (2≤n≤200000): the length of a. The second line contains n integers a1,a2,…,an (1≤ai≤n−1).
    The total sum of n is less than 200000.
    输出:
    Output
    For each test case, the first line of output should contain one integer k: the number of ways to divide a into permutations p1 and p2.
    Each of the next k lines should contain two integers l1 and l2 (1≤l1,l2≤n,l1+l2=n), denoting, that it is possible to divide a into two permutations of length l1 and l2 (p1 is the first l1 elements of a, and p2 is the last l2 elements of a). You can print solutions in any order.
    Example
    inputCopy
    6
    5
    1 4 3 2 1
    6
    2 4 1 3 2 1
    4
    2 1 1 3
    4
    1 3 3 1
    12
    2 1 3 4 5 6 7 8 9 1 10 2
    3
    1 1 1
    outputCopy
    2
    1 4
    4 1
    1
    4 2
    0
    0
    1
    2 10
    0
    Note
    In the first example, two possible ways to divide a into permutations are {1}+{4,3,2,1} and {1,4,3,2}+{1}.
    In the second example, the only way to divide a into permutations is {2,4,1,3}+{2,1}.
    In the third example, there are no possible ways.

    题解:这道题需要看出数学规律,没有看出数学规律来基本很难做,我之前写的都是超时。
    每个样例最多存在2种可能。找出所有元素的最大值,maxn;只可能是maxn,n-maxn;或者n-maxn,maxn;
    明白了这个规律,就能在O(n)时间内完成。
    AC代码:

    include

    include

    using namespace std;
    const int mm=200005;
    int a[mm];
    bool Check(int x,int y){
    int b[mm]={0},c[mm]={0};
    for(int i=1;i<=x;i++) b[a[i]]++;
    for(int i=x+1;i<=x+y;i++) c[a[i]]++;
    for(int i=1;i<=x;i++){
    if(b[i]!=1) return false;
    }
    for(int i=1;i<=y;i++){
    if(c[i]!=1) return false;
    }
    return true;
    }
    int main(){
    int t,n;
    cin>>t;
    while(t--){
    memset(a,0,sizeof(a));
    cin>>n;
    int maxn=0;
    for(int i=1;i<=n;i++){
    cin>>a[i];
    if(a[i]>maxn) maxn=a[i];
    }
    int l1=maxn,l2=n-maxn;
    int s=0,s1=0,s2=0;
    if(Check(l1,l2)) s++,s1=1;
    if(Check(l2,l1)) s++,s2=1;
    if(s2&&l1l2){
    cout<<1<<endl;
    cout<<l1<<" "<<l2<<endl;
    continue;
    }
    cout<<s<<endl;
    if(s1) cout<<l1<<" "<<l2<<endl;
    if(s2) cout<<l2<<" "<<l1<<endl;
    }
    return 0;
    }
    写于2020/04/04 23:11 星期六


    作者:孙建钊
    出处:http://www.cnblogs.com/sunjianzhao/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    不用keytool,tomcat打开https
    sqlserver获取某一张表中的所有列中的最大长度
    不用keytool,tomcat打开https
    到底私钥和公钥哪个是用来加密 哪个是用来解密的
    空间支持php解压
    到底私钥和公钥哪个是用来加密 哪个是用来解密的
    sqlserver获取某一张表中的所有列中的最大长度
    数字签名(代码签名)流程
    功夫电影中非常经典(武术非常实用)
    数字签名(代码签名)流程
  • 原文地址:https://www.cnblogs.com/sunjianzhao/p/12635070.html
Copyright © 2011-2022 走看看