zoukankan      html  css  js  c++  java
  • hdu6215 Brute Force Sorting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215

    题目:

    Brute Force Sorting

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 496    Accepted Submission(s): 119


    Problem Description
    Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
    1. A[i] is the first element of the array, or it is no smaller than the left one A[i1].
    2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
    In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
    Help Beerus predict the final array.
     
    Input
    The first line of input contains an integer T (1T10) which is the total number of test cases.
    For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
    The second line describes the array with N positive integers A[1],A[2],,A[N] where each integer A[i] satisfies 1A[i]100000.
     
    Output
    For eact test case output two lines.
    The first line contains an integer M which is the size of the final array.
    The second line contains M integers describing the final array.
    If the final array is empty, M should be 0 and the second line should be an empty line.
     
    Sample Input
    5 5 1 2 3 4 5 5 5 4 3 2 1 5 1 2 3 2 1 5 1 3 5 4 2 5 2 4 1 3 5
     
    Sample Output
    5 1 2 3 4 5 0 2 1 2 2 1 3 3 2 3 5
     
    Source
     
    思路:
      直接模拟多次扫数组肯定不行,TLE。
      仔细观察后发现在删一个数后v[i],在下一次的扫描中只会影响v[i]的前一个数和后一个数,所以我们可以用双向链表做。
      每次把被影响的数保存起来,然后从这些数开始扫,用链表模拟删除操作,把删除数的前一个数留到下次扫描。
      因为被删除的数最多只有n个,所以时间复杂度O(n)。
      我因为用了set来去重,时间复杂度变成了O(nlogn),不过也可以不用set,我只是为了好写。
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int now,pre,v[K],pe[K],nt[K],dl[K];
    15 set<int>st;
    16 vector<int>tmp;
    17 
    18 int main(void)
    19 {
    20     //freopen("in.acm","r",stdin);
    21     int t,n;cin>>t;
    22     while(t--)
    23     {
    24         memset(dl,0,sizeof dl);
    25         scanf("%d",&n);
    26         for(int i=1;i<=n;i++)   dl[i]=0,scanf("%d",v+i),pe[i]=i-1,nt[i]=i+1,st.insert(i);
    27         nt[0]=1,pe[n+1]=n,pe[1]=0,nt[n]=n+1;
    28         v[0]=0,v[n+1]=K;
    29         while(st.size())
    30         {
    31             tmp.clear();
    32             for(auto &x:st)
    33             {
    34                 int ntx=nt[x],px=pe[x];
    35                 if(v[px]>v[x])  tmp.PB(px),tmp.PB(x);
    36                 if(v[x]>v[ntx]) tmp.PB(x),tmp.PB(ntx);
    37             }
    38             st.clear();
    39             for(auto &x:tmp)
    40             if(!dl[x])
    41             {
    42                 int ntx=nt[x],px=pe[x];
    43                 nt[px]=ntx,pe[ntx]=px;
    44                 st.insert(px);
    45                 dl[x]=1;
    46             }
    47         }
    48         int cnt=0;
    49         for(int i=nt[0];i!=n+1;i=nt[i])   cnt++;
    50         printf("%d
    ",cnt);
    51         for(int i=nt[0];i!=n+1;i=nt[i])   printf("%d ",v[i]);
    52         printf("
    ");
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    bzoj2124 等差子序列(树状数组+hash)
    CF817F MEX Queries(线段树上二分)
    [USACO12MAR]摩天大楼里的奶牛(状态压缩DP)
    CF786B Legacy(线段树优化建图)
    绿豆蛙的归宿
    单选错位
    聪聪和可可
    Tyvj1952 Easy
    OSU!
    弱题
  • 原文地址:https://www.cnblogs.com/weeping/p/7542009.html
Copyright © 2011-2022 走看看