原题链接:http://codeforces.com/problemset/problem/618/F

Double Knapsack

题目描述

You are given two multisets A A and B B . Each multiset has exactly n n integers each between 1 1 and n n inclusive. Multisets may contain multiple copies of the same number.

You would like to find a nonempty subset of A A and a nonempty subset of B 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 −1 . Otherwise, print the indices of elements in any such subsets of A A and B 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 n integers, denoting the elements of A . Each element will be between 1 1 and n n inclusive.

The third line contains n 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 −1 . Otherwise, your solution should be printed on four lines.

The first line should contain a single integer kakak_{a} 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 A .

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

题目大意

给你两个可重集 A,B ,A、B的元素个数都为n且n⩽1000000,它们中每个元素的大小x∈[1,n]。请你分别找出A ,B的可重子集,使得它们中的元素之和相等。

输入格式:

第一行为一个整数n, 表示两个子集的大小。

第二、三行皆为n个整数,分别表示AA 、BB 的元素。

输出格式:

如果无解, 请输出-1。如果有解, 第一行输出A 的可重子集中元素的个数,第二行输出该子集中元素在A 中对应的下标;第三行输出B 的可重子集中元素的个数, 第四行输出该子集中元素在B 中对应的下标。

数据可能存在多组解, 输出一组即可。

题解

我们对于两个数列分别求前缀和,设两个前缀和分别为f(a)=∑ai=1Ai,g(b)=∑bi=1Bif(a)=∑i=1aAi,g(b)=∑i=1bBif(a)=\sum _ {i=1}^{a}A_i,g(b)=\sum_ {i=1}^{b}B_i,假设f(n)<g(n)f(n)<g(n)f(n),即AAA数列中的值小于B" role="presentation" style="position: relative;">BBB数列。找一个最小的g(b)g(b)g(b)使得g(b)⩾f(a)g(b)⩾f(a)g(b)\geqslant f(a),那么肯定有g(b)−f(a)∈[0,n−1]g(b)−f(a)∈[0,n−1]g(b)-f(a)\in [0,n-1],则共有nnn种取值,而因为我们从0开始求解,会求出n+1" role="presentation" style="position: relative;">n+1n+1n+1个取值,所以肯定会有两个重复的取值,那么这两个前缀之间的串的和肯定是一样的。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int M=1e6+5;
struct sd{int cot,t1,t2;
};
int n;
ll x1[M],x2[M];
sd bak[M];
void in()
{scanf("%lld",&n);for(int i=1;i<=n;++i)scanf("%lld",&x1[i]),x1[i]+=x1[i-1];for(int i=1;i<=n;++i)scanf("%lld",&x2[i]),x2[i]+=x2[i-1];
}
void ac()
{bool flag=0;if(x1[n]>x2[n])flag=1,swap(x1,x2);int j=0,hh,s1=1,s2=1,e1=1,e2=1;for(int i=0;i<=n;++i){while(x2[j]<x1[i])++j;hh=x2[j]-x1[i];bak[hh].cot++;if(bak[hh].cot>=2){s1=bak[hh].t1;s2=bak[hh].t2;e1=i;e2=j;break;}bak[hh].t1=i;bak[hh].t2=j;}if(flag)swap(x1,x2),swap(e1,e2),swap(s1,s2);printf("%d\n",e1-s1);for(int i=s1+1;i<=e1;++i)printf("%d ",i);printf("\n%d\n",e2-s2);for(int i=s2+1;i<=e2;++i)printf("%d ",i);
}
int main()
{   in();ac();return 0;
}

CF618F Double Knapsack相关推荐

  1. CF618F Double Knapsack 构造、抽屉原理

    传送门 首先,选取子集的限制太宽了,子集似乎只能枚举,不是很好做.考虑加强限制条件:将"选取子集"的限制变为"选取子序列"的限制.在接下来的讨论中我们将会知道: ...

  2. 2016区域赛前冲刺训练

    UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...

  3. 01背包问题分支限界java_分支限界法-01背包问题

    1.分支限界法介绍 分支限界法类似于回溯法,也是在问题的解空间上搜索问题解的算法.一般情况下,分支限界法与回溯法的求解目标不同.回溯法的求解目标是找出解空间中满足约束条件的所有解:而分支限界法的求解目 ...

  4. 0-1背包、部分背包和完全背包模板

    一:0-1背包 当数据是实数时,乘个数使它变成整数.如3.22->3.22*100=322; 理解模板: #include<cstdio> #include<iostream& ...

  5. 0-1背包问题和部分背包(fractional knapsack)问题分析

    简介 背包问题已经是一个很经典而且讨论很广泛的算法问题了.最近学习到这一部分,打算结合自己思考和编码的过程做一个思考总结.这里主要讨论的0-1背包问题和部分背包问题解决方法背后其实隐藏了两种我们比较常 ...

  6. Knapsack Problem

    背包九讲 0-1 knapsack problem 01背包-牛客网 已知一个背包最多能容纳体积之和为V的物品,现有 n 个物品,第 i 个物品的体积为 vi , 重量为 wi,求当前背包最多能装多大 ...

  7. 单例模式的两种实现方式对比:DCL (double check idiom)双重检查 和 lazy initialization holder class(静态内部类)...

    首先这两种方式都是延迟初始化机制,就是当要用到的时候再去初始化. 但是Effective Java书中说过:除非绝对必要,否则就不要这么做. 1. DCL (double checked lockin ...

  8. java double转换符_java中字符串怎么转换成double类型

    展开全部 public class Demo { public static void main(String[] args) { Demo demo = new Demo(); String str ...

  9. java中将int转换float_在java中将float和double转换为int时有什么区别?

    我用这些语句来测试 float f=4.35f; int i=(int)(f*100); System.out.println(i); double d=4.35; i=(int)(d*100); S ...

  10. C语言:十六进制(HEX)和浮点类型(float、double)转换

    目录 1.浮点类型转换为十六进制 方法1:用地址用指针 方法2:用共用体 方法3: 使用memcpy 2.十六进制转换为浮点类型 近日在研究Modbus协议的时候遇到这样一个情况:使用ModScan3 ...

最新文章

  1. 内存中的调用别的软件程序加密解密函数_公司加密软件哪个最好用?
  2. Face Alignment by 3000 FPS系列学习总结(一)
  3. node.js mysql防注入_避免Node.js中的命令行注入安全漏洞
  4. MySQL查询指定时间的数据
  5. PostgreSQL备份恢复实现
  6. LeetCode979. 在二叉树中分配硬币
  7. 中国基站射频设备行业市场供需与战略研究报告
  8. bzoj2436: [Noi2011]Noi嘉年华
  9. 你可能不知道的 Android Studio 小技巧之「多行编辑」
  10. 保护私有信息的叉积协议及其应用 in c
  11. python车牌识别_python+opencv实现车牌识别
  12. 过滤器和拦截器区别以及执行顺序
  13. Java中swing修改左上角的图标
  14. 合并两个有序链表-python
  15. Shiro框架 Subject、SecurityManager、线程之间的关系
  16. RAR压缩包没有密码如何解压
  17. 服务器被攻击网站打不开解决方案
  18. 2022年广东国家级专精特新企业奖励及培育方法,补贴50-100万
  19. 【时间之外】面向监狱的编程?该学学网络安全法了(2)
  20. 电感线圈绕制常用的漆包线

热门文章

  1. 【jquery调用ajax老是进error,不进success】 bug命名:小雨
  2. 提取二值图像中信号骨架matlab,matlab 二进制图像轮廓提取
  3. [C#] LINQ之GroupBy
  4. HDU4635 Strongly connected
  5. OD调试器调试Delphi程序按钮事件断点方法
  6. VC++ 6.0 快捷键
  7. Ioc--控制反转详解
  8. 怎么样把设备管理器弄到计算机处,电脑设备管理器要连接上蓝牙的方法
  9. 科学计算matlab尔雅答案,科学计算与MATLAB语言2019尔雅答案
  10. Mysql常见的日期查询语句