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);
}
}

最新文章

  1. 查看.NET Core源代码通过Autofac实现依赖注入到Controller属性
  2. redis 基础
  3. PHPCMS后台登陆路径修改方法(V9版)
  4. C#与数据库访问技术总结(十三)之DataReader对象
  5. java内部类的继承
  6. .net框架版本说明
  7. loadrunner做webservice接口之简单调用
  8. 3D魔方游戏
  9. MongoDB3.2版本与3.0版本写场景压力测试对比
  10. 8月1日起,这些新政将影响移动互联网产业-b
  11. Eclipse怎么忽略掉报错的js文件
  12. 从头认识Spring-2.7 自己主动检測Bean(2)-过滤器&amp;lt;context:include-filter/&amp;gt;
  13. 微软Azure AspNetCore微服务实战第1期【补充2017-09-09活动】
  14. Python基础05_str_补充
  15. centos iftop iotop htop
  16. docker 构建dockerfile
  17. 18.搭建 vue 环境
  18. html (第四本书第五章参考)
  19. CAN总线实际运用分析问题。
  20. Sublime 正则替换

热门文章

  1. Java垃圾收集算法
  2. MVC 从控制器将数据对象赋值给前端JS对象
  3. BZOJ 4516: [Sdoi2016]生成魔咒——后缀数组、并查集
  4. Makefile系列之二 : 命令
  5. 使用Storm实现实时大数据分析(转)
  6. CSS/Compass修改placeholder的文字样式
  7. gridcontrol的列头右键菜单问题
  8. 兼容python3的SSDB客户端
  9. 【UI】自动化用例设计技巧
  10. 四十八 常用内建模块 HTMLParser