F. Double Knapsack

题目连接:

http://www.codeforces.com/contest/618/problem/F

Description

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.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1 000 000) — 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.

Output

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 ka, the size of the corresponding subset of A. The second line should contain ka distinct integers, the indices of the subset of A.

The third line should contain a single integer kb, the size of the corresponding subset of B. The fourth line should contain kb 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.

Sample Input

10

10 10 10 10 10 10 10 10 10 10

10 9 8 7 6 5 4 3 2 1

Sample Output

1

2

3

5 8 10

Hint

题意

给你a集合和b集合,两个集合里面都恰好有n个数,每个数都是在[1,n]范围内的数

然后你需要找到a集合的一个子集,b集合的一个子集,使得这两个子集的和相同

然后输出出来。

题解:

令Ai = a0+a1+...+ai,Bi = b0+b1+...+bi

其中A0 = 0,B0 = 0;

我们假设An<Bn,那么对于每个Ai,我们都可以找到一个最大的j,使得Ai-Bj>=0

显然有(n+1)对Ai-Bj,且0<=(Ai-Bj)<=n-1

根据鸽巢原理,显然可以找到两对Ai-Bj = Ai'-Bj'

所以只要输出这两队就好了

边扫边存下来就好了

然后这道题就结束了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
const long long inf = 1e9;
long long a[maxn],b[maxn];
pair<int,int>d[maxn*2]; int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=1;i<=n;i++)
scanf("%lld",&b[i]);
for(int i=0;i<maxn*2;i++)
d[i]=make_pair(inf,inf);
d[maxn]=make_pair(1,1);
int p1=1,p2=1,now=0;
while(1)
{
if(now<=0)now+=a[p1++];
else now-=b[p2++];
if(d[now+maxn].first!=inf)
{
printf("%d\n",p1-d[now+maxn].first);
for(int i=d[now+maxn].first;i<p1;i++)
printf("%d ",i);
printf("\n");
printf("%d\n",p2-d[now+maxn].second);
for(int i=d[now+maxn].second;i<p2;i++)
printf("%d ",i);
printf("\n");
return 0;
}
d[now+maxn]=make_pair(p1,p2);
}
}

Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) F. Double Knapsack 鸽巢原理 构造的更多相关文章

  1. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B. Guess the Permutation 水题

    B. Guess the Permutation 题目连接: http://www.codeforces.com/contest/618/problem/B Description Bob has a ...

  2. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A. Slime Combining 水题

    A. Slime Combining 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2768 Description Your frien ...

  3. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) E. Robot Arm 线段树

    E. Robot Arm 题目连接: http://www.codeforces.com/contest/618/problem/E Description Roger is a robot. He ...

  4. Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)

    现在水平真的不够.只能够做做水题 A. Slime Combining 题意:就是给n个1给你.两个相同的数可以合并成一个数,比如说有两个相同的v,合并后的值就是v+1 思路:直接模拟栈 #inclu ...

  5. Codeforces Round #319 (Div. 2) B Modulo Sum (dp,鸽巢)

    直接O(n*m)的dp也可以直接跑过. 因为上最多跑到m就终止了,因为前缀sum[i]取余数,i = 0,1,2,3...,m,有m+1个余数,m的余数只有m种必然有两个相同. #include< ...

  6. Codeforces Round #648 (Div. 2) E. Maximum Subsequence Value(鸽巢原理)

    题目链接:https://codeforces.com/problemset/problem/1365/E 题意 有 $n$ 个元素,定义大小为 $k$ 的集合值为 $\sum2^i$,其中,若集合内 ...

  7. 2016 Multi-University Training Contest 3 1011【鸽巢原理】

    题解: 坐标(0,m)的话,闭区间,可能一共有多少曼哈顿距离? 2m 但是给一个n,可能存在n(n+1)/2个曼哈顿距离 所以可以用抽屉原理了 当n比抽屉的数量大,直接输出yes 不用计算 那...N ...

  8. Educational Codeforces Round 117 (Rated for Div. 2)

    Educational Codeforces Round 117 (Rated for Div. 2) A. Distance https://codeforces.com/contest/1612/ ...

  9. CF Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

    1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort    暴力枚举,水 1.题意:n*m的数组, ...

随机推荐

  1. 64_f2

    flxmlrpc-0.1.4-5.fc26.x86_64.rpm 22-May-2017 21:32 57950 flxmlrpc-devel-0.1.4-5.fc26.i686.rpm 22-May ...

  2. swift中闭包的循环引用

    首先我们先创造一个循环引用 var nameB:(()->())? override func viewDidLoad() { super.viewDidLoad() let bu = UIBu ...

  3. 启动另一个activity

    1. 只负责启动 Intent intent = new Intent(mContext, BookOrderActivity.class); Bundle mEmployeeBundle = new ...

  4. DataSet、DataTable、DataRow的数据复制方法

    DataSet 对象是支持 ADO.NET的断开式.分布式数据方案的核心对象 ,用途非常广泛.我们很多时候需要使用其中的数据,比如取得一个DataTable的数据或者复制另一个DataTabe中的数据 ...

  5. Elements in iteration expect to have 'v-bind:key' directives.' 提示错误如何解决?

    在学习vue过程中遇到Elements in iteration expect to have 'v-bind:key' directives.' 这个错误,查阅资料得知Vue 2.2.0+的版本里, ...

  6. 用webpy实现12306余票查询

    效果

  7. webpy 模版语言

    webpy框架拥有自己的模版语言Templetor Templetor具有Python的语言风格 可以直接嵌入Python代码 $code: videoIn = post.filename and p ...

  8. python的版本会导致IBus设置(中文输入法)出错

    最近在学习python,可是,发现我的输入法 IBus-pinyin ,不能用了, 现象: 发现 “首选输入法”,根本点击不进去IBus设置的窗口,想去设置输入法都不行, IBus设置的窗口: 原因是 ...

  9. python——聊聊iterable,sequence和iterators

    ---------------------------------------------------------------前言----------------------------------- ...

  10. Java Eclipse插件

    EasyExplore 快速打开文件所在目录1 http://sourceforge.net/projects/easystruts/ OpenExplorer 快速打开文件所在目录2 https:/ ...