枚举,二分

二分模板

① 普通的整数二分
int l, r, res;
while(l <= r){
int mid = (l + r) / 2;
if (ok (mid)){
res = mid;
r = mid - 1; 
}
else{
l = mid + 1; // 满足条件的最小值
}
}

精度精确型二分
const double eps = 1e-7;
double l, r;
while (l + eps < r){
double mid = (l + r) / 2.0;
if (ok (mid)){
l = mid;
}
else {
r = mid;
}
}

HDU4151 http://acm.hdu.edu.cn/showproblem.php?pid=4151

先是枚举,然后是二分^...^

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<string.h>
 5 using namespace std;
 6 bool judge[10];
 7 int m,n;
 8 bool check(int x){
 9 memset(judge,false,sizeof(judge));
10 bool flag=true;
11 while(x){
12 int temp=x%10;
13 x/=10;
14 if(judge[temp]){
15     flag=false;
16     break;
17 }
18 judge[temp]=true;
19
20 }return flag;
21 }
22 int arr[100000000];
23 int main(){
24     int tot=0;
25 for(int i=1;i<10000000;++i){
26     if(check(i))arr[tot++]=i;
27 }
28
29 while(cin>>n){
30     int r=tot-1;int l=0;
31     int mid,res;
32     if(n<=1){
33         cout<<"0"<<endl;
34         continue;
35     }
36     while(l<=r){
37         mid=(l+r)/2;
38         if(arr[mid]<n)
39         {l=mid+1;
40         res=mid;
41         }
42         else r=mid-1;
43     }
44     int s=res+1;
45     cout<<s<<endl;
46 }
47 }

时间: 2024-08-02 11:00:37

枚举,二分的相关文章

hdu4430之枚举+二分

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2549    Accepted Submission(s): 522 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

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

Eqs 折半枚举+二分查找 大水题

Eqs 题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50]. 求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}. 思路:折半枚举+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inclu

UVA10277 - Boastin&#39; Red Socks(枚举+二分)

UVA10277 - Boastin' Red Socks(枚举+二分) 题目链接 题目大意:现在有m只红袜子,n只黑袜子,这样总袜子total = n + m;现在给你p, q,确定n和m,使得从这些袜子中取两只都是红袜子的概率等于p/q:如果没有这样的n和m满足要求输出impossible; 解题思路:m *(m - 1) / (total * (total - 1)) = p /q; 那么我们只需要枚举total,就可以解到m.但是会有精度误差,貌似有解决的办法,但是觉得没法理解.后面看了

poj 3977 Subset 枚举+二分

首先分成一半2^17和2^18,并且把其中一半变成相反数,然后枚举一半二分查找另一半,在找到的位置前后也找找. 这里用到了二级排序,有很多细节要处理,不多说了. 巨坑的一个地方就是,不能用系统的abs,要自己手写,简直坑死.. #include<cstdio> #include<algorithm> #include<iostream> #include<map> using namespace std; typedef long long ll; stru

POJ 3977Subset(枚举+二分)

Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 1562   Accepted: 261 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of

Codeforces 496D Tennis Game 枚举+二分

题目链接:点击打开链接 题意: 给定n场比赛. 下面n个数字:表示该场是1获胜还是2获胜. 1.胜利者获得一分. 2.若已经决出整个赛季的胜负则比赛不会继续. 3.设要赢得这个赛季需要赢有s局,每局先获得t分的选手胜利. 问: 找出所有的(s,t)组合使得给定的n场比赛记录合法. 输出要排序. 枚举t. a数组存第一个人赢的哪些场次. b数组存第二个人赢的哪些场次. 设赢t分为一句.则判断 第一个人再赢t分是第几场,第二个人再赢t分是第几场. 显然先赢得t分的人赢了这场. 这样同时跑2个人的场数

51nod1010(枚举+二分)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 题意:中文题诶- 思路:求第一个比 x (1<=x<=1e18)大或者等于的数y, 且y的因子只有2, 3, 5,即y=pow(2, i)*pow(3, j)*pow(5, k); 那么显然我们可以通过枚举i, j, k求出所有由2, 3, 5因子构成的数,并将其存入数组中,再二分查找数组中第一个大于等于x的数即为答案: 代码: 1 #include

HDU 4282 A very hard mathematic problem --枚举+二分(或不加)

题意:问方程X^Z + Y^Z + XYZ = K (X<Y,Z>1)有多少个正整数解 (K<2^31) 解法:看K不大,而且不难看出 Z<=30, X<=sqrt(K), 可以枚举X和Z,然后二分找Y,这样的话不把pow函数用数组存起来的话好像会T,可以先预处理出1~47000的2~30次幂,这样就不会T了. 但是还可以简化,当Z=2时,X^2+Y^2+2XY = (X+Y)^2 = K, 可以特判下Z= 2的情况,即判断K是否为平方数,然后Z就可以从3开始了,这样的话X^