• HDU 6215 Brute Force Sorting(链表)


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

    题意:
    给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去。然后继续去判断剩下的数,直到最后都满足。

    思路:

    建立双向链表,如果一个数是需要删除的,那么它只会影响它前一个的数和后一个的数,这样只需要把它前面的数保存下来,下次再跑即可。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<sstream>
     6 #include<vector>
     7 #include<stack>
     8 #include<queue>
     9 #include<cmath>
    10 #include<map>
    11 #include<set>
    12 using namespace std;
    13 typedef long long ll;
    14 typedef pair<int,ll> pll;
    15 const int INF = 0x3f3f3f3f;
    16 const int maxn = 100000+5;
    17 
    18 int n ,top;
    19 int a[maxn],pre[maxn],nxt[maxn];
    20 int que[maxn];
    21 
    22 template <class T>
    23 inline void scan_d(T &ret)
    24 {
    25     char c;
    26     ret = 0;
    27     while ((c = getchar()) < '0' || c > '9');
    28     while (c >= '0' && c <= '9')
    29     {
    30         ret = ret * 10 + (c - '0'), c = getchar();
    31     }
    32 }
    33 
    34 int main()
    35 {
    36     //freopen("in.txt","r",stdin);
    37     int T;
    38     scan_d(T);
    39     while(T--)
    40     {
    41         top=0;
    42         scan_d(n);
    43         for(int i=1;i<=n;i++)
    44         {
    45             scan_d(a[i]);
    46             pre[i]=i-1;
    47             nxt[i]=i+1;
    48             que[top++]=i;
    49         }
    50         a[0]=0,a[n+1]=INF;
    51         nxt[0]=1,pre[n+1]=n;
    52         bool flag = true;
    53         int ans = n;
    54         while(flag)
    55         {
    56             int s = 0;
    57             flag = false;
    58             int now = 0;
    59             while(now<top)
    60             {
    61                 int cnt = 0;
    62                 int it = que[now];
    63                 while(a[it]>a[nxt[it]])  {cnt++;it=nxt[it];flag=true;}
    64                 if(cnt)
    65                 {
    66                     ans-=(cnt+1);
    67                     nxt[pre[que[now]]]=nxt[it];
    68                     pre[nxt[it]]=pre[que[now]];
    69                     que[s++]=pre[que[now]];;
    70                 }
    71                 while(que[now]<=it && now<top) now++;
    72             }
    73             top=s;
    74         }
    75         printf("%d
    ",ans);
    76         for(int i=0;nxt[i]!=n+1;i=nxt[i])
    77             printf("%d ",a[nxt[i]]);
    78         printf("
    ");
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    Nginx有哪些作用?
    MYSQL如何优化?
    jdk1.8新特性
    [javase基础] JDK JRE JVM的区别?
    JDBC中如何进行事务处理?
    JDBC、ibatis(mybatis)、Hibernate有什么不同?
    java面试题最容易犯错
    Spring高频率面试题
    python pip whl安装和使用
    深入理解 Linux的进程,线程,PID,LWP,TID,TGID
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7544827.html
Copyright © 2020-2023  润新知