HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

Brute Force Sorting

Time Limit: 1 Sec  Memory Limit: 128 MB

题目连接

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

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[i-1]. A[i−1].
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 againHelp Beerus predict the final array.

input

The first line of input contains an integer T(1<=T<=10) 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  1000.
The second line describes the array with N positive integers A[1],A[2],...,A[N] where each integer A[i] satisfies 1<=A[i]<=10000.0.

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 Mintegers 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

HINT

题意

有一个长度为n序列,如果第i(1<=i<=n)位上的值ai<ai-1 || ai>ai+1那么这一位需要被删除。

删除完后,再重复以上操作,直到序列单调不降。

题解:

先考虑暴力,即每次扫一遍数组,删除该删的数,直到不能删为止。

肯定有很多数扫过一遍第二次就可以不用扫了,顺着这个方向想,我们怎么节省扫描次数呢。

我们假设第一次删除了一些数,有些连着被删除的数,我将其称为一段(一个数也算一段),那么我们只需要记住每一段被删除的数的前一个数(未删除的数)即可,(想一想为什么)

将其存入一个队列,下次就直接扫这个队列即可。

具体实践,我们可以用链表记录每个位置的前一个未删除的位置以及后一个未删除的位置。

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define N 1000050
 4 int a[N],n,last[N],nex[N],f[N];
 5 template<typename T>void read(T&x)
 6 {
 7   int k=0;char c=getchar();
 8   x=0;
 9   while(!isdigit(c)&&c!=EOF)k^=c==‘-‘,c=getchar();
10   if(c==EOF)exit(0);
11   while(isdigit(c))x=x*10+c-‘0‘,c=getchar();
12   x=k?-x:x;
13 }
14
15 void work()
16 {
17   read(n);
18   nex[0]=1;
19   for(int i=1;i<=n;i++)
20     read(a[i]),nex[i]=i+1,last[i]=i-1;
21   a[n+1]=123456789;
22   int sum=0;
23   int k=0,flag=0;
24   memset(f,0,sizeof(f));
25   queue<int> Q[2];
26   while(!Q[k].empty())Q[k].pop();
27   while(!Q[1-k].empty())Q[1-k].pop();
28   for(int i=1;i<=n;i++)Q[k].push(i);
29   while(1)
30     {
31       flag=0;
32       //Q[1-k].clear();
33       while(!Q[k].empty())
34     {
35       int x=Q[k].front();Q[k].pop();
36       if (a[x]>a[nex[x]])
37         {
38           //sum+=f[x]==0+f[nex[x]]==0;
39           f[x]=1; f[nex[x]]=1;
40           flag=1;
41           nex[last[x]]=nex[nex[x]];
42           last[nex[nex[x]]]=last[x];
43           last[nex[x]]=last[x];
44           if (f[last[x]]==0&&(Q[1-k].empty()||Q[1-k].back()!=last[x]))Q[1-k].push(last[x]);
45         }
46     }
47       if (flag==0)break;
48       k=k^1;
49     }
50   for(int i=1;i<=n;i++)
51     {
52       if(f[i]==0)sum++;
53     }
54   printf("%d\n",sum);
55   for(int i=1;i<=n;i++)
56     {
57       if(f[i]==0)
58       printf("%d ",a[i]);
59     }
60   printf("\n");
61 }
62 int main()
63 {
64 #ifndef ONLINE_JUDGE
65   freopen("aa.in","r",stdin);
66 #endif
67   int q;
68   read(q);
69   while(q--)
70     {
71       work();
72     }
73   return 0;
74 }

原文地址:https://www.cnblogs.com/mmmqqdd/p/10739456.html

时间: 2024-07-30 14:01:14

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting的相关文章

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Qingdao Online 1011 A Cubic number and A Cubic Number

A Cubic number and A Cubic Number Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0 Problem Description A cubic number is the result of using a whole number in a mul

HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)

思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定. 快速幂入门第一题QAQ #include <stdio.h> #include <stdlib.h> #include <cmath> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #inc

2017 ACM/ICPC Asia Regional Qingdao Online

The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6778    Accepted Submission(s): 713 Problem Description Here you have a set of strings. A dominator is a string of the se

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 406 Problem Description I will show you the most popular board game in the Shanghai Ingress Resis

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is