A9232 | Bags and Coins
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
When you were a child you must have been told a puzzle of bags and coins. Anyway, here's one of its versions:
A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible?
The answer is quite simple. The third bag contains a coin and two other bags.
This problem is a generalization of the childhood puzzle. You have $n$ bags. You know that the first bag contains $a_{1}$ coins, the second bag contains $a_{2}$ coins, ..., the $n$ -th bag contains $a_{n}$ coins. In total, there are $s$ coins. Find the way to arrange the bags and coins so that they match the described scenario or else state that it is impossible to do.
A horse has three bags. The first bag has one coin, the second bag has one coin and the third bag has three coins. In total, the horse has three coins in the bags. How is that possible?
The answer is quite simple. The third bag contains a coin and two other bags.
This problem is a generalization of the childhood puzzle. You have $n$ bags. You know that the first bag contains $a_{1}$ coins, the second bag contains $a_{2}$ coins, ..., the $n$ -th bag contains $a_{n}$ coins. In total, there are $s$ coins. Find the way to arrange the bags and coins so that they match the described scenario or else state that it is impossible to do.
输入格式
The first line contains two integers $n$ and $s$ $(1<=n,s<=70000)$ — the number of bags and the total number of coins. The next line contains $n$ integers $a_{1},a_{2},...,a_{n}$ $(1<=a_{i}<=70000)$ , where $a_{i}$ shows the number of coins in the $i$ -th bag.
输出格式
If the answer doesn't exist, print -1.
Otherwise, print $n$ lines, on the $i$ -th line print the contents of the $i$ -th bag. The first number in the line, $c_{i}$ $(0<=c_{i}<=a_{i})$ , must represent the number of coins lying directly in the $i$ -th bag (the coins in the bags that are in the $i$ -th bag are not taken into consideration). The second number in the line, $k_{i}$ $(0<=k_{i}<n)$ must represent the number of bags that lie directly in the $i$ -th bag (the bags that are inside the bags lying in the $i$ -th bag are not taken into consideration). Next, the line must contain $k_{i}$ integers — the numbers of the bags that are lying directly in the $i$ -th bag.
The total number of coins in the solution must equal $s$ . If we count the total number of coins the $i$ -th bag in the solution has, we should get $a_{i}$ .
No bag can directly lie in more than one bag. The bags can be nested in more than one level (see the second test case). If there are multiple correct answers, you can print any of them.
Otherwise, print $n$ lines, on the $i$ -th line print the contents of the $i$ -th bag. The first number in the line, $c_{i}$ $(0<=c_{i}<=a_{i})$ , must represent the number of coins lying directly in the $i$ -th bag (the coins in the bags that are in the $i$ -th bag are not taken into consideration). The second number in the line, $k_{i}$ $(0<=k_{i}<n)$ must represent the number of bags that lie directly in the $i$ -th bag (the bags that are inside the bags lying in the $i$ -th bag are not taken into consideration). Next, the line must contain $k_{i}$ integers — the numbers of the bags that are lying directly in the $i$ -th bag.
The total number of coins in the solution must equal $s$ . If we count the total number of coins the $i$ -th bag in the solution has, we should get $a_{i}$ .
No bag can directly lie in more than one bag. The bags can be nested in more than one level (see the second test case). If there are multiple correct answers, you can print any of them.
输入输出样例
输入 #1
3 3 1 3 1
输出 #1
1 0 1 2 3 1 1 0
输入 #2
3 3 1 3 1
输出 #2
1 0 2 1 3 0 1 1
输入 #3
1 2 1
输出 #3
-1
输入 #4
8 10 2 7 3 4 1 3 1 2
输出 #4
2 0 1 2 1 4 0 2 7 8 0 2 5 6 1 0 3 0 1 0 2 0
The pictures below show two possible ways to solve one test case from the statement. The left picture corresponds to the first test case, the right picture corresponds to the second one.


C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted