hdu 2141 二分

题意是给你三个数组   分别有n m k个数     从三个数组里分别拿一个数能不能等于s

刚开始没注意数可以为负数    wa了好多次

别想直接暴力  肯定超时

现将两个数组合并  暴力枚举数少的      二分枚举数多的(非常省时)

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    int num1[510],num2[510],num3[510];
    int mark[300000];
    int i,j,k,p,d=1;
    int n,m;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        for(i=1;i<=n;i++)
        scanf("%d",&num1[i]);
        for(i=1;i<=m;i++)
        scanf("%d",&num2[i]);
        p=0;
        for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        mark[++p]=num1[i]+num2[j];
        sort(mark+1,mark+1+p);
        for(i=1;i<=k;i++)
        scanf("%d",&num3[i]);
        sort(num3+1,num3+1+k);
        int s;
        scanf("%d",&s);
        printf("Case %d:\n",d++);
        while(s--)
        {
            int sum;
            scanf("%d",&sum);
            int flash=0;
            int left,right,mid;
            for(i=1;i<=k;i++)
            {
                left=1;
                right=p;
                while(left<=right)
                {
                    mid=(left+right)/2;
                    if(mark[mid]>sum-num3[i]) right=mid-1;
                    else if(mark[mid]<sum-num3[i]) left=mid+1;
                    else
                    {
                        flash=1;
                        break;
                    }
                }
                if(flash) break;
            }
            if(flash) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}
时间: 2024-08-08 01:16:07

hdu 2141 二分的相关文章

Can you find it?(hdu 2141 二分查找)

Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 19416    Accepted Submission(s): 4891 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 2141 Can you find it?(二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 题目大意:查找是否又满足条件的x值. 这里简单介绍一个小算法,二分查找. 1 /* 2 3 x^2+6*x-7==y 4 输入y 求x 精确度为10^-5 5 0=<x<=10000 6 7 */ 8 #include <iostream> 9 #include <cstdio> 10 using namespace std; 11 int main (void) 1

hdu 2255 二分图带权匹配 模板题

模板+注解在 http://blog.csdn.net/u011026968/article/details/38276945 hdu 2255 代码: //KM×î´ó×îСƥÅä #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define INF 0x0fffffff const int MAXN

Hdu 2389 二分匹配

题目链接 Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Others)Total Submission(s): 2644    Accepted Submission(s): 823 Problem Description You’re giving a party in the garden of your villa by the sea. T

hdu 4737 二分或暴力

http://acm.hdu.edu.cn/showproblem.php?pid=4737 Problem Description There are n numbers in a array, as a0, a1 ... , an-1, and another number m. We define a function f(i, j) = ai|ai+1|ai+2| ... | aj . Where "|" is the bit-OR operation. (i <= j)

HDU 3656 二分+dlx判定

Fire station Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1308    Accepted Submission(s): 434 Problem Description A city's map can be seen as a two dimensional plane. There are N houses in

HDU 2141(二分&amp;三分 _B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 ----------------------------------------------------------------------------------- 题意:三个数组,找到每个数组中加和为M的值的组数. 思路:首先将后两个数组加和 O(N^2),排序 O(NlogN),然后利用二分求解. 代码: #include<cstdio> #include<cstring>

HDU 2141 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): 21485    Accepted Submission(s): 5446 Problem Description Give you three sequences of numbers A, B, C, then we give you a number

hdu 2141 (二分)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2141 Can you find it? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/10000 K (Java/Others)Total Submission(s): 11503    Accepted Submission(s): 3021 Problem Description Give you three seque