codeforce 847B Preparing for Merge Sort
来源:互联网 发布:中国运营商网络制式 编辑:程序博客网 时间:2024/06/06 03:35
题目链接:点击打开链接
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure:
- iterate through array from the left to the right;
- Ivan only looks at unused numbers on current iteration;
- if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers[1, 3, 5], and on second one — [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — Ivan's array.
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
51 3 2 5 4
1 3 5 2 4
44 3 2 1
4 3 2 1
410 30 50 101
10 30 50 101方法:二分,暴力超时,二分思路比较巧妙
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;const int N=2e5+5;int a[N],b[N];vector<int>vee[N];int serch(int l,int r,int x){ while(l<r) { int mid=l+(r-l)/2; if(b[mid]>=x) r=mid; else l=mid+1; } return l-1;}int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&a[i]); int pos=serch(0,n,a[i]); b[pos]=a[i]; vee[pos].push_back(a[i]); } for(int i=n-1;i>=0;i--) { for(int j=0;j<vee[i].size();j++) { if(j==vee[i].size()-1) printf("%d\n",vee[i][j]); else printf("%d ",vee[i][j]); } }}//直接用函数#include<stdio.h>#include<string.h>#include<algorithm>#include<algorithm>#include<vector>using namespace std;const int maxn = 1e5 + 5;int a[2 * maxn],b[2 * maxn];vector<int>G[2 * maxn];int main(){ int n; scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",&a[i]); int pos = lower_bound(b,b + n,a[i]) - b; pos--; b[pos] = a[i]; G[pos].push_back(a[i]); } for(int i = n-1;i >= 0;i--){ for(int j = 0;j < G[i].size();j++){ if(j == G[i].size() - 1) printf("%d\n",G[i][j]); else printf("%d ",G[i][j]); } }}
- codeforce 847B Preparing for Merge Sort
- Codeforces 847B Preparing for Merge Sort
- B. Preparing for Merge Sort
- Codeforces 847 B. Preparing for Merge Sort (二分)
- B. Preparing for Merge Sort(二分)
- 【二分优化】Preparing for Merge Sort CodeForces
- 2017-2018 ACM-ICPCNEERC : Preparing for Merge Sort
- codeforce B. Preparing Olympiad (DFS+回溯)/(暴力状压)
- codeforce-B. Sort the Array
- CodeForces 377 B. Preparing for the Contest
- codeforces 377B Preparing for the Contest
- CodeForces 377B Preparing for the Contest
- B. Preparing for the Contest----二分+贪心
- codeforce 451B Sort the Array
- Codeforces 377B . Preparing for the Contest【优先队列】
- CodeForces 377B---Preparing for the Contest(二分+贪心)
- Codeforces Round #222 (Div. 1) B - Preparing for the Contest
- CodeForces 377B-Preparing for the Contest(二分+贪心)
- cocos2d-js 分享截屏-js代码
- Fastjson
- Android 使用RenderScript 实现毛玻璃(模糊)
- python使用xlrd读取excel数据时,整数和日期变小数的解决办法
- Android从入门到转行之登陆
- codeforce 847B Preparing for Merge Sort
- Linux PCB,process descriptor,task_struct,thread_info区别与联系
- USB驱动开发之USB四种传输模式
- HADOOP编译WordCount.java文件报错
- Modbus协议完全资料与程序解析
- 第八届蓝桥杯大赛个人赛省赛(软件类)真题
- QMessageBox修改字体大小和button大小
- Android开发之RecyclerView(一)初步使用
- js二分法查找元素在排好序的数组下标