hdoj-2141-Can you find it?
来源:互联网 发布:医药魔方销售数据库 编辑:程序博客网 时间:2024/06/10 06:38
Description
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 31 2 31 2 31 2 331410
Sample Output
Case 1:NOYESNO
题意很容易理解,很水的一道模拟题,给你L个数A,N个数B,M个数C,然后一个X,求Ai+Bj+Ck = X,我拿到题的第一瞬间想到暴力,可是O(N3),1<=L, N, M<=500, 1<=S<=1000.是肯定会爆了,于是把第一个数组合第二个数组的所有组合线求出来,再将第三个数组排序,然后二分的去找,就ac了~~~
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>using namespace std;int num[250005];bool tree(int a[],int x,int y){ bool f=false; int lf,rig,mid; lf=0; rig=x-1; mid=(lf+rig)/2; //printf("1\n"); while(lf<=rig) { //printf("1\n"); if(a[mid]>y) { rig=mid-1; } else if(a[mid]<y) { lf=mid+1; } else{ f=true; break; } mid=(lf+rig)/2; } return f;}int main(){ int ll[505],mm[505],nn[505]; int l,n,m,s; int Cas=0; bool flag; while(scanf("%d%d%d",&l,&n,&m)!=EOF) { memset(ll,0,sizeof(ll)); memset(nn,0,sizeof(nn)); memset(mm,0,sizeof(mm)); for(int i=0;i<l;i++) scanf("%d",&ll[i]); for(int i=0;i<n;i++) scanf("%d",&nn[i]); for(int i=0;i<m;i++) scanf("%d",&mm[i]); int k=0; for(int i=0;i<l;i++) for(int j=0;j<n;j++) { num[k++]=ll[i]+nn[j]; } sort(num,num+k); Cas++; printf("Case %d:\n",Cas); scanf("%d",&s); while(s--) { flag=0; int x; scanf("%d",&x); for(int i=0;i<m;i++) { if(tree(num,k,x-mm[i])) { flag=1; break; } } if(flag) printf("YES\n"); else printf("NO\n"); } } return 0;}
0 0
- HDU/HDOJ 2141 Can you find it?
- hdoj 2141 Can you find it?
- hdoj 2141 Can you find it? 【二分法】
- hdoj-2141 Can you find it?【二分】
- hdoj 2141 can you find it ?
- HDOJ 2141 Can you find it?
- hdoj-2141-Can you find it?
- HDOJ 2141 Can you find it?
- HDOJ-2141 Can you find it?
- HDOJ 2141 Can you find it?
- HDOJ 2141 Can you find it?
- HDOJ 2141 Can you find it?
- HDOJ/HDU 2141 Can you find it? 二分搜索优化
- HDOJ 题目2141 Can you find it?(二分搜索)
- HDOJ Can you find it? 2141(二分搜索)
- HDOJ 2141 Can you find it?(二分)
- HDOJ 2141 Can you find it?(二分查找,binary_search())
- Hdoj 2141 Can you find it ?
- Android定时器
- 周总结3.13
- nrf51822 --- flash(操作单片机自带)
- 大数乘法
- 第五周项目2-游戏中的角色类(2)
- hdoj-2141-Can you find it?
- 第五周项目1:三角形类雏形(1)
- [LeetCode]Palindrome Linked List
- Java 封装 HDFS API 操作
- 生成boot.img的脚本
- 每天一个Linux命令
- 周总结3.26
- leetcode 130. Surrounded Regions
- hive与es之间实现数据交互