二分(二分查找和二分答案)

列车调度

火车站有一些用于列车调度的铁轨,其两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道。每趟列车从入口可以选择任意一条轨道进入,最后从出口离开。在图中有9趟列车,在入口处按照{8,4,2,5,3,9,1,6,7}的顺序排队等待进入。如果要求它们必须按序号递减的顺序从出口离开,则至少需要多少条平行铁轨用于调度?

输入格式:
输入第一行给出一个整数N (2 ≤ N ≤105​​ ),下一行给出从1到N的整数序号的一个重排列。数字间以空格分隔。
输出格式:
在一行中输出可以将输入的列车按序号递减的顺序调离所需要的最少的铁轨条数。

输入样例:
9
8 4 2 5 3 9 1 6 7
输出样例:
4

很容易想到此题的关键是找最长下降子序列的个数。
暴力算法:O(n2),当场TLE

#include<iostream>
#include<cstdio>
using namespace std;
int a[100010];
bool b[100010];
int main(){
	int n,ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	while(1){
		int k,i;
		for(k=1;k<=n;k++)
			if(!b[a[k]]){
				b[a[k]]=1;ans++;break;
			}
		if(k==n) break;
		for(i=k+1;i<=n;i++)
			if(!b[a[i]]&&a[i]<a[k]){
				k=i;b[a[i]]=1;
			}
	}
	printf("%d",ans);
	return 0;
}

二分算法:O(n),二分大法好啊

#include<iostream>
#include<cstdio>
using namespace std;
int rode[100010],cnt,n;
int main(){
	scanf("%d",&n);
	int x;
	for(int i=1;i<=n;i++){
		scanf("%d",&x);
		if(cnt==0||rode[cnt]<x) rode[++cnt]=x;//由于n个数各不相同,果断二分
		else{
			int l=1,r=cnt;
			while(l<r){
				int mid=l+r>>1;
				if(rode[mid]<x) l=mid+1;
				else r=mid;
			}
			rode[l]=x;
		}
	}
	printf("%d",cnt);
	return 0;
} 

A Cubic number and A Cubic Number

判断某个素数是否可能由两个数的立方差所得到。

Input
第一行输入一个整数t(1<=t<=100),表示询问次数。
接下来的t行每行有一个素数p(2<=p<=10^12)。
Output
对于每次询问,如果可能则输出“YES”,否则输出“NO”。

Sample Input
5
7
13
23
2269
5557
Sample Output
YES
NO
NO
YES
NO

暴力:O(nm)
二分查找:O(nlogm)

核心代码:
int queryl(int x){//找第一个大于等于x的数
    int l=1,r=n;
    while(l<r){
        int mid=l+r>>1;
        if(a[mid]<x) l=mid+1;//确保l左侧的数都小于x
         else r=mid;
    }
    if(a[l]>=x) return l;
     else return n+1;
}
int queryr(int x){//找第一个小于等于x的数
    int l=1,r=n;
    while(l<r){
        int mid=l+r+1>>1;
        if(a[mid]>x) r=mid-1;//确保r右侧的数都大于x
         else l=mid; 
    }
    if(a[r]<=x) return r;
     else return 0;
}
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
ll f(ll x){return 3*x*x+3*x+1;}
int main(){
	ll t,x;
	scanf("%lld",&t);
	while(t--){
		scanf("%lld",&x);
	    ll l=1,r=1000000,mid; 
		while(l<r){
			mid=l+r>>1;
			if(f(mid)<x) l=mid+1;//x为素数,a^3-b^3=3*b^2+3*b+1,找b,很大的取值范围用long long
			else r=mid;
		}
		if(f(l)==x) puts("YES");
		else puts("NO");
	}
	return 0;
} 

Cable master

Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants using a “star” topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter, and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Input
The input consists of several testcases. The first line of each testcase contains two integer numbers N and K, separated by a space. N (1 ≤ N ≤ 10000) is the number of cables in the stock, and K (1 ≤ K ≤ 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.
The input is ended by line containing two 0’s.
Output
For each testcase write to the output the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output must contain the single number “0.00” (without quotes).

Sample Input
4 11
8.02
7.43
4.57
5.39
0 0
Sample Output
2.00

把m段绳子割成一样长的n段,求最大长度,保留两位小数
暴力:做不出来
二分答案:O(1000*m)

核心代码:
double l=0,r=100010,mid;
	    int i=1000;
	    while(i--){
		    mid=(l+r)/2;
		    if(cut(n,mid)>=m) l=mid;
		    else r=mid;
	    }
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int MAXN=100010;
double a[MAXN];
int cut(int n,double x){
	int ans=0;
	for(int i=0;i<n;i++) ans+=(int)(a[i]/x);//高级别的数据类型转换成低级别的需要强转
	return ans;
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m),n!=0){
	    for(int i=0;i<n;i++) scanf("%lf",a+i);
	    double l=0,r=100010,mid;
	    int i=1000;
	    while(i--){
		    mid=(l+r)/2;
		    if(cut(n,mid)>=m) l=mid;
		    else r=mid;
	    }
	    printf("%.2lf\n",floor(r*100)/100);//浮点数两位小数向下取整r:2.0025000000003908
	   // printf("%.19lf\n",l);//不能输出l:2.00249999999999467
	}
	return 0;
}

Can you solve this equation?

已知方程:8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y
找它在[0,100]之间的实数解。
虽然xhk数学不好,但他还是利用编程轻松解决了这个问题。现在,他想考考你。

Input
输入的第一行包含一个整数T(1 <= T <=100),表示测试用例的数目。接下来T个数字,每一行都有一个实数Y(abs(Y)<=1e10);
Output
对于每个测试用例,如果有解,你应该输出一个实数(精确到小数点后4位,四舍五入),如果在0到100之间无解,就输出“No solution!”。

Sample Input
2
100
-4
Sample Output
1.6152
No solution!

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-6;
double f(double x){
	return 8*x*x*x*x+7*x*x*x+2*x*x+3*x+6;
}
int main(){
	int t;double q;
	scanf("%d",&t);
	while(t--){
		scanf("%lf",&q);
		double l=0,r=100,mid;
		int i=1000;
		while(i--){
			mid=(l+r)/2;
			if(f(mid)-q>0) r=mid;//二分答案应用
			else l=mid;
		}
		if(fabs(f(r)-q)<eps) printf("%.4lf\n",r);//判断表达式是否成立
		else printf("No solution!\n");
	}
	return 0;
}

Strange fuction

F(x) = 6 * x7+8x6+7x3+5x2-yx (0 <= x <=100) ,y值一定时,求F(x)的最小值(0 <= x <=100)

Input
第一行包含一个整数Y(1<=T<=100) ,代表测试样例的个数。接下来输入包含T行,每行一个实数y(0 < y <1e10)。
Output
输出一行包含一个实数F(x)(0 <= x <=100),保留4位小数。

Sample Input
2
100
200
Sample Output
-74.4291
-178.8534

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const double eps=1e-6;
double f0(double x,double y){
   return 6*x*x*x*x*x*x*x+8*x*x*x*x*x*x+7*x*x*x+5*x*x-y*x;
}
double f1(double x,double y){
	return 42*x*x*x*x*x*x+48*x*x*x*x*x+21*x*x+10*x-y;
}
int main(){
	int t;double y;
	scanf("%d",&t);
	while(t--){
		scanf("%lf",&y);
		double l=0,r=100,mid;
		int i=1000;
		while(i--){
			mid=(l+r)/2;
			if(f1(mid,y)>0) r=mid;//求最小值转换成求导数为零的点
			else l=mid;
		}
		printf("%.4lf\n",f0(r,y));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值