B. Preparing for Merge Sort(二分)
2017-09-21 21:25
387 查看
2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (Online Mirror, ACM-ICPC Rules, Teams Preferred)
B. Preparing for Merge Sorttime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Input
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.
Output
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.
Examples
input
5
1 3 2 5 4
output
1 3 5
2 4
input
4
4 3 2 1
output
4
3
2
1
input
4
10 30 50 101
output
10 30 50 101
题意:给你一个长度为n的序列,用一个或几个递增序表示给定的序列。
解题思路:二分查找。
#include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int a[2 * maxn],b[2 * maxn]; vector<i 4000 nt>G[2 * maxn]; int main(){ int n; scanf("%d",&n); for(int i = 1;i <= n;i++){ scanf("%d",&a[i]); int pos = lower_bound(b + 1,b + 1 + n,a[i]) - b; pos--; b[pos] = a[i]; G[pos].push_back(a[i]); } for(int i = n;i >= 1;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]); } } } /*lower_bound(b + 1,b + 1 + n,a[i]) - b: 返回b序列中第一个大于等于a[i]的下标,若b序列中的数都小于a[i],返回 n + 1。*/ /*upper_bound(b + 1,b + 1 + n,a[i]) - b: 返回b序列中第一个大于a[i]的下标,若b序列中的数都小于a[i],返回 n + 1。*/
相关文章推荐
- Codeforces 847 B. Preparing for Merge Sort (二分)
- 【二分优化】Preparing for Merge Sort CodeForces - 847B
- 2017-2018 ACM-ICPCNEERC : Preparing for Merge Sort
- B. Preparing for Merge Sort
- CodeForces 377B---Preparing for the Contest(二分+贪心)
- [LeetCode] merge sort for linked list
- Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树
- Data Structure Linked List: Merge Sort for Linked Lists
- 链表的归并排序 特殊优化, merge sort for list,非快慢指针法
- CodeForces 377B-Preparing for the Contest(二分+贪心)
- CodeForces 377B---Preparing for the Contest(二分+贪心)
- B. Preparing for the Contest----二分+贪心
- Codeforces 609D Gadgets for dollars and pounds 二分答案
- 2016Qingdao网络预选赛1007 / HDU5884 Sort(二分)
- 排序算法之归并排序(Mergesort)解析
- Java for LeetCode 023 Merge k Sorted Lists
- Error:Execution failed for task ':app:mergeDebugResources'.
- 【从零学习经典算法系列】分治策略实例——归并排序(Mergesort)
- C program for quick sort
- Merge Sort (归并排序)