Min-Max

http://poj.org/problem?id=2595

bySd_无心插柳

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <list>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
//#pragma comment(linker, "/STACK:16777216")
using namespace std;
typedef int LL;
typedef unsigned long long ULL;

#define N 50005
int n , C;
struct point
{
  LL x , y;
  point (LL x = 0 , LL y = 0): x(x) , y(y) {};
}p[N];
point operator + (point a , point b) { return point(a.x + b.x , a.y + b.y);}
point operator - (point a , point b) { return point(a.x - b.x , a.y - b.y);}
LL Cross(point a , point b) {return a.x * b.y - a.y * b.x;}
double length(point a) {return sqrt(double(a.x) * a.x + double(a.y) * a.y);}
double dis(point a , point b) {return length(a - b);}

bool cmp(point a , point b)
{
  if (a.x == b.x)
    return a.y < b.y;
  return a.x < b.x;
}

int s[N] , t;
int work()
{
    int i , j , x , a , b;
    //scanf("%d%d",&n,&C);
    for (i = 1 ; i <= n ;i ++)
        scanf("%d",&p[i].x);
    for (i = 1 ; i <= n ;i ++)
        scanf("%d",&p[i].y);
    sort(p + 1 , p + n + 1 , cmp);
    t = 0;
    for (i = 1 ; i <= n ;i ++)
    {
      while (t > 1 && Cross(p[s[t]] - p[s[t - 1]] ,p[i] - p[s[t - 1]]) <= 0) t --;
      s[++ t] = i;
    }
    j = t;
    for (i = n ; i > 0 ;i --)
    {
      while (t > j && Cross(p[s[t]] - p[s[t - 1]] ,p[i] - p[s[t - 1]]) <= 0) t --;
      s[++ t] = i;
    }
    if (n > 1) t --;
    double mn = 1e9 , mx = -1e9;
    n = t;
    a = s[1] , b = s[n];
    if (min(p[a].x , p[b].x) <= C && C <= max(p[a].x , p[b].x))
    {
        if (p[a].x == p[b].x)
            mn = min(mn , (double)min(p[a].y , p[b].y)) , mx = max(mx , (double)max(p[a].y , p[b].y));
        else
        {
            double k = (double)(p[a].y - p[b].y) / (p[a].x - p[b].x);
            double y = k * (C - p[b].x) + p[b].y;
            mn = min(mn , y) , mx = max(mx , y);
        }
    }
    for (i = 1 ; i < n ; ++ i)
    {
        a = s[i] , b = s[i + 1];
        if (min(p[a].x , p[b].x) <= C && C <= max(p[a].x , p[b].x))
        {
            if (p[a].x == p[b].x)
                mn = min(mn , (double)min(p[a].y , p[b].y)) , mx = max(mx , (double)max(p[a].y , p[b].y));
            else
            {
                double k = (double)(p[a].y - p[b].y) / (p[a].x - p[b].x);
                double y = k * (C - p[b].x) + p[b].y;
                mn = min(mn , y) , mx = max(mx , y);
            }
        }
    }
    printf("%.3f %.3f\n" , mn , mx);
}


int main()
{
    //freopen("~input.txt" , "r" , stdin);
    //int _; scanf("%d",&_); while (_--)
    while (~scanf("%d%d",&n,&C))
        work();
    return 0;
}


http://blog.sina.com.cn/s/blog_d760eb2e0101dl1c.html

题意:设函数F(x1,x2...xn)=Σuixi(1<=i<=n),u为函数固定的系数,且对u有限制Σu=1且u∈[0,1],但是你不知道,你只知道一组输入x1,x2...xn对应的输出为C,问题是给定的另一组输入y1,y2...yn的输出最小与最大可能是多少。

观察一下u的神奇范围我们可以很直观的联想到一群质点的重心公式,不妨将(xi,yi)作为坐标放在平面直角坐标系中,我们就可以将所有的有序实数对看成质量相等的质点,C就是其重心的x坐标,答案即为重心的y坐标,由重心知识可知重心位于点集的凸包内部,所以贪心得到答案为凸包边(点)上,所以求出凸包维护一下答案就行了。

// File Name: poj2595.cpp
// Author: bo_jwolf
// Created Time: 2013年09月29日 星期日 10:20:03

#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>

using namespace std;
/*
struct Point
{
	double x,y;
	Point(){}
	Point(double _x,double _y)
	{
	x = _x;y = _y;
	}
	Point operator -(const Point &b)const
	{
		return Point(x - b.x,y - b.y);
	}
	//叉积
	double operator ^(const Point &b)const
	{
		return x*b.y - y*b.x;
	}
	//点积
	double operator *(const Point &b)const
	{
		return x*b.x + y*b.y;
	}
	//绕原点旋转角度B(弧度值),后x,y的变化
	void transXY(double B)
	{
		double tx = x,ty = y;
		x = tx*cos(B) - ty*sin(B);
		y = tx*sin(B) + ty*cos(B);
	}
}*/

const int maxn = 50005 ;
struct node
{
	int x , y ; 
}num[ 50005 ] ;

node operator - ( const node &a , const node &b ) 
{
	node r ; 
	r.x = a.x - b.x ;
	r.y = a.y - b.y ;
	return r ;
}

int operator * ( const node &a , const node &b )
{
	return a.x * b.y - a.y * b.x ;
}

int operator == ( node a , node b )
{
	return a.x == b.x && a.y == b.y ;
}

bool cmp( node a , node b ) 
{
	if( a.y == b.y )
		return a.x < b.x ;
	return a.y < b.y ;
}

int i , l , r , n , c ;
node q[ maxn ] ;
double Min , Max ;

void check( node a , node b )
{
	if( a.x > b.x )
		swap( a , b ) ;
	if( a.x <= c && c <= b.x )
	{
		if( a.x == c && b.x == c )
		{
			Max = max( Max , ( double ) max( a.y , b.y ) ) ;
			Min = min( Min , ( double ) min( a.y , b.y ) ) ;
		}
		else
		{
			double temp = ( ( double ) c - a.x ) / ( b.x - a.x ) * ( b.y - a.y ) + a.y ;
			Max = max( Max , temp ) ;
			Min = min( Min , temp ) ;
		}
	}
	return ;
}

int main()
{
	while( scanf( "%d%d" , &n , &c ) != EOF )
	{
		Min = 1e8 , Max = - 1e8 ;
		for( i = 1 ; i <= n ; ++i )
		{
			scanf( "%d" , &num[ i ].x ) ;
		}
		for( i = 1 ; i <= n ; ++i )
		{
			scanf( "%d" , &num[ i ].y ) ;
		}
		if( n == 1 )
		{
			printf( "%.3lf %.3lf\n" , ( double )num[ 1 ].y , ( double )num[ 1 ].y ) ;
		}
		else
		{
			sort( num + 1 , num + 1 + n , cmp ) ;
			int temp = 1 ;
			r = 0 ; 
			for( i = 1 ; i ; i += temp )
			{
				if( i == n )
					temp = -1 ;
				while( r > 1 && ( num[ i ] - q[ r ] ) * ( q[ r ] - q[ r - 1 ] ) > 0 )
				{
					r-- ;
				}
				if( !( num[ i ] == q[ r ] ) )
					q[ ++r ] = num[ i ] ; 
			}
			for( i = 1 ; i < r ; ++i )
			{
				check( q[ i ] , q[ i + 1 ] ) ;
			}
			printf( "%.3lf %.3lf\n" , Min , Max ) ;
		}
	}
return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值