CodeForces - 618F Double Knapsack
Discription
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.
Examples
10
10 10 10 10 10 10 10 10 10 10
10 9 8 7 6 5 4 3 2 1
1
2
3
5 8 10
5
4 4 3 3 3
2 2 2 2 5
2
2 3
2
3 5 一道神构造。
设sa[]为a[]的前缀和,sb[]为b的前缀和,对于每个0<=i<=n,我们找到一个最大的使得sb[j]<=sa[i]的j,这样每个sa[i]-sb[j]都是[0,n-1]的整数了(因为如果sa[i]-sb[j]>=n的话,j可以继续后移(a[],b[]中元素都<=n))
所以至少会有一对 i和i'满足 sa[i]-sb[j] == sa[i']-sb[j'],直接输出就行了。。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1000005;
ll a[maxn],b[maxn];
int px[maxn],py[maxn],n;
inline int read(){
int x=0; char ch=getchar();
for(;!isdigit(ch);ch=getchar());
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return x;
}
void W(int x){ if(x>=10) W(x/10); putchar(x%10+'0');}
int main(){
n=read();
for(int i=1;i<=n;i++) a[i]=read(),a[i]+=a[i-1];
for(int i=1;i<=n;i++) b[i]=read(),b[i]+=b[i-1];
for(int i=0,j=0,D;i<=n;i++){
for(;j<n&&b[j+1]<=a[i];j++);
D=a[i]-b[j];
if(px[D]){
W(i-px[D]+1),puts("");
for(int o=px[D];o<=i;o++) W(o),putchar(' ');
puts(""),W(j-py[D]+1),puts("");
for(int o=py[D];o<=j;o++) W(o),putchar(' ');
return 0;
}
px[D]=i+1,py[D]=j+1;
}
return 0;
}
CodeForces - 618F Double Knapsack的更多相关文章
- Codeforces.618F.Double Knapsack(构造 鸽巢原理)
题目链接 \(Description\) 给定两个大小为\(n\)的可重集合\(A,B\),集合中的元素都在\([1,n]\)内.你需要从这两个集合中各选一个非空子集,使它们的和相等.输出方案. \( ...
- 618F Double Knapsack
传送门 题目大意 分析 代码 #include<iostream> #include<cstdio> #include<cstring> #include<s ...
- 【CF618F】Double Knapsack(构造)
[CF618F]Double Knapsack(构造) 题面 洛谷 Codeforces 题解 很妙的一道题. 发现找两个数集很不爽,我们强制加强限制,我们来找两个区间,使得他们的区间和相等. 把区间 ...
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) F. Double Knapsack 鸽巢原理 构造
F. Double Knapsack 题目连接: http://www.codeforces.com/contest/618/problem/F Description You are given t ...
- [codeforces 618 F] Double Knapsack (抽屉原理)
题目链接:http://codeforces.com/contest/618/problem/F 题目: 题目大意: 有两个大小为 N 的可重集 A, B, 每个元素都在 1 到 N 之间. 分别找出 ...
- Codeforces 1140G Double Tree 倍增 + dp
刚开始, 我以为两个点肯定是通过树上最短路径过去的, 无非是在两棵树之间来回切换, 这个可以用倍增 + dp 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ...
- 【Codeforces 1132E】Knapsack
Codeforces 1132 E 题意:给\(cnt_i\)个\(i\)(\(1\leq i\leq 8\)),问用这些数所能构成的最大的不超过\(W\)的数. 思路:随机+贪心... 我们考虑将贪 ...
- CF618F Double Knapsack 构造、抽屉原理
传送门 首先,选取子集的限制太宽了,子集似乎只能枚举,不是很好做.考虑加强限制条件:将"选取子集"的限制变为"选取子序列"的限制.在接下来的讨论中我们将会知道: ...
- 2018.09.27 codeforces618F. Double Knapsack(抽屉原理+构造)
传送门 思维题. 考虑维护两个数列的前缀和a1,a2,a3,...,ana_1,a_2,a_3,...,a_na1,a2,a3,...,an和b1,b2,b3,...,bnb_1,b_2,b_ ...
随机推荐
- jflash合并两个文件
有时候需要将两个代码块烧写进入单片机的flash,可以使用合并的方法将两个文件合并为一个文件进行烧写,也可以分两次烧写,但要注意不要擦写不相关的存储空间. 打开J-FLASH,新建一个工程,然后fil ...
- nRF52-PCA10040——Overview
Overview Zephyr applications use the nrf52_pca10040 board configuration to run on the nRF52 Developm ...
- windows下创建子进程过程中代码重复执行问题
windows在启动子进程的时候会将主进程文件倒入到子进程中.导入模块就相当于执行这个模块中的代码, 所以第一个print会在主进程中执行一次,又在被导入的过程中在子进程中又执行了一次. p.star ...
- IDEA字体颜色快速导入辅助工具设置
原创链接:https://www.cnblogs.com/ka-bu-qi-nuo/p/9181954.html 程序员开发大多数都是使用IDE进行代码开发的,这样能快速的开发出需要的项目.之前一直 ...
- 29、在android中采用动画方案来弹出窗口
1.自定义style 2.使用方法: buttonWhat.setOnClickListener(new OnClickListener() { @Override public void onCli ...
- webdriver高级应用- 使用日志模块记录测试过程中的信息
在自动化脚本执行过程中,使用Python的日志模块记录在测试用例执行过程中一些重要信息或者错误日志等,用于监控和后续调试脚本. 在pycharm下新建工程,并创建Log.py.Logger.conf以 ...
- python学习-- Django根据现有数据库,自动生成models模型文件
Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...
- SQL 与或运算
如果一个字段需要同时包含多个信息点, 最佳的方法是进行位运算,如:1,2,4,8,16 根据与运算进行判断,如一个字段为7,判断2是否存在, 7&2 = 2为ture时,表示存在,反之亦然, ...
- cobbler 安装centos7.3时GPT问题(五)
磁盘分区表MBR和GPT介绍: MBR(Master Boot Record):最大只支持2 TB的盘,最多只支持4个主分区,信息只存储在一个区域. GPT(GUID partition table) ...
- 【Luogu】P2634聪聪可可(树形DP)
题目链接 水题,时限放得非常宽,暴力DP随便套上一波register就能卡过去. 唯一的遗憾是5A. 树形DP,s[i][j]表示以i为根的子树里距i的距离%3=j的点数,f[i]表示i为根的子树内一 ...