二分训练7.14

懵逼场。

A - 4 Values whose Sum is 0

在这里插入图片描述
输入:

6
-45 22 42 -16
-41 -27 56 30
-36 53 -37 77
-36 30 -75 -46
26 -38 -10 62
-32 -54 -6 45

输出:

5

Hint:

样例解释: (-45, -27, 42, 30), (26, 30, -10, -46), (-32, 22, 56, -46),(-32, 30, -75, 77), (-32, -54, 56, 30).

按列来看有四组数据,求每组数据各出一个数字之和为0的个数。直接暴力会T,用二分就可以把题目简化为O(n^2)的了。
lower_bound 和upper_bound 的使用:

upper_bound(f,f+temp2,-1*temp)-lower_bound(f,f+temp2,-1*temp);	//可以得到在f中值为-1*temp的个数,为0就是没有

代码:

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=4005;
int a[N],b[N],c[N],d[N],e[N*N],f[N*N];
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i]>>b[i]>>c[i]>>d[i];
	int temp1=0,temp2=0;	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			e[temp1++]=a[i]+b[j];
		}
	}	
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			f[temp2++]=c[i]+d[j];
		}
	}	
	sort(e,e+temp1);
	sort(f,f+temp2);	
	int sum=0;	
	for(int i=0;i<temp1;i++)
	{
		int temp=e[i];
		sum+=upper_bound(f,f+temp2,-1*temp)-lower_bound(f,f+temp2,-1*temp);		
	}	
	cout<<sum;
	return 0;
}

B - Pie

在这里插入图片描述
输入:

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

输出:

25.1327
3.1416
50.2655

Hint:

定义pi的方法 const double pi=acos(-1.0);//头文件math.h

三角函数arccos
分饼,大饼可分为小饼,但小饼不能凑成大饼。
二分,上界为所有体积平分,下界为最大值的平分。
参考:https://blog.csdn.net/Love_Jacques/article/details/104140243

#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=1e4+5;
const double pi=acos(-1.0);
double a[N],sum,maxn=-1;
int n,f;

int judge(double mid)
{
	int sum=0;
	for(int i=0;i<n;i++)
	{
		sum+=(int)(a[i]/mid);//一共能分出的蛋糕块数 
	}
	if(sum>=f) return 1;
	else return 0;
}

double binary_search()
{
	double high=sum/f,low=maxn/f,mid=(high+low)/2;//mid是每人的蛋糕大小 
	while(high-low>1e-7)
	{
		if(judge(mid)) low=mid;
		else high=mid;
		mid=(high+low)/2;
	}	
	return mid;
}

int main()
{
	int t;cin>>t;
	while(t--)
	{
		cin>>n>>f;f++;
		memset(a,0,sizeof(a));
		for(int i=0;i<n;i++)
		{
			int temp;cin>>temp;
			a[i]=pi*temp*temp;
			sum+=a[i];
			if(a[i]<maxn) maxn=a[i];
		}
		
		double ans=binary_search();
		printf("%.4f\n",ans);		
	}
}

C - Can you find it?

在这里插入图片描述
输入:

3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10

输出:

Case 1:
NO
YES
NO

其实是找是否有a[i]+b[j]==k[k1]-c[z];
要用二分。

二分的while条件是小于等于!!整数有加一减一之分。

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=505;
int a[N],b[N],c[N],d[N*N],k[1005];
int main()
{
	int Case=1,l,n,m;
	while(cin>>l>>n>>m)
	{
		
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		memset(d,0,sizeof(d));
		memset(k,0,sizeof(k));
		
		for(int i=0;i<l;i++) cin>>a[i];
		for(int i=0;i<n;i++) cin>>b[i];
		for(int i=0;i<m;i++) cin>>c[i];
		
		int k1;cin>>k1;
		for(int i=0;i<k1;i++) cin>>k[i];
		
		printf("Case %d:\n",Case++);
		int tempd=0;
		for(int i=0;i<l;i++)
		{
			for(int j=0;j<n;j++)
			{
				d[tempd++]=a[i]+b[j];
			}
		}
		
		sort(d,tempd+d);sort(c,c+m);
		
		for(int i=0;i<k1;i++)
		{
			int flag=0;
			for(int j=0;j<m;j++)
			{
				int low=0,high=tempd-1,mid=(low+high)/2;
				int temp=k[i]-c[j];
				if(temp<d[0]) break;//k太小
				if(temp>d[high]) continue;//换下一个大点的c
				
				while(low<=high)
				{
					if(d[mid]==temp){flag++;break;
					}	
					
					if(d[mid]<temp) low=mid+1;
					else high=mid-1;
					mid=(high+low)/2;
				} 
				if(flag) break;
			}
			if(flag) cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}

D - A Cubic number and A Cubic Number

在这里插入图片描述
输入:

5
7
13
23
2269
5557

输出:

YES
NO
NO
YES
NO

平方差公式:
由于是素数,则只能分成1和它自己。因此,素数本身等于x2+y2+xy;

x^3-y^3=(x-y)*(x^2+y^2+xy);

数据范围是1e12,因此一定要二分和long long;
二分后范围为1e6;

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
ll a[N];
int main()
{
	//打表
	ll temp=0;
	for(ll i=1;i<=1e6;i++)
	{
		a[temp++]=i*i+i*(i+1)+(i+1)*(i+1);
	}
	sort(a,a+temp);
	int t;cin>>t;
	while(t--)
	{
		ll n;cin>>n;
		int flag=0;
		for(ll i=0;i<temp;i++)
		{
			if(a[i]==n) {flag=1;break;
			}
			if(a[i]>n){break;
			}
		}
		if(flag) cout<<"YES";
		else cout<<"NO";
		cout<<endl;
	}
	return 0;
}

E - Funky Numbers

原题:https://codeforces.com/problemset/problem/192/A
想要求是否存在a[i]+a[j]==n,就求在范围内是否存在n-a[i];

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll a[N];
int main()
{
	for(ll i=1;i<=N;i++) a[i-1]=i*(i+1)/2;
	sort(a,a+N);
	ll n;cin>>n;
	int flag=0;
	for(ll i=0;i<N;i++)
	{
		ll t=lower_bound(a,a+N,n-a[i])-a;
		if(a[i]+a[t]==n)
		{
			flag=1;break;
		}
	}
	if(flag) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
}

F - Burning Midnight Oil

原题:https://codeforces.com/problemset/problem/165/B
要求最小的v,因此是high减小。

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=105;
int n,k;

int check(int nn)
{
	int sum=nn;
	while(nn)
	{
		sum+=nn/k;
		nn/=k;
	}
	if(sum>=n) return 1;
	else return 0;
}

int main()
{
	cin>>n>>k;
	int low=1,high=n,mid=(low+high)/2;
	//求最小的v 因此是high减小 
	while(low<high)
	{
		if(check(mid)) high=mid;
		else low=mid+1;
		
		mid=(low+high)/2;
	}
	cout<<mid;
	return 0;
}

G - Can you solve this equation?

来源:http://acm.hdu.edu.cn/showproblem.php?pid=2199

高中数学题。先求导得知单调性,可知单调递增。
若x0时大于0,或x100时小于0,那都是无零点的。
开始输出的时候发现输出的总是整数,后来发现习惯性的定义函数形参是int型…

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const double eps=1e-6;
double y;

double check(double x)
{
	double temp=8*x*x*x*x+7*x*x*x+2*x*x+3*x+6-y;
	return temp;
}

int main()
{
	int t;cin>>t;
	while(t--)
	{
		cin>>y;
		//增函数
		if(check(0)>eps||check(100)<eps) {
			cout<<"No solution!"<<endl;continue;
		}
		double low=0,high=100,mid=(low+high)*1.0/2;
		
		while(high-low>eps)
		{
			if(check(mid)>0) high=mid;
			else low=mid;
			
			mid=(high+low)/2;
		}
		printf("%.4f\n",high);
	}
	return 0;
}

H - Cup

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2289
杯子是圆台,而且是上大下小的圆台。
圆台公式:pih(RR+rr+R*r)/3;
圆台横截面圆的半径公式:(R顶r底,H总高
(R-r)*h/H+r;

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);
const double eps=1e-8;
double r,R,h,v;

double check(double mid)
{
	double rr=(R-r)*mid/h+r;//顶-底 的比例 +底 
	return pi*mid*(rr*rr+rr*r+r*r)/3; 
}

int main()
{
	//圆台:pi*h*(r*r+R*R+R*r)/3  r底R顶 
	int t;cin>>t;
	while(t--)
	{
		cin>>r>>R>>h>>v;
		double low=0,high=h,mid=(low+high)/2;
		while(high-low>eps)
		{
			if(check(mid)>v) high=mid;
			else low=mid;
			
			mid=(high+low)/2;
		}
		printf("%.6f\n",mid);
	}
	return 0;
}

I - Strange fuction

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2899
求最小值,高中数学。
已知二阶导恒大于零,那么一阶导数单调递增。若y<0,则函数f(x)单调递增,x=0的点为最小值。若y>0,则要二分出拐点,此点上的函数值为最小值。

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int N=105;
double y;
const double eps=1e-6;

double check1(double mid)
{
	return 42*mid*mid*mid*mid*mid*mid+48*mid*mid*mid*mid*mid+21*mid*mid+10*mid-y;
}
double check2(double mid)
{
	return 6*mid*mid*mid*mid*mid*mid*mid+8*mid*mid*mid*mid*mid*mid+7*mid*mid*mid+5*mid*mid-y*mid;
} 

int main()
{
	// 一阶导是单增的:42*x^6+48*x^5+21*x^2+10*x-y
	int t;cin>>t;
	while(t--)
	{
		cin>>y;
		double guai;
		if(y<0) 
		{
			guai=-1;
		}
		else
		{
			//找拐点 
			double low=0,high=100,mid=(low+high)/2;
			while(high-low>eps)
			{
				if(check1(mid)>0) high=mid;
				else low=mid;
				mid=(high+low)/2;
			}
			guai=mid;
		}
		
		double low=0,high=100,mid=(low+high)/2;
		if(guai==-1) //fx单增,直接冲 
		{
			double ans=check2(0);
			printf("%.4f\n",ans);
			continue;
		}
		else //fx [0,guai]减 [guai,100]增 
		{
			double ans=check2(guai);
			printf("%.4f\n",ans);
			continue;
		} 
	}
	return 0;
}

J - Shell Pyramid

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2446

K - Hard Process

原题:https://codeforces.com/problemset/problem/660/C

L - Tavas and Karafs

原题:https://codeforces.com/problemset/problem/535/C

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

karshey

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值