HDU2141——二分——Can you find it?
2015-08-01 15:15
141 查看
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
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了。。否则TLE */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 500 + 10; int L[MAX], N[MAX], M[MAX]; int LN[1000100],Ln[1000100]; int l, n, m, q; int main() { int cas = 0; while(~scanf("%d%d%d", &l, &n, &m)){ for(int i = 1; i <= l; i++) scanf("%d", &L[i]); for(int i = 1; i <= n; i++) scanf("%d", &N[i]); for(int i = 1; i <= m; i++) scanf("%d", &M[i]); int cout = 1; for(int i = 1; i <= l; i++){ for(int j = 1; j <= n; j++){ LN[cout++] = L[i] + N[j]; } } sort(LN + 1, LN + cout); int cout1 = 1; for(int i = 1; i < cout; i++){ if(LN[i] != LN[i+1]) ; Ln[cout1++] = LN[i]; } scanf("%d", &q); cas++; printf("Case %d:\n", cas); int p; for(int i = 1; i <= q; i++){ scanf("%d", &p); int flag = 0; for(int j = 1; j <= m; j++){ int ll = 1, rr = cout1 - 1; while(ll <= rr){ int mid = (ll + rr) >> 1; if(Ln[mid] + M[j] == p){ // printf("%d %d\n", mid, M[j]); flag = 1; break; } else if(Ln[mid] + M[j] > p) rr = mid - 1; else ll = mid + 1; } if(flag == 1) break; } if(flag == 1) printf("YES\n"); else printf("NO\n"); } } return 0; }
相关文章推荐
- 数据结构 线性表的顺序存储
- Lucene八(搜索分 4000 页)
- 高效Linux之Terminal下实用快捷键
- HDOJ 1276 士兵队列训练问题(模拟,水)
- hdu2187悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
- [LeetCode]240.Search a 2D Matrix II
- bitset 基本使用
- 树莓派+罗技270摄像头+mjpg_streamer实现网路监控
- Tomcat6 一些调优设置内存和连接数
- google code jam 2009 round1 c
- POJ3258——二分——River Hopscotch
- ios-利用xib重新写 应用管理
- zoj3890 BFS
- 面向对象_多态
- ggplot2--实操(图例的修改:以频数散点图为例)
- Problem D
- 详解Fragment的传值问题
- LeetCode之First Missing Positive
- ZH奶酪:Ubuntu 14.04安装LAMP(Linux,Apache,MySQL,PHP)
- HDU2289——二分——Cup