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. 二叉查找树、平衡二叉树、红黑树、B-/B+树性能对比

    转载:https://blog.csdn.net/z702143700/article/details/49079107 前言:BST.AVL.RBT.B-tree都是动态结构,查找时间基本都在O(l ...

  2. C++学习之路(六):实现一个String类

    直接贴代码吧,这段时间准备面试也正好练习了一下. class String { public: String(const char *str = ""); ~String(void ...

  3. TCP之connect

    1. connect函数: #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *servaddr, ...

  4. Linux typeof【转】

    转自:http://blog.csdn.net/xiaofeng_yan/article/details/5248633 今天偶然又看到了typeof这个东西,只知道它大概是返回变量的类型,后来上网查 ...

  5. charger related source code position

    Platform Qualcomm MSM8917 or MSM8937 Source kernel/msm-3.18/drivers/power/qpnp-smbcharger.c kernel/m ...

  6. 深入浅出Node.js(一) - 初识Node.js

    1.Node.js将Javascript解决不确定性所使用的事件驱动方式引入了进来,因为JS是一门事件驱动的语言,旨在能够对外界的事件作出响应; 2.Node.js中,所有的有关异步的操作,都在同步操 ...

  7. J2EE MySQL Date数据保持一致解决方案

    1.设置MySQL时区,明确指定 MySQL 数据库的时区,不使用引发误解的 CST show variables like '%time_zone%';set global time_zone = ...

  8. redis之(十三)redis的三种启动方式

    Part I. 直接启动 下载 官网下载 安装 tar zxvf redis-2.8.9.tar.gz cd redis-2.8.9 #直接make 编译 make #可使用root用户执行`make ...

  9. Go语言的指针的一些测试

    参考URL: http://ilovers.sinaapp.com/drupal/node/33 1). 指针在 c 中是个重要的东西,& 和 * 一个取地址.一个解析地址,这是 c 的用法, ...

  10. 如何配置Java环境,包括JDK,Maven等

    下载JDK并安装 搜索JDK,官网立马就出来了,下载之后个人觉得毕竟开发,毕竟这东西不大,C盘稳一点,安装在C盘可以的 配置 右键打开计算机->属性->高级系统设置->高级-> ...