[BZOJ1670][Usaco2006 Oct]Building the Moat护城河的挖掘

1670: [Usaco2006 Oct]Building the Moat护城河的挖掘

Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 628  Solved: 466 [Submit][Status][Discuss]

Description

为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河。农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水。护城河必须能保护所有的泉水,也就是说,能包围所有的泉水。泉水一定在护城河的内部,或者恰好在河道上。当然,护城河构成一个封闭的环。 挖护城河是一项昂贵的工程,于是,节约的FJ希望护城河的总长度尽量小。请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少。 所有泉水的坐标都在范围为(1..10,000,000,1..10,000,000)的整点上,一股泉水对应着一个唯一确定的坐标。并且,任意三股泉水都不在一条直线上。 以下是一幅包含20股泉水的地图,泉水用"*"表示

图中的直线,为护城河的最优挖掘方案,即能围住所有泉水的最短路线。 路线从左上角起,经过泉水的坐标依次是:(18,0),(6,-6),(0,-5),(-3,-3),(-17,0),(-7,7),(0,4),(3,3)。绕行一周的路径总长为70.8700576850888(...)。答案只需要保留两位小数,于是输出是70.87。

Input

* 第1行: 一个整数,N * 第2..N+1行: 每行包含2个用空格隔开的整数,x[i]和y[i],即第i股泉水的位 置坐标

Output

* 第1行: 输出一个数字,表示满足条件的护城河的最短长度。保留两位小数

Sample Input

20
2 10
3 7
22 15
12 11
20 3
28 9
1 12
9 3
14 14
25 6
8 1
25 1
28 4
24 12
4 15
13 5
26 5
21 11
24 4
1 8

Sample Output

70.87

求凸包周长

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char buf[10000000], *ptr = buf - 1;
inline int readint(){
    int f = 1, n = 0;
    char ch = *++ptr;
    while(ch < ‘0‘ || ch > ‘9‘){
        if(ch == ‘-‘) f = -1;
        ch = *++ptr;
    }
    while(ch <= ‘9‘ && ch >= ‘0‘){
        n = (n << 1) + (n << 3) + ch - ‘0‘;
        ch = *++ptr;
    }
    return f * n;
}
typedef long long ll;
const int maxn = 5000 + 10;
struct Point{
    ll x, y;
    Point(ll _x = 0, ll _y = 0): x(_x), y(_y){}
    Point operator - (const Point &a) const {
        return Point(x - a.x, y - a.y);
    }
    ll operator % (const Point &a){
        return x * a.y - a.x * y;
    }
}p[maxn];
class cmp{
    public:
        bool operator () (const Point &a, const Point &b){
            return (a - p[1]) % (b - p[1]) > 0;
        }
};
inline ll sqr(const ll &x){
    return x * x;
}
inline double dis(const Point &a, const Point &b){
    return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
}
int n;
Point sta[maxn];
int top = 0;
void Graham(){
    int t = 1;
    for(int i = 2; i <= n; i++)
        if(p[i].x < p[t].x || p[i].x == p[t].x && p[i].y < p[t].y) t = i;
    swap(p[t], p[1]);
    sort(p + 2, p + n + 1, cmp());
    sta[++top] = p[1];
    for(int i = 2; i <= n; i++){
        while(top > 1 && (p[i] - sta[top - 1]) % (sta[top] - sta[top - 1]) > 0) top--;
        sta[++top] = p[i];
    }
    sta[top + 1] = p[1];
}
int main(){
    fread(buf, sizeof(char), sizeof(buf), stdin);
    n = readint();
    for(int i = 1; i <= n; i++){
        p[i].x = readint();
        p[i].y = readint();
    }
    Graham();
    double ans = 0;
    for(int i = 1; i <= top; i++) ans += dis(sta[i], sta[i + 1]);
    printf("%.2lf\n", ans);
    return 0;
}
时间: 2024-08-04 03:45:45

[BZOJ1670][Usaco2006 Oct]Building the Moat护城河的挖掘的相关文章

【计算几何】【凸包】bzoj1670 [Usaco2006 Oct]Building the Moat护城河的挖掘

#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define N 5001 struct Point{int x,y;}p[N],bao[N]; bool operator < (Point a,Point b){return a.x!=b.x?a.x<b.x:a.y<b.y;} typedef long long ll; typedef Point

[bzoj1670][Usaco2006 Oct]Building the Moat

Description 为了防止口渴的食蚁兽进入他的农场,$Farmer John$决定在他的农场周围挖一条护城河.农场里一共有$N$股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河必须能保护所有的泉水,也就是说,能包围所有的泉水.泉水一定在护城河的内部,或者恰好在河道上.当然,护城河构成一个封闭的环. 挖护城河是一项昂贵的工程,于是,节约的$FJ$希望护城河的总长度尽量小.请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少. 所有泉水的坐标都在范围为$(1..1

bzoj1670【Usaco2006 Oct】Building the Moat 护城河的挖掘

1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 387  Solved: 288 [Submit][Status][Discuss] Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两股泉水.护城河必须能保护

bzoj1670 Usaco2006 Building the Moat护城河的挖掘 [凸包模板题]

Description 为了防止口渴的食蚁兽进入他的农场,Farmer John决定在他的农场周围挖一条护城河.农场里一共有N(8<=N<=5,000)股泉水,并且,护城河总是笔直地连接在河道上的相邻的两 股泉水.护城河必须能保护所有的泉水,也就是说,能包围所有的泉水.泉水一定在护城河的内部,或者恰好在河道上.当然,护城河构成一个封闭的环. 挖护城河是一项昂贵的工程,于是,节约的FJ希望护城河的总长度尽量小.请你写个程序计算一下,在满足需求的条件下,护城河的总长最小是多少. 所有泉水的坐标都在

BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )

dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) ( i - 1 <= k <= i + 1 , dp[ k ][ j - 1 ] > 0 ) 一开始没注意到要 dp[ k ][ j - 1 ] > 0 才能取 , 然后就WA 了2次... -------------------------------------------------------------------------- #include<cstdio> #incl

1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 498  Solved: 289[Submit][Status] Description 最近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼.第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在馅饼表面. 奶牛们把所有烤好的馅饼在草地上排成了一个R行(1<=R<=100)

BZOJ1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富

1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 459  Solved: 268[Submit][Status] Description 最近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼.第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在馅饼表面. 奶牛们把所有烤好的馅饼在草地上排成了一个R行(1<=R<=100)

[BZOJ1666][Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏

1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 849  Solved: 746 [Submit][Status][Discuss] Description 奶牛们又在玩一种无聊的数字游戏.输得很郁闷的贝茜想请你写个程序来帮她在开局时预测结果.在游戏的开始,每头牛都会得到一个数N(1<=N<=1,000,000).此时奶牛们的分数均为0.如果N

BZOJ1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛

1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 665  Solved: 419[Submit][Status] Description Farmer John养了N(1 <= N <= 5,000)头奶牛,每头牛都有一个不超过32位二进制数的正整数编号.FJ希望奶牛们在进食前,能按编号从小到大的顺序排好队,但奶牛们从不听他的话.为了让奶牛们养成这个习惯,每次开饭时,FJ从奶