A8055 | Permutations
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. For example, $(1)$ , $(4,3,5,1,2)$ , $(3,2,1)$ are permutations, and $(1,1)$ , $(4,3,1)$ , $(2,3,4)$ are not.
There are many tasks on permutations. Today you are going to solve one of them. Let’s imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
There are many tasks on permutations. Today you are going to solve one of them. Let’s imagine that somebody took several permutations (perhaps, with a different number of elements), wrote them down consecutively as one array and then shuffled the resulting array. The task is to restore the initial permutations if it is possible.
输入格式
The first line contains an integer $n$ ( $1<=n<=10^{5}$ ). The next line contains the mixed array of $n$ integers, divided with a single space. The numbers in the array are from $1$ to $10^{5}$ .
输出格式
If this array can be split into several permutations so that every element of the array belongs to exactly one permutation, print in the first line the number of permutations. The second line should contain $n$ numbers, corresponding to the elements of the given array. If the $i$ -th element belongs to the first permutation, the $i$ -th number should be $1$ , if it belongs to the second one, then its number should be $2$ and so on. The order of the permutations’ numbering is free.
If several solutions are possible, print any one of them. If there’s no solution, print in the first line $-1$ .
If several solutions are possible, print any one of them. If there’s no solution, print in the first line $-1$ .
输入输出样例
输入 #1
9 1 2 3 1 2 1 4 2 5
输出 #1
3 3 1 2 1 2 2 2 3 2
输入 #2
4 4 3 2 1
输出 #2
1 1 1 1 1
输入 #3
4 1 2 2 3
输出 #3
-1
In the first sample test the array is split into three permutations: $(2,1)$ , $(3,2,1,4,5)$ , $(1,2)$ . The first permutation is formed by the second and the fourth elements of the array, the second one — by the third, the fifth, the sixth, the seventh and the ninth elements, the third one — by the first and the eigth elements. Clearly, there are other splitting variants possible.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted