HDU - 2141 Can you find it?(二分)

题目链接:点我点我

题意:给出L个A,N个B,M个C,然后S个X,求能否找出解使得Ai+Bj+Ck = X.成立

题解:TLE了一晚上。这道题其实就是个暴力二分,前把三组数中任意两组数先合并,只不过最后的时候转变一下思想,不要直接去求使X成立的条件,

而是反过去把X当作条件(但是有些大神直接set+set的find()函数过了,当我没说,(捂脸逃....)

1.巧妙暴力二分

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int N=300000;
 6 int x[N],y[N],z[N],t[N];
 7
 8 int solve(int l,int r,int tar){
 9     while(l<=r){
10         int mid=(l+r)>>1;
11         if(z[mid]==tar) return mid;
12         else if(z[mid]>tar) r=mid-1;
13         else if(z[mid]<tar) l=mid+1;
14     }
15     return -1;
16 }
17
18 int main(){
19     int a,b,c,d,tmp,Case=1;
20     while(scanf("%d %d %d",&a,&b,&c)!=EOF){
21         printf("Case %d:\n",Case++);
22         int cnt=0;
23         for(int i=0;i<a;i++) scanf("%d",&x[i]);
24         for(int i=0;i<b;i++) scanf("%d",&y[i]);
25         for(int i=0;i<c;i++){
26             scanf("%d",&tmp);
27             for(int j=0;j<b;j++){
28                 z[cnt++]=tmp+y[j];
29             }
30         }
31         scanf("%d",&d);
32         for(int i=0;i<d;i++) scanf("%d",&t[i]);
33         sort(x,x+a);
34         sort(z,z+cnt);
35         for(int i=0;i<d;i++){
36             int flag=0;
37             for(int j=0;j<a;j++){
38                 if(solve(0,cnt-1,t[i]-x[j])!=-1) {flag=1;break;}
39             }
40             if(flag) printf("YES\n");
41             else printf("NO\n");
42         }
43
44     }
45     return 0;
46 }

2.set暴力二分

时间: 2024-11-19 20:25:22

HDU - 2141 Can you find it?(二分)的相关文章

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 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 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

HDU 4004 The Frog&#39;s Games 二分 贪心

戳这里:HDU 4004 //思路:二分经典入门题...贪心判方案是否可行 1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

HDU 4004 The Frog&#39;s Games 二分

1.题意:一条河长为L,河上有N块石头,一只青蛙可以利用这些石头从河的一端跳到对岸,可以跳不超过M次,求各种跳法中,找到最小化的最大步长.输入第一行依次给出L.N.M,第二行依次给出N块石头距离起点的距离. 2.分析:这类最小化最大值的问题用二分来求解最高效.先预先处理,连同起点,终点,共N+2个点排序,两两相减得到N+1段距离C[i],即跨一步的最小距离.二分维护mid值,上界为河长L,下界为两两距离之中的最大值maxC[i](反证易知),以mid值分割C[i]数组,分割的段数<=M则mid偏

HDU - 1845 Jimmy’s Assignment (二分匹配)

Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignment is to find a maximum matching in a special kind of graph. This graph is undirected, has N vertices and each vertex has degree 3. Furthermore, the gr

hdu 4938 Seeing People 排序+二分查找

Seeing People Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 241    Accepted Submission(s): 61 Problem Description There are two kinds of people. If person i is the first kind of people, it

hdu 3641 Treasure Hunting 强大的二分

1 /** 2 大意:给定一组ai,bi . m = a1^b1 *a2^b2 * a3^ b3 * a4^b4*...*ai^bi 3 求最小的x!%m =0 4 思路: 将ai 质因子分解,若是x!%m=0 那么x! 质因子分解之后 质因子的个数一定大于等于m的个数.二分求解可得 5 注意: 二分时,需要将,上下限 设定好,low =0: high = 1ll<<60; 6 **/ 7 8 #include <iostream> 9 #include <cstring&g