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_ ...
随机推荐
- Python基础(二)——常用内置函数
1. 常用内置函数 (1)isinstance(object, classinfo) 用于判断一个对象是否为某一类型. object 是实例对象 classinfo 是基本类型如 int, floa ...
- UVA - 11572 Unique Snowflakes 滑动扫描
题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...
- Diango 一——URL
内容概要 1.web框架初识 2.MTV模式 3.Django创建流程和命令行工具 4.配置文件 settings 5.视图函数 views 6.路由系统 URL 7.模板系统 templat ...
- TensorFlow——小练习:feed
feed就是喂入数据 使用feed前必须要有占位符作为操作的对象,在运行操作的时候喂入数据. # _*_coding:utf-8_*_ import tensorflow as tf import n ...
- uiautomator 一个简单脚本创建流程
http://www.codeceo.com/article/android-ui-auto-test.html
- ABP数据库的迁移
添加表,一(Test)对多(Test1)关系 Test using Abp.Domain.Entities.Auditing; using System; using System.Collectio ...
- 九度oj 题目1187:最小年龄的3个职工
题目描述: 职工有职工号,姓名,年龄.输入n个职工的信息,找出3个年龄最小的职工打印出来. 输入: 输入第一行包括1个整数N,1<=N<=30,代表输入数据的个数. 接下来的N行有N个职工 ...
- mq类----1
MQ.php <?php /** * Created by PhpStorm. * User: brady * Date: 2017/12/6 * Time: 14:42 * * amqp协议操 ...
- Bootstrap-datepicker 用法
<div class="input-group input-daterange"> <input type="text" id="s ...
- 【Luogu】P3224永无乡(splay)
题目链接 splay模板,启发式合并(其实就是暴力插入)即可. 顺便吐槽时限,带垃圾回收而已……不至于最后一个点死活不让过吧? #include<cstdio> #include<c ...