zoj 1760 查找

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=760

撸了个二分查找

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int bs(int a[],int n,int len)
 8 {
 9           int lo = 0,hi = len-1,mid = (lo+hi)/2;
10           while(lo<=hi)
11           {
12                     if(n<a[mid])
13                     {
14                               hi = mid -1;
15                               mid = (lo+hi)/2;
16                     }
17                     else if(n>a[mid])
18                     {
19                               lo = mid +1;
20                               mid = (lo+hi)/2;
21                     }
22                     else
23                               return mid;
24           }
25           if(lo>hi)
26                     return -1;
27 }
28
29
30 int main()
31 {
32           int a[10000];
33           int x;
34           while(cin>>x && x!=-1)
35           {
36                     int i = 1;
37                     a[0] = x;
38                     while(cin>>a[i])
39                     {
40                               if(a[i]==0)
41                                         break;
42                               i++;
43                     }
44                     sort(a,a+i);
45                     int count = 0;
46                     for(int j = i-1;j>=0;j--)
47                     {
48                               if(bs(a,a[j]*2,i)!=-1)
49                               {
50                                         count++;
51                               }
52                     }
53                     cout<<count<<endl;
54
55
56           }
57 }
时间: 2024-10-12 23:44:36

zoj 1760 查找的相关文章

zoj 1760

#include<iostream>#include<set>using namespace std;int main(void){ while(true) { set<int> s; int n, doubles = 0; while(cin>>n && n) { if(n == -1) return 0; s.insert(n); if(s.find(2*n) != s.end()) doubles++; if(n%2 == 0 &

zoj 1760 Doubles(set集合容器的应用)

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1760 题目描述: As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many i

ZOJ 1760 &amp;&amp;POJ1552 Doubles (模拟)

链接:click here 题意:叫你求一个数是另一个数的二倍的这样的组合有多少个. 思路:纯模拟,一重循环:读入当前数据组a,并累积数据元素个数n,循环的结束标志是读入数据0,两重循环结构枚举组内所有数据对a[i] a[j] 判断是否成两倍关系 代码: #include <stdio.h> #include <string.h> #include <math.h> #include <iostream> #include <algorithm>

ZOJ 1760 Doubles

Doubles Time Limit: 2 Seconds      Memory Limit: 65536 KB As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list ar

ZOJ 3156 Taxi (二分匹配+二分查找)

题目链接:Taxi Taxi Time Limit: 1 Second      Memory Limit: 32768 KB As we all know, it often rains suddenly in Hangzhou during summer time.I suffered a heavy rain when I was walking on the street yesterday, so I decided to take a taxi back school. I foun

BNU 4260 Trick or Treat &amp;&amp; ZOJ 3386 (三分查找)

[题目链接]click here~~ [题目大意]求x轴上一点到各点的最大值中的最小值 点到线段距离  [解题思路]三分查找 三分查找初学可以参考这篇博客分析:三分查找,写的很详细了,其实跟类似于二分查找,理解了如何构造,代码不难实现 方法1: #include <bits/stdc++.h> using namespace std; const double eps=1e-7; const double inf=0x3f3f3f3f; const int N=55000; int n; st

ZOJ 3640--Missile【二分查找 &amp;&amp; 最大流dinic &amp;&amp; 经典建图】

Missile Time Limit: 2 Seconds      Memory Limit: 65536 KB You control N missile launching towers. Every tower has enough missiles, but for each tower only one missile can be launch at the same time. Before the launching, every missile needT1 seconds

1760: 学生信息查找

#include<stdio.h>struct student{ long no; char name[9]; int score;} ;void input(struct student stu[],int n){ int j; for(j=0;j<n;j++) { scanf("%ld %s %d",&stu[j].no,&stu[j].name,&stu[j].score); }}int search(struct student stu

三分查找

我们都知道 二分查找 适用于单调函数中逼近求解某点的值. 如果遇到凸性或凹形函数时,可以用三分查找求那个凸点或凹点. 下面的方法应该是三分查找的一个变形. 如图所示,已知左右端点L.R,要求找到白点的位置. 思路:通过不断缩小 [L,R] 的范围,无限逼近白点. 做法:先取 [L,R] 的中点 mid,再取 [mid,R] 的中点 mmid,通过比较 f(mid) 与 f(mmid) 的大小来缩小范围. 当最后 L=R-1 时,再比较下这两个点的值,我们就找到了答案. 1.当 f(mid) >