zoukankan      html  css  js  c++  java
  • CF AIM Tech Round 4 C. Sorting by Subsequences

    题目链接http://codeforces.com/contest/844/problem/C

    题目大意:给出一个长度为n的序列a[n],问如果将其分为K个子序列,要求每个子序列排序之后整个序列也是有序的,那么最大的K是多少。

    解题思路:首先我们考虑当前序列中和排序后的序列中对应位置相同的元素,那么显然,为了K最大,我们使每个单独成一个子序列;

    现在考虑不同的元素,对于如下序列(下标--排序后--原序列):

    1  2  3  4  5  6 

    2  6  7  9  12   13

    9   13 2  7  12  6

    我们知道1 4 3;2 6(下标)号元素排序后与整个序列排序后相同,简单思考一下会发现实际上对于1号元素,我们只要找9对应的排序后的序列的元素下标4,然后判断其是否为2,如果不为2,继续找7对应的下标,直到其为2,那么这个序列排序后与整个序列排序后一定是一样的。

    代码:

     1 const int maxn = 1e5 + 5;
     2 int n;
     3 int a[maxn], as[maxn];
     4 queue<int> qs[maxn];
     5 int tot;
     6 bool vis[maxn];
     7 
     8 void solve(){
     9     tot = 0;
    10     memset(vis, 0, sizeof(vis));
    11     for(int i = 0; i < n; i++) as[i] = a[i];
    12     sort(as, as + n);
    13     for(int i = 0; i < n; i++){
    14         if(as[i] == a[i]){
    15             vis[i] = 1;
    16             qs[tot].push(i);
    17             tot++;
    18         }
    19     }
    20     queue<int> tmq;
    21     for(int i = 0; i < n; i++){
    22         if(vis[i]) continue;
    23         int tma = as[i], j = lower_bound(as, as + n, a[i]) - as;
    24         tmq.push(i);
    25         tmq.push(j);
    26         vis[j] = 1;
    27         while(a[j] != tma){
    28             j = lower_bound(as, as + n, a[j]) - as;
    29             tmq.push(j);
    30             vis[j] = 1;
    31         }
    32         qs[tot++] = tmq;
    33         while(!tmq.empty()) tmq.pop();
    34     }
    35     printf("%d
    ", tot);
    36     for(int i = 0; i < tot; i++){
    37         printf("%d", qs[i].size());
    38         while(!qs[i].empty()){
    39             printf(" %d", qs[i].front() + 1);
    40             qs[i].pop();
    41         }
    42         puts("");
    43     }
    44 }
    45 int main(){
    46     scanf("%d", &n);
    47     for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    48     solve(); 
    49 }

    题目:

    C. Sorting by Subsequences
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a sequence a1, a2, ..., an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order.

    Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.

    Every element of the sequence must appear in exactly one subsequence.

    Input

    The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence.

    The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.

    Output

    In the first line print the maximum number of subsequences k, which the original sequence can be split into while fulfilling the requirements.

    In the next k lines print the description of subsequences in the following format: the number of elements in subsequence ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ n) — indices of these elements in the original sequence.

    Indices could be printed in any order. Every index from 1 to n must appear in output exactly once.

    If there are several possible answers, print any of them.

    Examples
    input
    6
    3 2 1 6 5 4
    output
    4
    2 1 3
    1 2
    2 4 6
    1 5
    input
    6
    83 -75 -49 11 37 62
    output
    1
    6 1 2 3 4 5 6
    Note

    In the first sample output:

    After sorting the first subsequence we will get sequence 1 2 3 6 5 4.

    Sorting the second subsequence changes nothing.

    After sorting the third subsequence we will get sequence 1 2 3 4 5 6.

    Sorting the last subsequence changes nothing.

  • 相关阅读:
    分期付款购买固定资产账务处理
    会计要素计量
    接受现金捐赠分录
    分配股票股利的分录
    R语言代写对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归
    R语言代写用随机森林和文本挖掘提高航空公司客户满意度
    R语言代写时间序列TAR阈值模型分析 2
    R语言代写时间序列TAR阈值模型分析
    R语言代写文本挖掘tf-idf,主题建模,情感分析,n-gram建模研究
    R语言代写文本挖掘NASA数据网络分析,tf-idf和主题建模
  • 原文地址:https://www.cnblogs.com/bolderic/p/7467267.html
Copyright © 2011-2022 走看看