hdu 2141 纯水题

题意:

     给三列数,在每一个列中找个数,使之相加等于给定的x。

解:

       本来想写二分的,多校曾经出过类似的,但是给的是五列数,它给了个solution,当时没用那个写,用的是哈希。于是,我就用这个类似快速查找的方法去做了,1Y,有木有。

      就是搞成两列,一个指针指向第一列最小的,第二个指针指向第二列最大的,然后都往相反的方向移动。

/*
Pro: 0

Sol:

date:
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <vector>
const int maxn = 510;
using namespace std;
int a[maxn],b[maxn],c[maxn],bc[maxn * maxn],m,n,l,s,x,ca,sub;
bool find(int x){
     int pa = 0; int pbc = sub - 1;
     while(pa < l && pbc >= 0){
         if(a[pa] + bc[pbc] == x) return true;
         else if(a[pa] + bc[pbc] < x) pa ++;
         else pbc --;
     }
     return false;
}
int main(){
    ca = 1;
    while(scanf("%d%d%d",&l,&n,&m)!= EOF){
        printf("Case %d:\n",ca ++);
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(bc,0,sizeof(bc));
        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);
        sub = 0;
        for(int i = 0; i < n; i ++){
            for(int j = 0; j < m; j ++){
                bc[sub ++] = b[i] + c[j];
            }
        }
        sort(a,a + l);  sort(bc,bc + sub);
        scanf("%d",&s);
        for(int i = 0; i < s; i ++){
            scanf("%d",&x);
            if(find(x)) printf("YES\n");
            else printf("NO\n");
        }
    }
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值