C++&Pascal——【USACO 5.1.1】——Fencing the Cows

来源:互联网 发布:淘宝导航所有分类代码 编辑:程序博客网 时间:2024/05/22 16:48

Fencing the Cows
Hal Burch

Farmer John wishes to build a fence to contain his cows, but he's a bit short on cash right. Any fence he builds must contain all of the favorite grazing spots for his cows. Given the location of these spots, determine the length of the shortest fence which encloses them.

PROGRAM NAME: fc

INPUT FORMAT

The first line of the input file contains one integer, N. N (0 <= N <= 10,000) is the number of grazing spots that Farmer john wishes to enclose. The next N line consists of two real numbers, Xi and Yi, corresponding to the location of the grazing spots in the plane (-1,000,000 <= Xi,Yi <= 1,000,000). The numbers will be in decimal format.

SAMPLE INPUT (file fc.in)

44 84 125 9.37 8

OUTPUT FORMAT

The output should consists of one real number, the length of fence required. The output should be accurate to two decimal places.

SAMPLE OUTPUT (file fc.out)

12.00

农夫约翰希望建一个篱笆来控制他的奶牛,但是他的钱不够用。他所建的任何栅栏都必须包含他的牧牛点。考虑到这些点的位置,确定覆盖它们的最短栅栏的长度。

项目名称: fc

输入格式

输入文件的第一行包含一个整数N(0 <= N <= 10,000),是农民约翰希望包围的放牧点的数量。接下来的N行由两个实数Xi和Yi组成,对应于平面上的放牧点的位置(- 1,000,000 <= Xi,Yi <= 1,000,000)。数字将以十进制格式。

示例输入(文件 fc.in)

4

4  8

4 12

5  9.3

7  8

输出格式

输出应该包含一个实数,即所需的围栏长度。输出应该精确到小数点后两位。

样例输出(文件fc.out)

12.00



/*ID : mcdonne1LANG : C++TASK : fc*/#pragma GCC optimize("O3")#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <vector>using namespace std;struct node{double x, y, cosine;}p[10001];int n;double ans, rad[10001];vector <int> v;inline bool comp(const node &a, const node &b) {return a.y == b.y ? a.x < b.x : a.y < b.y;}inline bool cmop(const node &a, const node &b) {return a.cosine > b.cosine;}inline double __dis(double x, double y) {return sqrt(x * x + y * y);}inline double __cos(double x, double y) {return x / __dis(x, y);}inline double getrad(double x, double y) {double __rad = acos(__cos(x, y));if (y < 0) __rad = M_PI * 2 - __rad;return __rad;}int main () {freopen ("fc.in", "r", stdin);freopen ("fc.out", "w", stdout);cin>>n;for (int i = 1; i <= n; i++) cin>>p[i].x>>p[i].y;sort (p + 1, p + 1 + n, comp);for (int i = 2; i <= n; i++)p[i].cosine = __cos(p[i].x - p[1].x, p[i].y - p[1].y);sort (p + 2, p + 1 + n, cmop);v.push_back(1);v.push_back(2);rad[1] = getrad(p[2].x - p[1].x, p[2].y - p[1].y);for (int i = 3; i <= n; i++) {while(true) {node a = p[v[v.size() - 2]];double Rad = getrad(p[i].x - a.x, p[i].y - a.y);if (Rad <= rad[v[v.size() - 2]]) v.erase(v.end() - 1);else {node b = p[v[v.size() - 1]];rad[v[v.size() - 1]] = getrad (p[i].x - b.x, p[i].y - b.y);break;}}v.push_back(i);}v.push_back(1);for (int i = 1; i < v.size(); i++)ans += __dis(p[v[i]].x - p[v[i - 1]].x, p[v[i]].y - p[v[i - 1]].y);printf("%.2lf\n", ans);return 0;}

{ID : mcdonne1LANG : PASCALTASK : fc}uses math;varp : array [0..10001] of record x, y, cosine : double; end;ans, rad__ : double;rad : array [0..10001] of double;n, i, pos : integer;vector : array [0..10010] of integer;function partition (l, r : integer) : integer;vari, j : integer;x, y : double;begin        x := p[r - 1].y;        y := p[r - 1].x;        i := l - 1;        for j := l to r - 2 do                if (p[j].y < x) or ((p[j].y = x) and (p[j].x <= y)) then begin                        inc (i);                        p[0] := p[j];                        p[j] := p[i];                        p[i] := p[0];                end;        p[0] := p[r - 1];        p[r - 1] := p[i + 1];        p[i + 1] := p[0];        exit (i + 1);end;procedure quicksort (l, r : integer);var q : integer;begin        if l + 1 < r then begin                q := partition (l, r);                quicksort (l, q);                quicksort (q + 1, r);        end;end;function __part (l, r : integer) : integer;vari, j : integer;x : double;begin        x := p[r - 1].cosine;        i := l - 1;        for j := l to r - 2 do                if p[j].cosine >= x then begin                        inc (i);                        p[0] := p[j];                        p[j] := p[i];                        p[i] := p[0];                end;        p[0] := p[r - 1];        p[r - 1] := p[i + 1];        p[i + 1] := p[0];        exit (i + 1);end;procedure quicksort2 (l, r : integer);var q : integer;begin        if l + 1 < r then begin                q := __part (l, r);                quicksort2 (l, q);                quicksort2 (q + 1, r);        end;end;function __dis (x, y : double) : double;var z : double;begin        z := sqrt (x * x + y * y);        exit (z);end;function __cos (x, y : double) : double;var z : double;begin        z := x / __dis (x, y);        exit (z);end;function getrad (x, y : double) : double;var __rad : double;begin        __rad := arccos(__cos(x, y));        if y < 0 then __rad := Pi * 2 - __rad;        exit (__rad);end;begin        assign (input, 'fc.in');        assign (output, 'fc.out');        reset (input);        rewrite (output);        read (n);        for i := 1 to n do read (p[i].x, p[i].y);        quicksort (1, n + 1);        for i := 2 to n do p[i].cosine := __cos (p[i].x - p[1].x, p[i].y - p[1].y);        quicksort2 (2, n + 1);        inc (pos);        vector[pos] := 1;        inc (pos);        vector[pos] := 2;        rad[1] := getrad(p[2].x - p[1].x, p[2].y - p[1].y);        for i := 3 to n do begin                while true do begin                        p[0] := p[vector[pos - 1]];                        rad__ := getrad(p[i].x - p[0].x, p[i].y - p[0].y);                        if rad__ <= rad[vector[pos - 1]] then dec (pos)                        else begin                                p[0] := p[vector[pos]];                                rad[vector[pos]] := getrad (p[i].x - p[0].x, p[i].y - p[0].y);                                break;                        end;                end;                inc (pos);                vector[pos] := i;        end;        inc (pos);        vector[pos] := 1;        for i := 2 to pos do                ans := ans + __dis(p[vector[i]].x - p[vector[i - 1]].x, p[vector[i]].y - p[vector[i - 1]].y);        writeln (ans : 0 : 2);end.


原创粉丝点击