8.14 训练11 分治:二分答案,三分区间

8.14 训练11

Cable master

The Frog’s Games

Pie

Strange fuction

Error Curves


Cable master

Problem Description
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

问题描述 就是给你n个绳子,分成m个等长的,绳子最长是多长
方法 需要枚举答案,二分答案枚举即可。最小值为0,最大值为总长/m。二分答案和二分区间有许些不同就是二分答案是闭区间,即[start,end]。此题在递归边界处有可能会start== end或start==end-1,后者需要分别考虑,前者则直接返回。

//You has the final say in what kind of life you want.
#include<iostream>
#define MAX 10000+10
using namespace std;
int cable[MAX];
int n,k;
int howmany(long long x)
{
	int ans=0;
	for(int i=0;i<n;i++)
		ans+=cable[i]/x;

	return ans;
}
long long solve(long long start,long long end)
{
//	cout<<start<<","<<end<<endl;
	if(start==end)
		return start;
	if(start==end-1)
	{
		if(howmany(end)==k)
			return end;
		else return start;
	}
	long long mid=(start+end)>>1;
	int quan=howmany(mid);
//	cout<<mid<<"->"<<quan<<endl;
	if(quan>=k)
		return solve(mid,end);
	else return solve(start,mid-1);	
}


int main()
{
	double d;
	long long sum=0;
	while(1)
	{
		scanf("%d%d",&n,&k);
		if(!n&&!k)
			break;
		for(int i=0;i<n;i++)
		{
			scanf("%lf",&d);
			cable[i]=(d+0.001)*100;
			sum+=cable[i];
		}
	//	cout<<"**"<<solve(0,sum/k+1);
		printf("%.2lf\n",double(solve(0,sum/k+1))/100);
	}
	return 0;
} 


The Frog’s Games

Problem Description
The annual Games in frogs’ kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog’s longest jump distance).
Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.
Output
For each case, output a integer standing for the frog’s ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11
2
18
Sample Output
4
11
问题描述一条长为l的河,中间有n个石头。求跳跃距离最大值的最小值
方法 典型的最大值最小化问题,二分答案。然后判断这个答案的距离需要跳几次,如果中间跳不过去了直接返回无穷大。
关于二分答案有个小窍门,先写上 k>m k<m的情况,再根据题意添加等号。
WA的原因 因为这个题存在数组1-n里,sort少排了个元素,防不胜防,真蠢

//You has the final say in what kind of life you want.
#include<iostream>
#include<algorithm>
#define MAX 500000+10
#define INF 0x3f3f3f3f
using namespace std;
int stone[MAX];
int n,m;
int len;
int jump(int x)
{
	int cnt=0;
	int last=0;
	int i=1;
	while(i<=n+1)
	{
		if(stone[i]-stone[last]>x)
			return INF;
		while(i<=n+1&&stone[i]-stone[last]<=x)
			i++;
		cnt++;
		last=i-1;
	}
	return cnt;
}

int solve(int start,int end)
{
	if(start==end)
		return start;
	if(start==end-1)
	{
		if(jump(start)==m)
			return start;
		return end;
	}
	int mid=(start+end)>>1;
	int j=jump(mid);
	if(j<=m)
		return solve(start,mid);
	if(j>m)
		return solve(mid,end);
}

int main()
{
	while(scanf("%d%d%d",&len,&n,&m)!=EOF)
	{
		stone[0]=0;
		stone[n+1]=len;
		for(int i=1;i<=n;i++)
			scanf("%d",&stone[i]);
		sort(stone+1,stone+n+1);
		printf("%d\n",solve(0,len));		
	}
	return 0;
	
}

Pie

Problem Description
My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.

My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size.

What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.
Input
One line with a positive integer: the number of test cases. Then for each test case:
—One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.
—One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.
Output
For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).
Sample Input
3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2
Sample Output
25.1327
3.1416
50.2655
问题描述这个和第一题是一个问题,给你n个半径为ri的饼,分成f+1份等大的。
方法 二分答案,看每个答案能分成几份,再用上题的窍门判断是往左还是往右。
WA的原因坑死了,后面几个题都坑死了。都是精度问题。这个题因为是园么,要xPi。首先就是Pi要取acos(-1.0)。然后我错在哪里了呢?我是在答案最后xPi的,对r^2进行二分。最后答案对拍了数十次都没出错没想到问题出在了这里。

//You has the final say in what kind of life you want.
#include<cmath>
#include<iostream>
#include<cstdio>
#define Pi acos(-1.0)
#define MAX 10000+10
#define MIN 0.0000001
using namespace std;
double cake[MAX];
int n,f;
int eat(double x)
{
	int ans=0;
	for(int i=0;i<n;i++)
		ans+=int(cake[i]/x);
	return ans;
}
double solve(double start,double end)
{
//	cout<<start<<","<<end<<endl;
	if(start-end>=-MIN)
		return start;
	
	double mid=(start+end)/2;
	int e=eat(mid);
	if(e<f)
		return solve(start,mid);
	else if(e>=f)
		return solve(mid,end);
}
int main()
{
	double sum=0;
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&f);
		++f;
		for(int i=0;i<n;i++)
		{
			scanf("%lf",&cake[i]);
			cake[i]*=cake[i]*Pi;
			sum+=cake[i];
		}
		printf("%.4lf\n",solve(0,sum/f+1));
	}
	return 0;
}

Strange fuction

Problem Description
Now, here is a fuction:
F(x) = 6 x^7 +8x^6 +7x^3 +5x^2 -y*x (0 <= x <=100)
Can you find the minimum value when x is between 0 and 100.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has only one real numbers Y.(0 < Y <1e10)
Output
Just the minimum value (accurate up to 4 decimal places),when x is between 0 and 100.
Sample Input
2
100
200
Sample Output
-74.4291
-178.8534
问题描述在[0,100]找函数F(x)的最小值
方法 典型的凹区间找最小值
WA原因依旧是精度问题,要1e-6才行

//You has the final say in what kind of life you want.
#include<iostream>
#include<cstdio>
#include<cmath>
#define MIN 0.000001
using namespace std;
double powx[10];
int y;
double fun(double x)
{
	//6 * x^7+8*x^6+7*x^3+5*x^2-y*x
	powx[0]=1;
	for(int i=1;i<=7;i++)
		powx[i]=powx[i-1]*x;
	return 6*powx[7]+8*powx[6]+7*powx[3]+5*powx[2]-y*powx[1];
}

double thrfind(double start,double end)
{
	if(end-start<=MIN)
		return start;
	double left=start+(end-start)/3;
	double right=end-(end-start)/3;
	if(fun(left)>=fun(right))
		return thrfind(left,end);
	else return thrfind(start,right);
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d",&y);
	
		printf("%.4lf\n",fun(thrfind(0,100)));
	}
	
	return 0;
}

Error Curves

Problem Description
Josephina is a clever girl and addicted to Machine Learning recently. She
pays much attention to a method called Linear Discriminant Analysis, which
has many interesting properties.
In order to test the algorithm’s efficiency, she collects many datasets.
What’s more, each data is divided into two parts: training data and test
data. She gets the parameters of the model on training data and test the
model on test data. To her surprise, she finds each dataset’s test error curve is just a parabolic curve. A parabolic curve corresponds to a quadratic function. In mathematics, a quadratic function is a polynomial function of the form f(x) = ax2 + bx + c. The quadratic will degrade to linear function if a = 0.

在这里插入图片描述

It’s very easy to calculate the minimal error if there is only one test error curve. However, there are several datasets, which means Josephina will obtain many parabolic curves. Josephina wants to get the tuned parameters that make the best performance on all datasets. So she should take all error curves into account, i.e., she has to deal with many quadric functions and make a new error definition to represent the total error. Now, she focuses on the following new function’s minimum which related to multiple quadric functions. The new function F(x) is defined as follows: F(x) = max(Si(x)), i = 1…n. The domain of x is [0, 1000]. Si(x) is a quadric function. Josephina wonders the minimum of F(x). Unfortunately, it’s too hard for her to solve this problem. As a super programmer, can you help her?
Input
The input contains multiple test cases. The first line is the number of cases T (T < 100). Each case begins with a number n (n ≤ 10000). Following n lines, each line contains three integers a (0 ≤ a ≤ 100), b (|b| ≤ 5000), c (|c| ≤ 5000), which mean the corresponding coefficients of a quadratic function.
Output
For each test case, output the answer in a line. Round to 4 digits after the decimal point.
Sample Input
2
1
2 0 0
2
2 0 0
2 -4 2
Sample Output
0.0000
0.5000
问题描述 有n个二次函数,在[0,1000]里找他们最大值的最小值
方法 画画图就知道是这永远都是凹区间,三分区间解决
WA的原因 你猜猜为什么WA?这题居然精度要1e-9

//You has the final say in what kind of life you want.
#include<iostream>
#include<cmath>
#include<cstdio>
#define MAX 10000+10
#define MIN 0.000000001
using namespace std;
int a[MAX],b[MAX],c[MAX];
int n;
double fun(int aa,int bb,int cc,double x)
{
	return double(aa)*x*x+double(bb)*x+double(cc);
}
double findmax(double x)
{
	double maxx=-1e10;
	for(int i=0;i<n;i++)
		if(fun(a[i],b[i],c[i],x)>maxx)
			maxx=fun(a[i],b[i],c[i],x);
	return maxx;
}
double solve(double start,double end)
{
	if(end-start<=MIN)
		return start;
	double left=start+(end-start)/3.0;
	double right=end-(end-start)/3.0;
	if(findmax(left)>=findmax(right))
		return solve(left,end);
	else return solve(start,right);
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%d%d%d",&a[i],&b[i],&c[i]);
		printf("%.4lf\n",findmax(solve(0,1000.0)));
	}
	return 0;
}

嗯第一次写这些题解。希望以后不要犯这些错误了。明天继续努力。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值