[构造] Codeforces 618F Wunder Fund Round 2016 F. Double Knapsack
来源:互联网 发布:python web运维自动化 编辑:程序博客网 时间:2024/05/20 19:48
给定两个大小为n的可重集A, B ,两个数集中的元素均为[1, n]的整数。
现在要求在两个数集中各找出一个非空子集(子集也为可重集) ,满足两个集合中元素的和相等。
n<=10^6加限制使得选的为两个连续的段。
不妨设SA<=SB,记SA的前缀和为SA(0),SA(1),SA(2),…, SB的前缀和为SB(0),SB(1),SB(2),…
对于每个SA(i),找到最大的SB(j)满足SA(i)>=SB(j),这样可以得到n+1个SA(i)-SB(j),然而每个都是在0到n-1之间。
所以至少有两个是一样的。
于是SA(i)-SB(j)=SA(p)-SB(q),即SA(i)-SA(p)=SB(j)-SB(q)。
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;typedef long long ll;typedef pair<int,int> abcd;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=1000005;int n,a[N],b[N];ll sa[N],sb[N];int match[N];int pos[N];inline void print(int l,int r){ if (l>r) swap(l,r); printf("%d\n",r-l); for (int i=l+1;i<=r;i++) printf("%d ",i); printf("\n");}int main(){ int flag=0; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(n); for (int i=1;i<=n;i++) read(a[i]),sa[i]=sa[i-1]+a[i]; for (int i=1;i<=n;i++) read(b[i]),sb[i]=sb[i-1]+b[i]; if (sa[n]>sb[n]){ flag=1; for (int i=1;i<=n;i++) swap(a[i],b[i]),swap(sa[i],sb[i]); } sort(sb,sb+n+1); for (int i=0;i<n;i++) pos[i]=-1; for (int i=0;i<=n;i++){ int it=upper_bound(sb,sb+n+1,sa[i])-sb-1; match[i]=it; if (~pos[sa[i]-sb[it]]){ int t=pos[sa[i]-sb[it]]; if (!flag) print(t,i),print(match[t],match[i]); else print(match[t],match[i]),print(t,i); return 0; } else pos[sa[i]-sb[it]]=i; } return 0;}
0 0
- [构造] Codeforces 618F Wunder Fund Round 2016 F. Double Knapsack
- Wunder Fund Round 2016 F. Double Knapsack(two pointers + 鸽笼原理)
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) F. Double Knapsack
- Codeforces Wunder Fund Round 2016 C D E
- Codeforces Wunder fund 618ABCDEF
- Wunder Fund Round 2016 A. Slime Combining
- cf#Wunder Fund Round 2016 -B- Guess the Permutation-构造-乱搞
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)(B)(构造,找规律)
- codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A.Slime Combining
- Codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) C. Constellation
- Codeforces - Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)C - Constellation(数学练习)
- Codeforces Round 877 F
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) 618C Constellation(计算几何+stl)
- Codeforces 542F 构造
- Wunder Fund Round 2016 C. Constellation(贪心+计算几何)
- cf#Wunder Fund Round 2016 -A - Slime Combining-简单数学
- cf#Wunder Fund Round 2016 -C - Constellation-计算几何-水题
- Wunder Fund Round 2016 B. Guess the Permutation
- Android Studio 更新第三方.so
- 红蓝牌
- 奇怪的比赛,蓝桥杯2012年第4题
- Jsch学习
- Banner 统计
- [构造] Codeforces 618F Wunder Fund Round 2016 F. Double Knapsack
- 蓝桥杯第五届省赛C/C++A组第五题 锦标赛
- 快速实现python c扩展模块
- kubernetes calico网络qos设计
- 向后兼容--对象检测
- hdu 4508 湫湫系列故事——减肥记I (最简单的完全背包)
- 蓝桥杯——网络寻路(dfs)
- 二叉树的前序遍历
- 煤球数目