C语言排序(12)___Can you find it?(Hdu 2141)
来源:互联网 发布:centos 用户管理 编辑:程序博客网 时间:2024/06/06 03:32
Problem 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
题目大意:
输入数据第一排是三个数,分别表示三个数组的大小. (三个数组的大小的最大值都是500)
接下来的三排是输入三个数组的数字.
然后第四排输入一个整数n表示接下来n排测试数据.
每个测试数据是一个整数m.
要求在上面的三个数组中每个数组找一个数(共三个数)相加之和等于该测试数据m.如果找到了就输出YES,如果不存在这样的三个数那么就输出NO.
大致思路:
因为题目给的数据每个数组最多有500个元素,那么如果三个for循环就是500*500*500肯定超时,(姑且我们这里把第一第二第三数组叫为a,b,c数组)
这个时候我们可以把c数组排序,然后用两个for循环枚举a,b两个数组中所有组合的和k,然后再用二分查找在c数组中查找m-k这个数.这样时间最多为500*500*log500≈2250000
或者是在a,b两个数组中所有组合的和k组成的数组中去二分查找m-c[i]这个数(以下代码便是).
代码如下:
#include <stdio.h>#include <algorithm>using namespace std;int a[501],b[501],m[250001],c[501],flag;void find(int a[],int b,int n){ int left,right,mid; left=0,right=n-1; while(left<=right) { mid=(left+right)/2; if(a[mid]==b) { flag=1; break; } else if(a[mid]>b) right=mid-1; else left=mid+1; }}int main(){ int i,j,k,n1,n2,n3,n4,ci=0; while(scanf("%d%d%d",&n1,&n2,&n3)!=EOF) { ci++; for(i=0;i<n1;i++) scanf("%d",&a[i]); for(i=0;i<n2;i++) scanf("%d",&b[i]); for(i=0;i<n3;i++) scanf("%d",&c[i]); n4=0; for(i=0;i<n1;i++) for(j=0;j<n2;j++) m[n4++]=a[i]+b[j]; sort(m,m+n4); int N; scanf("%d",&N); printf("Case %d:\n",ci); while(N--) { flag=0; scanf("%d",&k); for(i=0;i<n3;i++) { find(m,k-c[i],n4); if(flag==1) break; } if(flag==1) printf("YES\n"); else printf("NO\n"); } } return 0;}
0 0
- C语言排序(12)___Can you find it?(Hdu 2141)
- C - Can you find it? HDU 2141
- C语言--Can you find it?
- HDU/HDOJ 2141 Can you find it?
- hdu 2141 can you find it?
- HDU-2141 can you find it?
- hdu 2141 Can you find it?
- Can you find it? hdu 2141
- HDU 2141 Can you find it?
- hdu 2141 Can you find it ?
- hdu(2141) Can you find it?
- Can you find it?(hdu 2141)
- hdu 2141 Can you find it?
- HDU 2141 Can you find it?
- HDU - 2141 Can you find it?
- hdu 2141 Can you find it? 二分
- HDU 2141 Can you find it?
- HDU - 2141 Can you find it?
- Linux下find的用法记录
- WEB站点性能优化实践(加载速度提升2s)
- maven 私服 OpenSCManager failed - 拒绝访问。 (0x5)
- 跨浏览器的inline-block
- shell 内部变量
- C语言排序(12)___Can you find it?(Hdu 2141)
- 《数字图像处理原理与实践(MATLAB版)》一书之代码Part4
- openldap配置笔记
- centOS利用crond自动备份mysql数据库
- 《Redis设计与实现》读书笔记
- Invalidate 和 UpdateWindow
- 麒麟操作系统内核同其他操作系统内核的相似性分析
- 无废话XML--XML细节
- Java新技术之反射