hdu6215—Brute Force Sorting(双向链表)
来源:互联网 发布:淘宝儿童电动车 编辑:程序博客网 时间:2024/06/04 19:57
题目链接:传送门
Brute Force Sorting
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1849 Accepted Submission(s): 459
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[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 again.
Help Beerus predict the final array.
1.
2.
In
Help 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 than100000 .
The second line describes the array withN positive integers A[1],A[2],⋯,A[N] where each integer A[i] satisfies 1≤A[i]≤100000 .
For each test case, the first line provides the size of the inital array which would be positive and no bigger than
The second line describes the array with
Output
For eact test case output two lines.
The first line contains an integerM which is the size of the final array.
The second line containsM integers describing the final array.
If the final array is empty,M should be 0 and the second line should be an empty line.
The first line contains an integer
The second line contains
If the final array is empty,
Sample Input
551 2 3 4 555 4 3 2 151 2 3 2 151 3 5 4 252 4 1 3 5
Sample Output
51 2 3 4 5 021 2 21 3 32 3 5
Source
2017 ACM/ICPC Asia Regional Qingdao Online
解题思路:每删除一个数只会对这个数的前面那个数和后面那个数产生影响,用一个set将这些数保存起来(去重),再次对序列进行操作时,不需要再将整个序列扫描一遍,此时只需要对set里面的数进行判断是否合乎条件,这里用双向链表来实现。最多删除n个数,用set来保存被影响的数,时间复杂度尾nlogn。(链表都差点忘记写了)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <set>using namespace std;typedef long long ll;const int INF = 0x3fffffff;const int N = 100100;struct Node{ int pos,id; Node*pre,*next;}node[N];vector<int>vec;set<int>st;int tick[N];void Delete(int u){ Node*p = node[u].pre; p->next = node[u].next; node[u].next->pre = p;}int main(){ int T; scanf("%d",&T); while(T--){ memset(tick,0,sizeof(tick)); memset(node,0,sizeof(node)); vec.clear(); st.clear(); int n,p; scanf("%d",&n); node[0].pos = 0; node[0].id = 0; node[0].next = &node[1]; node[0].pre = NULL; for( int i = 1 ; i <= n ; ++i ){ scanf("%d",&node[i].pos); node[i].id = i; node[i].next = &node[i+1]; node[i].pre = &node[i-1]; } node[n+1].pos = INF; node[n+1].id = n+1; node[n+1].pre = &node[n]; node[n+1].next = NULL; tick[0] = tick[n+1] = 1; for( int i = 1 ; i <= n ; ++i ){ if(node[i].pos < node[i-1].pos || node[i].pos > node[i+1].pos){ vec.push_back(i); tick[i] = 1; } } int flag = 1; while(flag){ st.clear(); for( int i = 0 ; i < vec.size() ; ++i ){ int id1 = node[vec[i]].pre->id; int id2 = node[vec[i]].next->id; if(!tick[id1]) st.insert(id1); if(!tick[id2]) st.insert(id2); Delete(vec[i]); } vec.clear(); for(set<int>::iterator it = st.begin() ; it != st.end() ; ++it){ int p = *it; if(node[p].pos < node[p].pre->pos || node[p].pos > node[p].next->pos){ tick[p] = 1; vec.push_back(p); } } if(vec.size() == 0) flag = 0; } for( int i = 1 ; i <= n ; ++i ){ if(tick[i] == 0) vec.push_back(node[i].pos); } if(vec.size() == 0){ printf("0\n\n"); }else{ printf("%d\n",vec.size()); for( int i = 0 ; i < vec.size() ; ++i ){ printf("%d ",vec[i]); } printf("\n"); } } return 0;}
阅读全文
0 0
- hdu6215—Brute Force Sorting(双向链表)
- hdu6215 Brute Force Sorting 链表模拟
- HDU6215 Brute Force Sorting 【模拟】
- hdu6215 Brute Force Sorting 模拟题+想法题
- HDU 6215 Brute Force Sorting (链表维护数组)
- HDU 6215 Brute Force Sorting(伪双向链表删边)
- 【2017青岛网络赛】1010 hdu 6215 Brute Force Sorting 双向链表+队列
- hdu6215(双向链表)
- hdu 6215 Brute Force Sorting 模拟链表
- HDU-6215 Brute Force Sorting(思维、模拟链表)
- HDU 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- hdoj 6215 Brute Force Sorting
- HDU6215【队列+双向链表维护】
- HDU 6215 Brute Force Sorting 链表 + 队列(模拟)
- HDU 6215 Brute Force Sorting [链表]
- HDU 6215 Brute Force Sorting 模拟
- git命令
- python操作excel
- android:windowIsTranslucent 和 activity 动画关系
- Android studio 中使用Httpclient权限
- Image image = Toolkit.getDefaultToolkit().getImage("picture/bg.jpg");// 这种图片加载方法为什么要 先最小化窗口然后才会显示
- hdu6215—Brute Force Sorting(双向链表)
- nginx的安装及启动
- CentOS下配置typecho
- CAD文件转换成图片的方法分享
- BZOJ 1475 最小割 解题报告
- VMware Workstation 不可恢复错误 (vcpu-0)20171010
- 在刚开始创建链表的时候遇到的小问题
- maven入门
- 学习Retrofit+RXJava