zoukankan      html  css  js  c++  java
  • luogu P6247 [SDOI2012]最近最远点对 |随机化

    题目描述

    给定平面直角坐标系上的 (n) 个点,分别求出距离最近的两个点的距离和距离最远的两个点的距离。注意,距离为直线距离。

    输入格式

    第一行一个整数,(n)。 接下来 (n) 行每行两个非负浮点数,(x_i)​,(y_i),表示第 (i) 个点的 X 坐标与 Y 坐标。

    输出格式

    总共一行,两个浮点数,为最短距离与最长距离。误差不超过 (0.01) 视为正确。


    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N=5e5+10;
    #define int long long
    #define db double
    inline char get_char() {
    	static char buf[1000001],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline void read(double &r) {
    	double x=0,t=0;
    	int s=0,f=1;
    	char c=get_char();
    	for (; !isdigit(c); c=get_char()) {
    		if (c=='-') f=-1;
    		if (c=='.') goto readt;
    	}
    	for (; isdigit(c)&&c!='.'; c=get_char()) x=x*10+c-'0'; 
    readt:
    	for (; c=='.'; c=get_char());
    	for (; isdigit(c); c=get_char()) t=t*10+c-'0',++s; 
    	r=(x+t/pow(10,s))*f;
    }
    inline void read(int &x) { 
    	x=0;
    	char c=getchar();
    	for (; !isdigit(c); c=getchar());
    	for (; isdigit(c); c=getchar()) x=x*10+c-'0';
    }
    int n;
    struct node{
    	db x,y;	
    }e[N];
    inline bool cmp(node t1,node t2){
    	return t1.x<t2.x;	
    }
    inline db dis(int x,int y){
    	return (e[x].x-e[y].x)*(e[x].x-e[y].x)+(e[x].y-e[y].y)*(e[x].y-e[y].y);
    }
    db Min=1e9,Max=0;
    inline void around(int p){
    	db x,y;
    	for(int i=1;i<=n;i++){
    		x=e[i].x, y=e[i].y;
    		e[i].x=x*cos(p)-y*sin(p);
    		e[i].y=y*cos(p)+x*sin(p);
    	}
    	sort(e+1,e+1+n,cmp);
    	for(int i=1;i<=n;i++)
    	for(int j=max(1ll,i-4);j<i;j++)
    	Min=min(Min,dis(i,j));
    	
    	for(int i=1;i<=9;i++)
    	for(int j=n-9;j<=n;j++)
    	Max=max(Max,dis(i,j));
    }
    signed main(){
    	srand(time(0));
    	read(n);
    	for(int i=1;i<=n;i++)read(e[i].x),read(e[i].y);
    	around(rand());
    	around(rand());
    	printf("%.2lf %.2lf",sqrt(Min),sqrt(Max));
    }
    
  • 相关阅读:
    c#中文字符串与byte数组互相转化
    c#的中英文混合字符串截取 public static string SubString(string inputString, int byteLength)
    c#的中英文混合字符串截取指定长度,startidx从0开始
    //字符是否为汉字
    //获得字节长度
    C# 截取中英文混合字符串分行显示宽度相同
    C#截取中英文混合字符串分行显示
    C#截取指定长度中英文字符串方法 (修改)
    截取字符串的长度(中英文)
    canvas贪吃蛇游戏
  • 原文地址:https://www.cnblogs.com/naruto-mzx/p/12967825.html
Copyright © 2011-2022 走看看