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_ ...
随机推荐
- pandas-Notes2
#coding = utf-8 import pandas as pd import numpy as np import matplotlib as plt dates = pd.date_rang ...
- Divisibility by 25 CodeForces - 988E
You are given an integer nn from 11 to 10181018 without leading zeroes. In one move you can swap any ...
- debian卸载vmware
原因: 由于vagrant默认支持virtualbox,而要支持vmware需要一个商用付费的插件.所以卸载vmware,使用virtualbox 具体操作: $ sudo vmware-instal ...
- MySQL-状态Waiting on empty queue引申
MySQL 事件调度器示例演示 我们大家都知道MySQL 事件调度器是在 MySQL 5.1 中新生的一个较为特殊的功能,其可以作为定时任务调度器,来取代部分原先只能用操作系统任务调度器才能完成的定时 ...
- python字符串、列表和字典的说明
python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...
- UVa 12235 状压DP Help Bubu
题解戳这 一开始没看懂题解,后来想明白以后,d(i, j, s, x)是考虑第i本书的时候,前面已经拿走了j本书,剩下的书的种类的二进制状态为s,剩下的最后一本书的编号为x,所能得到的最小混乱度. 这 ...
- day01_14.遍历数组
<?php $a = array('a','b','c'); print_r($a); ?> 输出结果:Array ( [0] => a [1] => b [2] => ...
- [Offer收割]编程练习赛50
题目3 : 末尾有最多0的乘积 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定N个正整数A1, A2, ... AN. 小Hi希望你能从中选出M个整数,使得它们的乘 ...
- Navicat数据库备份还原
Navicat数据库备份包括数据和架构 还原数据库:把备份文件复制到备份文件夹所在位置,还原即可
- 阿里云 fatal: Authentication failed 退出码 128
阿里云拉取代码时需要输入的用户名密码 跟我们登录aliyun的时候的账号密码不一样,需要先重置一下的,重置之前要配置好邮箱 https://code.aliyun.com/profile/passwo ...