hdu2141Can you find it?
给你四个集合。要你从这四个集合中
各取出一个数出来,推断,取出的前三个数的和
是否等于第四个数。
数据比較大。我的做法是将
前两个集合全部数全部和的情况取出来,
然后二分查找第四个集合和第三集合取出的数的差值。
我比較懒。用了stl中的bsearch实现二分查找。
代码例如以下:
#include<iostream>
#include<cstdlib>
using namespace std;
int num_a,num_b,num_c,a[510],b[510],c[510],ab[250010];
int cmp(const void *a,const void *b)
{
return *(int *)a - *(int *)b;
}
void init()
{
int num_x,x,*p,tmp;
for(int i=0;i<num_a;i++)
scanf("%d",&a[i]);
for(int i=0;i<num_b;i++)
scanf("%d",&b[i]);
for(int i=0;i<num_a;i++)
for(int j=0;j<num_b;j++)
ab[i*num_b+j]=a[i]+b[j];
for(int i=0;i<num_c;i++)
scanf("%d",&c[i]);
qsort(ab,num_a*num_b,sizeof(int),cmp);
scanf("%d",&num_x);
while(num_x--)
{
scanf("%d",&x);
for(int i=0;i<num_c;i++)
{
tmp=x-c[i];
p=(int*)bsearch(&tmp,ab,num_a*num_b,sizeof(int),cmp);
if(p!=NULL)
{
printf("YES\n");
break;
}
}
if(p==NULL)
printf("NO\n");
}
}
int main()
{
int exp=0;
while(scanf("%d%d%d",&num_a,&num_b,&num_c)!=EOF)
{
printf("Case %d:\n",++exp);
init();
}
}
hdu2141Can you find it?的更多相关文章
- hdoj-2141-Can you find it?(二分查找)
题目链接 /* Name:HDU-2141-Can you find it? Copyright: Author: Date: 2018/4/12 17:15:46 Description: 暴力,复 ...
随机推荐
- 数组去重js方式
var selectmap = new Array(); /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+selectmap.join(&quo ...
- 【bzoj3119】Book
小清新题,有手有笔就能做出来了…… 先把 $b$ 取相反数,这样写加法好看. 设 $x,y$,使得 $ax+by=m-np$(其实是懒得想文字定义了),该方程与 $x+y=\frac{n(n-1)}{ ...
- Python之时间:time模块
import time 对于时间,使用最频繁的模块 1.获取当前时间 (1)时间戳 time.time() 时间戳:从1970年1月1日0点开始到现在按秒计算的偏移量 (2)时间元组 time.l ...
- pat 甲级 1072. Gas Station (30)
1072. Gas Station (30) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A gas sta ...
- .NET设计模式系列文章《转》
原文发布时间为:2008-11-02 -- 来源于本人的百度文章 [由搬家工具导入] http://www.cnblogs.com/Terrylee/archive/2006/07/17/334911 ...
- saltstack 模块学习之 state
入口文件top.sls 三要素环境:通过file-roots指定目标主机:可以使用通配符*配置文件路径:路径分割符为. 比如a.mysql 表示在环境指定的路径下有个a目录,a目录下有个mysql. ...
- emacs使用http代理打开
环境:ubuntu 15.10 64bit,emacs 24 ,git http代理搭建方法:http://www.cnblogs.com/liuxuzzz/p/5324749.html 原因: 因为 ...
- spring整合jedis时所遇问题
@Test public void testSpringJedisPool(){ ApplicationContext ac = new ClassPathXmlApplicationContext( ...
- LeetCode OJ-- Binary Tree Maximum Path Sum ***
https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 给一棵二叉树,路径可以从任一点起,到任一点结束,但是可以连成一个路径的.求 ...
- AC日记——3的幂的和 51nod 1013
3的幂的和 思路: 矩阵快速幂: sn-1 3 1 sn * = 1 0 1 1 来,上代码: #include <cstdio> ...