Codeforces847B Preparing forMerge Sort

标签:递推,DP

Ivan hasan array consisting of n differentintegers. He decided to reorder all elements in increasing order. Ivan lovesmerge sort so he decided to represent his array with one or several increasingsequences which he then plans to merge into one sorted array.

Ivanrepresent his array with increasing sequences with help of the followingalgorithm.

Whilethere is at least one unused number in array Ivan repeats the followingprocedure:

·  iterate through array from the left to theright;

·  Ivan only looks at unused numbers on currentiteration;

·  if current number is the first unused numberon this iteration or this number is greater than previous unused number oncurrent iteration, then Ivan marks the number as used and writes it down.

Forexample, if Ivan's array looks like [1, 3,2, 5, 4] then he will perform two iterations. On first iterationIvan will use and write numbers [1, 3, 5], and onsecond one — [2, 4].

Write aprogram which helps Ivan and finds representation of the given array with oneor several increasing sequences in accordance with algorithm described above.

Input

The firstline contains a single integer n (1 ≤ n ≤ 2·105) — the number ofelements in Ivan's array.

The secondline contains a sequence consisting of distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — Ivan'sarray.

Output

Printrepresentation of the given array in the form of one or more increasingsequences in accordance with the algorithm described above. Each sequence mustbe printed on a new line.

Example

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

类似于noip1999拦截导弹那题

为了防止TLE,只好先前加入判断了,这样可以避免一部分的双重循环,时间复杂度O(n*cnt)

//cnt代表输出答案的行数

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
 
inline int read()
{
       intf=1,x=0;char ch=getchar();
       while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
       while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
       returnx*f;
}
struct data{int num,x;}a[200005];
int n,cnt=1,Max[200005];
 
inline int cmp(data a,data b)
{
   if(a.num==b.num)return a.x<b.x;
       elsereturn a.num<b.num;
}
 
int main()
{
       n=read();
       rep(i,1,n)a[i].x=read();
       rep(i,1,n){
              if(a[i].x<Max[cnt]){Max[++cnt]=a[i].x;a[i].num=cnt;continue;}
              rep(j,1,cnt)if(a[i].x>Max[j]){Max[j]=a[i].x;a[i].num=j;break;}
       }
       sort(a+1,a+1+n,cmp);
       printf("%d",a[1].x);
       rep(i,2,n){
              if(a[i].num!=a[i-1].num)printf("\n");
              printf("%d",a[i].x);
       }
       return0;
}


### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值