HDU 2143 Can you find it?(基础二分)

Can you find it?

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)
Total Submission(s): 42520    Accepted Submission(s): 10315

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 3

1 2 3

1 2 3

1 2 3

3

1

4

10

Sample Output

Case 1:

NO

YES

NO

Author

wangye

Source

HDU 2007-11 Programming Contest

Recommend

威士忌   |   We have carefully selected several similar problems for you:  2899 2289 1597 1551 2298

今天从基础开始学起,以为二分挺容易的,但是忽视了题目需要考虑时间复杂度,会不会爆int,等等。

存不存在ai,bi,ci加起来是x。

如果直接枚举那也太简单了吧,我就想着,大循环a,小循环b,小小循环对c二分,发现这样也超时!!!

然后,我就大循环a,小循环对b二分,小小循环对c二分,结果这样的思路是完全错的!!!

然后看看题解,要先把a+b的所有可能都存起来放到sum里,再大循环c,对sum二分。开始还觉得这个思路不是和我一开始的思路差不多吗?仔细一想,我大循环a,小循环b,一方面会有很多重复的a+b,重复带入算,费时,另一方面,每输入一个x,又要大循环啊,小循环b的,很费时。

#include <iostream>
#include <stack>
#include <string.h>
#include <stdio.h>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int a[505];
int b[505];
int c[505];
int sum[500005];
int main()
{
    int L,N,M;
    int k=0;
    while(~scanf("%d %d %d",&L,&N,&M))
    {
        k++;
        for(int i=1;i<=L;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1;i<=N;i++)
        {
            scanf("%d",&b[i]);
        }
        for(int i=1;i<=M;i++)
        {
            scanf("%d",&c[i]);
        }
        int n,x;
        printf("Case %d:\n",k);
        scanf("%d",&n);
        int p=1;

        for(int i=1;i<=L;i++)
        {
            for(int j=1;j<=N;j++)
            {
                sum[p++]=a[i]+b[j];
            }
        }
        sort(sum+1,sum+p);
        sort(c+1,c+1+M);
        while(n--)
        {
            scanf("%d",&x);
            bool f=0;
            for(int i=1;i<=M;i++)
            {
                if(sum[1]>x-c[i])//不能写成sum[1]+c[1]>x,因为可能会爆int
                    break;
                int le=1;int ri=p-1;
                while(le<=ri)
                {
                    int mid=(le+ri)/2;
                    if(sum[mid]==x-c[i])
                    {
                        f=1;
                        break;
                    }
                    else if(sum[mid]>x-c[i])
                    {
                        ri=mid-1;
                    }
                    else if(sum[mid]<x-c[i])
                        le=mid+1;
                }
                if(f)
                    break;
            }
            if(f)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}       

原文地址:https://www.cnblogs.com/caiyishuai/p/9527220.html

时间: 2024-11-13 09:43:40

HDU 2143 Can you find it?(基础二分)的相关文章

hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分)

Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 288    Accepted Submission(s): 108 Problem Description Long long ago, there lived a lot of rabbits in the forest. One day, the

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

hdu 1829 A Bug&#39;s Life (基础并查集)

题目: 链接:点击打开链接 题意: 给定虫子的交配关系,确定实验是否支持教授的假设即没有同性恋或者不符合假设. 思路: 是一道基础的并查集题目.存在两个集合异性和同性,给出多组关系,看这两个集合有木有联系,即是否有同性恋. 定义一个数组sex[],sex[i]表示与编号i的性别相反的虫子编号.然后将和i虫子有联系的合并为同一个集合(认为是同性的).如果findset(u) == findset(v),出现了反常行为. 代码: #include <iostream> #include <c

hdu 4004 The Frog&#39;s Games【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 3980    Accepted Submission(s): 1931 Problem Description The annual Games in frogs' kingdom started again. The most famous game

HDU 3729 I&#39;m Telling the Truth (二分匹配)

题意:给定 n 个人成绩排名区间,然后问你最多有多少人成绩是真实的. 析:真是没想到二分匹配,....后来看到,一下子就明白了,原来是水题,二分匹配,只要把每个人和他对应的区间连起来就好,跑一次二分匹配,裸的. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #inc

hdu 4004 The Frog&#39;s Games 【二分】

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 4565    Accepted Submission(s): 2225 Problem Description The annual Games in frogs' kingdom started again. The most famous game

hdu 2147 kiki&#39;s game, 入门基础博弈

博弈的一些概念: 必败点(P点) : 前一个选手(Previous player)将取胜的位置称为必败点. 必胜点(N点) : 下一个选手(Next player)将取胜的位置称为必胜点. 必败(必胜)点属性 (1) 所有终结点是必败点(P点): (2) 从任何必胜点(N点)操作,至少有一种方法可以进入必败点(P点): (3)无论如何操作, 从必败点(P点)都只能进入必胜点(N点). hdu 2147 kiki's game 题意: 在一个m*n的棋盘内,从(1,m)点出发,每次可以进行的移动是

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

HDU 2254 奥运(矩阵快速幂+二分等比序列求和)

HDU 2254 奥运(矩阵快速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 根据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k天后就算矩阵的k次方. 这样就变成:初始矩阵的^[t1,t2]这个区间内的v[v1][v2]的和. 所以就是二分等比序列求和上场的时候了. 跟HDU 1588 Gauss Fibonacci的算法一样. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * B