[Trichotomy] unimodal (single valley) function to find the pole-thirds

Copyright: https: //blog.csdn.net/qq_40831340 welcome treatise https://blog.csdn.net/qq_40831340/article/details/90639163

https://www.luogu.org/problemnew/show/P3382
subject description
As stated, is given a function of N times, to ensure that the range [l, r] in the memory point x, such that [l, x] monotonically increasing on [ x, r monotone decreasing] on. Test determined the value of x.

Input Output Format
Input format:
The first line comprises a first positive integer N as shown in two real numbers and l, r, meaning as described in the title.

The second line contains N + 1 real numbers, the descending order of coefficients representing the function of N times.

Output format:
output line, comprising a real number, is the value of x. Rounded to five decimal places.

Principle is simple, and applicable to all domains in the definition of monotonic change only once a function
is mapped three equal values of the two points of comparison domain of definition, three equal points if the left is relatively large, then the right boundary of the right shift the left margin for the same reason, and ultimately get closer and closer to the position of a single peak.
Trisection point how to find, on the left third of the length of the interval and right border to the right of the left edge of one third of the length of the interval.

Rule of Thirds

#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> P;

const int maxn = 100000 + 5 ;
const int INF = 0x3f3f3f3f ;
const int mod = 9901 ;
const double eps = 1e-6;

int n;
double a[25];

double fun(double x) {
	double res = 0;
	for(int i = 0; i <= n; i++)
		res = res * x + a[i];
	return res;
}

signed main() {
	//  fastio;
	double l, r, midl, midr;
	cin >> n >> l >> r;

	for(int i = 0; i <= n; i++)
		cin >> a[i];

	while(r - l > eps) {
		double d = (r - l) / 3.0;
		midl = (l + d), midr = (r - d);

		if(fun(midl) > fun(midr))
			r = midr;
		else
			l = midl;
	}
	printf("%.5lf\n", l);
	return 0;
}

Derivative bipartite

This question is of course due to a polynomial function of power so good derivation and derivation is half point 0 inevitable for this function is unimodal pole

Corresponding to the function value of the derivative represented by the slope of the tangent at each point, the point when the derivative value is greater than 0 monotonously increases, less than zero is monotonically decreasing, it can be simply determined is greater than zero.
Shortcomings, if a question is not a polynomial function change if the request can not be used a single peak.

#include <bits/stdc++.h>
#define fastio ios::sync_with_stdio(false);cin.tie(0)
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> P;

const int maxn = 100000 + 5 ;
const int INF = 0x3f3f3f3f ;
const int mod = 9901 ;
const double eps = 1e-6;

int n;
double a[25];

double fun(double x) {
	double res = 0;
	for(int i = 0; i < n; i++) // 常数项 消失 求导完 
		res = res * x + a[i];
	return res;
}

signed main() {
	//  fastio;
	double l, r, midl, midr;
	cin >> n >> l >> r;

	for(int i = 0; i <= n; i++)
		cin >> a[i], a[i] *= (n-i);

	while(r - l > eps) {
		double mid = (r + l) / 2.0;

		if(fun(mid) > 0)
			l = mid;
		else
			r = mid;
	}
	printf("%.5lf\n", l);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40831340/article/details/90639163