A10241 | Double Knapsack
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given two multisets $A$ and $B$ . Each multiset has exactly $n$ integers each between $1$ and $n$ inclusive. Multisets may contain multiple copies of the same number.
You would like to find a nonempty subset of $A$ and a nonempty subset of $B$ such that the sum of elements in these subsets are equal. Subsets are also multisets, i.e. they can contain elements with equal values.
If no solution exists, print $-1$ . Otherwise, print the indices of elements in any such subsets of $A$ and $B$ that have the same sum.
You would like to find a nonempty subset of $A$ and a nonempty subset of $B$ such that the sum of elements in these subsets are equal. Subsets are also multisets, i.e. they can contain elements with equal values.
If no solution exists, print $-1$ . Otherwise, print the indices of elements in any such subsets of $A$ and $B$ that have the same sum.
输入格式
The first line of the input contains a single integer $n$ ( $1<=n<=1000000$ ) — the size of both multisets.
The second line contains $n$ integers, denoting the elements of $A$ . Each element will be between $1$ and $n$ inclusive.
The third line contains $n$ integers, denoting the elements of $B$ . Each element will be between $1$ and $n$ inclusive.
The second line contains $n$ integers, denoting the elements of $A$ . Each element will be between $1$ and $n$ inclusive.
The third line contains $n$ integers, denoting the elements of $B$ . Each element will be between $1$ and $n$ inclusive.
输出格式
If there is no solution, print a single integer $-1$ . Otherwise, your solution should be printed on four lines.
The first line should contain a single integer $k_{a}$ , the size of the corresponding subset of $A$ . The second line should contain $k_{a}$ distinct integers, the indices of the subset of $A$ .
The third line should contain a single integer $k_{b}$ , the size of the corresponding subset of $B$ . The fourth line should contain $k_{b}$ distinct integers, the indices of the subset of $B$ .
Elements in both sets are numbered from $1$ to $n$ . If there are multiple possible solutions, print any of them.
The first line should contain a single integer $k_{a}$ , the size of the corresponding subset of $A$ . The second line should contain $k_{a}$ distinct integers, the indices of the subset of $A$ .
The third line should contain a single integer $k_{b}$ , the size of the corresponding subset of $B$ . The fourth line should contain $k_{b}$ distinct integers, the indices of the subset of $B$ .
Elements in both sets are numbered from $1$ to $n$ . If there are multiple possible solutions, print any of them.
输入输出样例
输入 #1
10 10 10 10 10 10 10 10 10 10 10 10 9 8 7 6 5 4 3 2 1
输出 #1
1 2 3 5 8 10
输入 #2
5 4 4 3 3 3 2 2 2 2 5
输出 #2
2 2 3 2 3 5
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted