二分贪心专题C
来源:互联网 发布:moga算法 编辑:程序博客网 时间:2024/06/05 03:21
Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X.
3 3 31 2 31 2 31 2 331410
Sample Output
Case 1:NOYESNO
题目大意:给你三个数组A,B,C。问能不能再A,B,C数组中各找一个数使得他们的和为X里。
从数据范围来看A,B,C数组都不超过500个,但是暴力的话复杂度为n^3 空间与时间复杂度难以接受。
换个思路,将A,B数组合并为数组D,那么问题就变成了Di+Ci=X
即X-Di=Ci
枚举D数组中的元素并在C数组中进行二分查找。
依然是思路的问题。我们看到500^3难以接受,但是500*500很好实现,自己的榆木脑袋还是有待雕琢呀。。。
源代码:
#include<iostream>#include <algorithm>using namespace std;int main(){int l,n,m,total=0;while (cin>>l>>n>>m){total++;int tot=0,s;int a[505];int b[505];int c[505];int d[260000];for (int i=1;i<=l;i++) cin>>a[i];for (int i=1;i<=n;i++) cin>>b[i];for (int i=1;i<=m;i++) cin>>c[i];cin>>s;for (int i=1;i<=l;i++)for (int j=1;j<=n;j++) {tot++;d[tot]=a[i]+b[j];}sort(d+1,d+tot+1);cout<<"Case "<<total<<":"<<endl;for (int i=1;i<=s;i++){bool flag=false;int k;cin>>k;for (int j=1;j<=m;j++){int l=1,r=tot,mid;int ans=k-c[j];while (l<=r){mid=(l+r)/2;if (d[mid]==ans) {flag=true;break;}if (d[mid]<ans) l=mid+1;if (d[mid]>ans) r=mid-1;}if (flag) break;}if (flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;}}return 0;}
0 0
- 二分贪心专题C
- 二分贪心专题A
- 二分贪心专题B
- 二分贪心专题D
- 二分贪心专题E
- 二分贪心专题F
- 二分贪心专题总结
- 二分贪心练习题专题总结
- 【二分图匹配入门专题1】C
- 贪心专题
- 贪心专题
- 贪心专题
- 【贪心专题】POJ 2456 Aggressive cows && NYOJ 586 疯牛(最大化最小值 贪心+二分搜索)
- 【贪心专题】POJ 3258 River Hopscotch (最大化最小值 贪心+二分搜索)
- CodeForces 460C--- Present(二分+贪心)
- C. Present(二分加贪心)
- codeforces 343C Read Time 二分 + 贪心
- Codeforces 460C Present (贪心 + 二分)
- 通俗讲解:缓存、缓存算法和缓存框架
- C++中产生随机整数的方法(srand()函数与rand()函数)
- CSS: display:none和visibility:hidden的区别
- How to remove unused kernel from fedora 25
- 学习整理HttpClient4.5成http访问服务类
- 二分贪心专题C
- 机器学习与计算机视觉资料汇总
- 引导页的实现大全
- SD卡CMD命令介绍
- mvp思考
- android 新闻系统客户端,服务器端采用JSP Servlet 实现
- New Start
- svn使用教程及常见问题解决方案
- mysql5.6启动失败的一般解决方法