HDU 2578(二分查找)

686MS

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <algorithm>
 5
 6 using namespace std;
 7
 8 int a[100010];
 9
10 int n;
11
12 bool bsearch(int key)
13 {
14           int lo = 0,hi = n-1,mid = lo+(hi-lo)/2;
15           while(lo<=hi)
16           {
17                     if(key==a[mid])
18                               return true;
19                     else if(key >a[mid])
20                     {
21                               lo = mid+1;
22                               mid = lo+(hi-lo)/2;
23
24                     }
25                     else
26                     {
27                               hi = mid-1;
28                               mid =lo+(hi-lo)/2;
29                     }
30           }
31           return false;
32 }
33
34 int main()
35 {
36     int t,m,i;
37     scanf("%d",&t);
38     while (t--)
39     {
40         scanf("%d%d",&n,&m);
41         for(i = 0;i<n;i++)
42                               scanf("%d",&a[i]);
43                     int count = 0;
44
45                     sort(a,a+n);
46
47                     for(i = 0;i<n;i++)
48                     {
49                               if(a[i]>m/2) break;
50
51                               if(a[i] == a[i-1])
52                                         continue;
53
54                               if(2*a[i]==m)
55                                         count++;
56                               else if(bsearch(m-a[i]))
57                                         count+=2;
58                     }
59                     printf("%d\n",count);
60     }
61     return 0;
62 }
时间: 2024-10-13 10:14:50

HDU 2578(二分查找)的相关文章

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 5592 ZYB&#39;s Premutation (线段树+二分查找)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=5592 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutation,now he ask you to  restore the premutation.Pair (i,j)(i<j) is considered as a reverse

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

hdu 5249 KPI 【二分查找】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5249 分析:这道题是2015百度之星初赛1的4题 这道题不算太难当时队友做出来了,不过费了老大劲,其实我从中能够吸取教训的, 原因是,我一看这道题就是数据结构的,然后和队友想的一样二分查找,但是从中 遇到了一系列的问题: 首先储存数据我们不能用带有下标的数组,因为题目中的数据是可删可添的这样如果用 数组的话时间复杂度是相当高的,因为如果按大小插入,就要移动其他数据,如果在 尾部添加,就要对其排序,因

hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676 ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 594    Accepted Submission(s): 257 Problem Description ztr loves luck

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 4768 Flyer(二分查找)

Flyer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1537    Accepted Submission(s): 552 Problem Description The new semester begins! Different kinds of student societies are all trying to adv

HDU 5288 OO&#39;s sequence (2015多校第一场 二分查找)

OO's Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 955    Accepted Submission(s): 358 Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the nu

HDU 4941 Magical Forest(map映射+二分查找)杭电多校训练赛第七场1007

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4941 解题报告:给你一个n*m的矩阵,矩阵的一些方格中有水果,每个水果有一个能量值,现在有三种操作,第一种是行交换操作,就是把矩阵的两行进行交换,另一种是列交换操作,注意两种操作都要求行或列至少要有一个水果,第三种操作是查找,询问第A行B列的水果的能量值,如果查询的位置没有水果,则输出0. 因为n和m都很大,达到了2*10^9,但水果最多一共只有10^5个,我的做法是直接用结构体存了之后排序,然后m