Can you find it? 【二分】
来源:互联网 发布:岳不群 知乎 编辑:程序博客网 时间:2024/06/03 18:12
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.
Input
There are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers.
Output
For each case, firstly you have to print the case number as the form “Case d:”, then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print “YES”, otherwise print “NO”.
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
Sample Output
Case 1:
NO
YES
NO
记得 白书上好像有和这个类似的。 就是要先将式子变下形,
A+B = X-C ;
并且让A+B 合在一个数组里; 【不是很难,不过一定要理解这个二分枚举】
代码
#include<bits/stdc++.h>#define LL long longusing namespace std;const int MAXN = 500+10;const int MAXM = 3000000;const int inf = 0x3f3f3f3f;const double pi= acos(-1.0);const double eps = 1e-5;/*------------------------------*/int l,n,m;int a[MAXN],b[MAXN],c[MAXN];int d[MAXM];int main(){ int kk=1; while(~scanf("%d%d%d",&l,&n,&m)){ for(int i=0;i<l;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=0;i<m;i++) scanf("%d",&c[i]); printf("Case %d:\n",kk++); int s=0; for(int i=0;i<l;i++){ for(int j=0;j<n;j++){ d[s++]=a[i]+b[j]; } } int ri=s; sort(d,d+s); int q;cin>>q; while(q--){ int a;scanf("%d",&a); int i; int mid; for(i=0;i<m;i++){ int o=a-c[i]; int L=0;int R=ri-1; int ss; while(L<=R){ mid=(L+R)>>1; if(d[mid]>=o) { ss=d[mid]; R=mid-1;} else L=mid+1; } //printf("ss=== %d \n",ss); if(ss==o) break; } if(i==m) puts("NO"); else puts("YES"); } } return 0;}
- Can you find it?---二分
- Can you find it?(二分)
- Can you find it? 【二分】
- Can you find it? 二分
- Can you find it? hdu2141 二分查找
- HDU2141:Can you find it?(二分)
- hdu 2141 Can you find it? 二分
- HDU 2141 Can you find it?(二分)
- hdu 2141 Can you find it? 二分
- 二分 hdu2141 Can you find it?
- HDU 2141 Can you find it?【二分】
- hdu2141 Can you find it?(二分*)
- hdoj2141 Can you find it?二分+模拟
- hdoj-2141 Can you find it?【二分】
- HDU2141 Can you find it?(二分)
- HDU2141:Can you find it? (二分)
- hdu2141 Can you find it? (二分)
- 【HDU 2141】【二分】 Can you find it?
- iOS基础教程-SQLite数据库操作(一 StoryBoard操作 SQLite操作前准备工作)
- libevent实现的一个简单的服务器和客户端程序
- jenkins中添加HTML Publisher Plugin插件
- opencv需要学习的知识总结
- Django向Highcharts图表发送数据并用模板语言实现动态显示
- Can you find it? 【二分】
- Lucene学习笔记(2)
- mysql的插入语句的条件插入
- HDU 5821 Ball【贪心】
- expect编程
- 阿里云PAI
- android 五角星画法 五角星计算公式
- 剑指offer 35---查找第一个只出现一次的字符
- Zuul动态路由及动态Filter实现