poj2595(凸包)

Min-Max

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2192   Accepted: 502

Description

Define the following function

Given the value C of F(p1, p2 ... pn), can you find the minimum and maximum value of F(q1, q2 ... qn)?

Input

The input contains several test cases. For each test case, it contains three lines.

Line 1: two integers n (1<= n <= 50000) and C.

Line 2: n integers p1, p2 ... pn (|pi| < 1000 for 1 <= i <= n).

Line 3: n integers q1, q2 ... qn (|qi| < 1000 for 1 <= i <= n).

Output

For each test case, output the minimum and maximum value in a single line with the fraction rounded to 3 decimal places.

Sample Input

2 1
3 1
0 2

Sample Output

2.000 2.000

Source

POJ Monthly--2005.08.28,Static

题目一看就很熟悉很熟悉很熟悉啊!

我怎么一眼看到就想起詹森不等式呢。

。。。

事实上是重心公式。C是重心的x,而y在凸包上。

所以问题就变成求凸包啦!

求完凸包就要绕着凸包上每一条边求极值并更新。

/***********************************************************
	> OS     : Linux 3.13.0-24-generic (Mint-17)
	> Author : yaolong
	> Mail   : [email protected]
	> Time   : 2014年10月14日 星期二 07时44分53秒
 **********************************************************/
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const double INF = 1e50;
const int N = 61111;
struct Point
{
    int x, y;
} ;
Point p[N], stk[N];
Point minp;
int top;
double cross ( Point &o,  Point &a,  Point &b )
{
    return ( a.x - o.x ) * ( b.y - o.y ) - ( a.y - o.y ) * ( b.x - o.x );
}
double dist ( Point &A,  Point & B )
{
    return hypot ( A.x - B.x, A.y - B.y );
}
bool cmp ( Point A, Point B )
{
    double k = cross ( minp, A, B );
    if ( k < 0 ) return 0;
    if ( k > 0 ) return 1;
    return dist ( minp, A ) < dist ( minp, B );
}
void Gramham ( int n )
{
    int i;
    for ( i = 1; i < n; i++ )
    {
        if ( p[i].y < p[0].y || ( p[i].y == p[0].y && p[i].x < p[0].x ) )
        {
            swap ( p[i], p[0] );
        }
    }
    minp = p[0];
    p[n] = p[0];
    sort ( p + 1, p + n, cmp );
    stk[0] = p[0];
    stk[1] = p[1];
    top = 1;
    for ( i = 2; i < n; i++ )
    {
        while ( top >= 1 && cross ( stk[top - 1], stk[top ], p[i] ) <= 0 ) --top;
        stk[++top] = p[i];
    }
}
double mmin, mmax;
int c;
void update ( Point a, Point b )
{
    if ( a.x > b.x )
    {
        swap ( a, b );
    }
    if ( a.x <= c && b.x >= c )
    {
        if ( a.x == c && b.x == c )
        {
            mmax = max ( mmax, ( double ) max ( a.y, b.y ) );
            mmin = min ( mmin, ( double ) min ( a.y, b.y ) );
        }
        else
        {
            double k = ( ( double ) c - a.x ) / ( ( double ) b.x - a.x ) * ( b.y - a.y ) + a.y;
            mmax = max ( mmax, k );
            mmin = min ( mmin, k );
        }
    }
}
int main()
{
    int n,  i;
    while ( ~scanf ( "%d%d", &n, &c ) )
    {
        for ( i = 0; i < n; i++ )
        {
            scanf ( "%d", &p[i].x );
        }
        for ( i = 0; i < n; i++ )
        {
            scanf ( "%d", &p[i].y );
        }
        if ( n == 1 )
        {
            printf ( "%.3f %.3f\n", ( double ) p[0].y, ( double ) p[0].y );
            continue;
        }
        Gramham ( n );
        stk[++top] = stk[0];
        mmin = INF, mmax = -INF;
        for ( i = 1; i <= top; i++ )
        {
            update ( stk[i - 1], stk[i] );
        }
        printf ( "%.3f %.3f\n", mmin, mmax );
    }
    return 0;
}
时间: 2024-10-21 02:07:48

poj2595(凸包)的相关文章

POJ3528 HDU3662 三维凸包模板

POJ3528 HDU3662 第一道题 给定若干点 求凸包的表面积,第二题 给定若干点就凸包的面数. 简单说一下三维凸包的求法,首先对于4个点假设不共面,确定了唯一四面体,对于一个新的点,若它不在四面体内,为了让它进入凸包, 则对于所有凸包上的边,若边的一面是该点可以看到的而另一面看不到,则该点与该边构成的面要加入凸包. 模板代码非常清晰, #include<stdio.h> #include<algorithm> #include<string.h> #includ

关于2016.12.12——T1的反思:凸包的意义与应用

2016.12.12 T1 给n个圆,保证圆圆相离,求将圆围起来的最小周长.n<=100 就像上图.考场上,我就想用切线的角度来做凸包.以圆心x,y排序,像点凸包一样,不过用两圆之间的下切线角度来判断. 这就是下切线(我自己瞎编的名字): 好像是对的啊: 然后我就保证必AC的希望,用这种写法交了,然后就只得了N=2的暴力分... 自以为是正解,却落得如此下场... 为什么?这样不对吗?借用学长的力量,果然被Hack掉了: 这种情况,圆心排序后,检测的顺序并不是圆上的切点的顺序,自然就会挂. 蓝瘦

poj 1113 凸包周长

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33888   Accepted: 11544 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

UVA 11800 Determine the Shape --凸包第一题

题意: 给四个点,判断四边形的形状.可能是正方形,矩形,菱形,平行四边形,梯形或普通四边形. 解法: 开始还在纠结怎么将四个点按序排好,如果直接处理的话,有点麻烦,原来凸包就可搞,直接求个凸包,然后点就自动按逆时针排好了,然后就判断就可以了,判断依据题目下面有,主要是用到点积和叉积,判断垂直用点积,判断平行用叉积. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstd

POJ 3348 最直接的凸包问题

题目大意: 给定一堆树的点,找到能组合成的最大面积,一个物体占50面积,求最多放多少物体 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 const double eps = 1e-10; 7 const int N = 10005; 8 double myabs(double x) 9

hdu 1348 Wall(凸包模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3386    Accepted Submission(s): 968 Problem Description Once upon a time there was a gre

POJ1113 Wall【凸包】

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24604   Accepted: 8183 Description King想在自己的n个城堡外建Wall,使Wall与任一城堡距离至少为L且能围住它的城堡. 求Wall最短长度. Input 第一行为 N (3 <= N <= 1000) 和 L(1 <= L <= 1000). 接下来 N 行,每行为城堡坐标Xi,Yi (-10000 <=

[凸包]Triangles

https://nanti.jisuanke.com/t/15429 题目大意:给出平面内$n$个整数坐标点,保证无三点共线.可以进行若干次连线,每次选择一个点对连接线段,但是任意两条线段都不得在给定的$n$个点之外有交点.问连线完成后,最多能构造出多少个三角形. 解题关键: 小于三个点的情况答案为零.考虑三个点的情况,由于三点不共线,必然构成一个三角形.现加入第四个点,若其在原三角形外部,则称其为外点,可以新构造$1$个三角形:若其在原三角形内部,则称其为内点,可以新构造$3$个三角形.故要尽

ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)

Description You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut the cake into several triangle-shaped parts for the invited comers. You have a knife to cut. The trace of each cut is a line segment, whose two endpoin