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. 2017多校第6场 HDU 6096 String AC自动机

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6096 题意:给了一些模式串,然后再给出一些文本串的不想交的前后缀,问文本串在模式串的出现次数. 解法: ...

  2. 设计模式之笔记--工厂方法模式(Factory Method)

    工厂方法模式(Factory Method) 定义 工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类.工厂方法使一个类的实例化延迟到其子类. 类图 描 ...

  3. 外部div不能包裹内部div的问题

    转自http://www.du52.com/text.php?id=362 当设计网页时,如果内部div全部设置css属性float为左右浮动,那么外部div将不能包裹内部div 解决方法 1.在内部 ...

  4. 如何测试一台主机的IP和端口是否能连通,ping telnet

    通过ping 判断一台主机是否开机. 通过:telnet 121.199.167.99 61616  判断一台主机的端口是否能连通. 本机------本地防火墙-------本地路由器-------- ...

  5. json转换工具——fastjson的使用

    1.maven依赖<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...

  6. 初步探究ES6之箭头函数

    今天要介绍的是ES6中的箭头函数. 语法 我们先来看看箭头函数的语法: ([param] [, param]) => { statements } param => expression ...

  7. thinkphp join 表前缀

    public function get_user_group_title($uid){ $pre = C('DB_PREFIX'); $res = M('AuthGroupAccess aga')-& ...

  8. C++ 单例模式的几种实现研究

    都是从网上学得,整理下自己的理解. 单例模式有两种实现模式: 1)懒汉模式: 就是说当你第一次使用时才创建一个唯一的实例对象,从而实现延迟加载的效果. 2)饿汉模式: 就是说不管你将来用不用,程序启动 ...

  9. 货币金额javascript正则表达式

    最多保留两位小数,货币金额(不能为0): /^(([1-9]\d*)(\.\d{1,2})?)$|^(0\.0?([1-9]\d?))$/

  10. Ghostscript 中 ps2pdf 命令在 windows msys 下的运行错误问题。

    前两天看到了 miloyip/game-programmer 这个项目觉得特别有用,真是好东西,明确了指出了学习路线,尤其是新手.不过打开看,有些书对应的亚马逊链接是无效的,比如<Tricks ...