Three-way solution for extreme values of convex (concave) Functions

Source: Internet
Author: User

The bipartite method is applicable only to linear functions. It is necessary to divide functions into three points when they are convex or concave when they are separated from linear functions.

The trigger process is as follows:

Convex Function:

Concave function:

Implementation Method:

double Calc(double p) {   /*...*/}double Solve(double MIN, double MAX) {    double Left, Right;    double mid, midmid;    double mid_area = 0, midmid_area = 0;    //***    Left = MIN; Right = MAX;    while (Left + eps < Right) {        mid = (Left + Right) / 2;        midmid = (mid + Right) / 2;        mid_area = Calc(mid);        midmid_area = Calc(midmid);        if (midmid_area - mid_area > eps) Right = midmid;        else Left = mid;    }    return mid_area;}

 

 

Example: HDU4355 (party all the time)

 

View code

#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <ctime>#include <queue>#include <map>#include <sstream>#define CL(arr, val)    memset(arr, (val), sizeof(arr))#define REP(i, n)       for((i) = 0; (i) < (n); ++(i))#define FOR(i, l, h)    for((i) = (l); (i) <= (h); ++(i))#define FORD(i, h, l)   for((i) = (h); (i) >= (l); --(i))#define L(x)    (x) << 1#define R(x)    (x) << 1 | 1#define MID(l, r)   ((l) + (r)) >> 1#define Min(x, y)   (x) < (y) ? (x) : (y)#define Max(x, y)   (x) < (y) ? (y) : (x)#define E(x)    (1 << (x))#define iabs(x)  ((x) > 0 ? (x) : -(x))typedef long long LL;const double eps = 1e-6;const double inf = 1000000000;using namespace std;const int N = 50010;struct node {    double p;    double w;} q[N];int n;double Calc(double p) {    double tmp = 0, d;    for(int i = 0; i < n; ++i) {        d = abs(q[i].p - p);        tmp += d*d*d*q[i].w;    }    return tmp;}double Solve(double MIN, double MAX) {    double Left, Right;    double mid, midmid;    double mid_area = 0, midmid_area = 0;    Left = MIN; Right = MAX;    while (Left + eps < Right) {        mid = (Left + Right) / 2;        midmid = (mid + Right) / 2;        mid_area = Calc(mid);        midmid_area = Calc(midmid);        if (midmid_area - mid_area > eps) Right = midmid;        else Left = mid;    }    //printf("%.10f\n", mid_area);    return mid_area;}int main() {    //freopen("data.in", "r", stdin);    int t, j, cas = 0;    double mx, mi;    scanf("%d", &t);    while(t--) {        scanf("%d", &n);        mx = -inf, mi = inf;        for(j = 0; j < n; ++j) {            scanf("%lf%lf", &q[j].p, &q[j].w);            if(mx < q[j].p) mx = q[j].p;            if(mi > q[j].p) mi = q[j].p;        }        double ans = Solve(mi, mx) + 0.5;        printf("Case #%d: %d\n", ++cas, int(ans));    }    return 0;}

 

Poj 3301

Method to rotate the coordinate system (0,180] degrees, and then get a new coordinate for each point, find the top, bottom, leftmost and rightmost points, then you can determine the area of the current rotation angle.

X' = x * Cos (th) + y * sin (th );

Y' = y * Cos (th)-x * sin (th );

View code

//#pragma comment(linker,"/STACK:327680000,327680000")#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <functional>#include <numeric>#include <sstream>#include <stack>#include <map>#include <queue>#define CL(arr, val)    memset(arr, val, sizeof(arr))#define REP(i, n)       for((i) = 0; (i) < (n); ++(i))#define FOR(i, l, h)    for((i) = (l); (i) <= (h); ++(i))#define FORD(i, h, l)   for((i) = (h); (i) >= (l); --(i))#define L(x)    (x) << 1#define R(x)    (x) << 1 | 1#define MID(l, r)   (l + r) >> 1#define Min(x, y)   (x) < (y) ? (x) : (y)#define Max(x, y)   (x) < (y) ? (y) : (x)#define E(x)        (1 << (x))#define iabs(x)     (x) < 0 ? -(x) : (x)#define OUT(x)  printf("%I64d\n", x)#define Read()  freopen("data.in", "r", stdin)#define Write() freopen("data.out", "w", stdout);typedef long long LL;const double eps = 1e-8;const double pi = acos(-1.0);const double inf = ~0u>>2;using namespace std;const int N = 50;struct node {    double x, y;}p[N];int n;double Calc(double th) {    double l = inf, r = -inf, d = inf, u = -inf;    double xx, yy, t;    for(int i = 0; i < n; ++i) {        t = th*pi/180.0;        xx = p[i].x*cos(t) + p[i].y*sin(t);        yy = p[i].y*cos(t) - p[i].x*sin(t);        l = min(l, xx); d = min(d, yy);        r = max(r, xx); u = max(u, yy);    }    return max((r - l)*(r - l), (u - d)*(u - d));}double Solve(double MIN, double MAX) {    double Left, Right;    double mid, midmid;    double mid_area = 0, midmid_area = 0;    Left = MIN, Right = MAX;    while(Left + eps < Right) {        mid = (Left + Right) / 2.0;        midmid = (mid + Right) / 2.0;        mid_area = Calc(mid);        midmid_area = Calc(midmid);        if(midmid_area - mid_area > eps)    Right = midmid;        else    Left = mid;    }    return mid_area;}int main() {    //Read();    int T, i;    scanf("%d", &T);    while(T--) {        scanf("%d", &n);        for(i = 0; i < n; ++i)  scanf("%lf%lf", &p[i].x, &p[i].y);        printf("%.2f\n", Solve(0, 180));    }    return 0;}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.