codeforces/2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest, qualification stage (B)

    Preparing for Merge Sort

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的序列(数值全部都不相同),然后从第一个开始向后遍历,找到第一个比其打的第一个数并标记他,然后再从这个数
开始向后遍历,直到找不到比大的数,在从没有标记的数开始重复。。。直到,所有数都标记,
这样 就会得到很多段有序的序列,这就是要求的ans;
eg:
5
1 3 2 5 4
从1开始,从1开始向后遍历,比1大的第一个数是3,从3开始向后遍历,比3大的第一个数是5,从5开始向后遍历,没有比5大的数了,故:第一段序列为:1 3 5;从没有使用过的第一个数开始向后遍历,1,3,被用过了,因此从2开始,比2大的第一个是5,但是5已经被使用了,因此是4;故第二段
序列:2 4 ;所以ans就是:
1 3 5
2 4
解法:

这个题很容易想到暴力(复杂度 n^2),如果是随机数据就可以过,但是如果整个序列都是逆序的,要被卡;现在说正解:我用vectorve[maxn],来存放每一段序列,那我假设,在取得i位置的时候,vector中的序列都是对的,那么问题的关键就是将i位置的值放到哪个vector中,很容易想到在vector中 i的前一个位置的值(j) ,一定比i位置的值小,而且j位置的值,是在i以前所有元素中,第一个比i位置的值小的,那么,问题就转化为找第一个比i小的j在 哪个vector中,然后push_back即可,那么就需要维护vector的最后的那个元素;

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 9;
vector<int>ve[maxn];
set<int>se;
map<int, int>mp;
int num = 1;//记录vector的个数
void add(int val)
{
    auto it = se.lower_bound(val);//找到比val大的第一个元素
    if (it == se.begin())//val是整个ve中的最小的那个,创建一个新的ve
    {
        ve[num].push_back(val);
        se.insert(val);//用set维护ve中(*rbegin)的值
        mp[val] = num;//用map维护(*rbegin)所对应的num值
        num++;
    }
    else
    {
        it--;//it是比val大的第一个值,it--,就是比val小的第一个值
        int temp = *it;
        ve[mp[temp]].push_back(val);//mp[temp]为temp所在的vector的num值
        mp[val] = mp[temp];//将val对应
        mp.erase(temp); //可以不写
        se.erase(temp);se.insert(val);//temp所在的ve的最后元素更新为val
    }
}
int main()
{
    int n; num = 1;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        int x;
        scanf("%d",&x);
        add(x);
    }
    for (int i = 1; i < num; ++i)
    {
        for (auto it : ve[i])printf("%d ", it);
        puts("");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值