HDU 1392 Surround the Trees
2011-07-29 12:54
302 查看
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3223 Accepted Submission(s): 1230
[align=left]Problem Description[/align]There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.
There are no more than 100 trees.
[align=left]Input[/align]The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.
Zero at line for number of trees terminates the input for your program.
[align=left]Output[/align]The minimal length of the rope. The precision should be 10^-2.
[align=left]Sample Input[/align]9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
[align=left]Sample Output[/align]243.06
[align=left]Source[/align]Asia 1997, Shanghai (Mainland China)
[align=left]Recommend[/align]Ignatius.L 凸包水题直接GramhamScan可以过,不过要注意n < 3两个特例。果断贴代码:
4285845 | 2011-07-29 12:51:30 | Accepted | 1392 | 78MS | 272K | 1631 B | C++ | 10SGetEternal{(。)(。)}! |
#include <iostream> #include <vector> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std; #define MAXI 111 class point { public : double x, y; point(double tx = 0.0, double ty = 0.0) { x = tx; y = ty; } double dis() { return sqrt(pow(x, 2) + pow(y, 2)); }; point operator - (point &o) { return point(x - o.x, y - o.y); } double operator * (point &o) { return x * o.y - y * o.x; } }ps[MAXI]; vector<point> chul; int n; bool cmp(point &a, point &b) { double cp = (ps[0] - a) * (b - a); if (cp == 0) return (a - ps[0]).dis() > (b - ps[0]).dis(); else if (cp < 0) return 1; return 0; } double GHS() { int i, idm; double tsu = 0.0; for (idm = i = 0; i < n; i++) if (ps[i].y == ps[idm].y && ps[i].x < ps[idm].x || ps[i].y < ps[idm].y) idm = i; swap(ps[idm], ps[0]); sort(ps + 1, ps + n, cmp); chul.clear(); chul.push_back(ps[0]); chul.push_back(ps[1]); for (i = 2; (ps[0] - ps[1]) * (ps[i] - ps[1]) == 0; i++); chul.push_back(ps[i]); for (i++; i < n; i++) { while ((chul[chul.size() - 2] - chul[chul.size() - 1]) * (ps[i] - chul[chul.size() - 1]) >= 0) chul.pop_back(); chul.push_back(ps[i]); } for (i = 0; i < chul.size(); i++) tsu += (chul[(i + 1) % chul.size()] - chul[i]).dis(); return tsu; } int main() { int i; while (scanf("%d", &n), n) { for (i = 0; i < n; i++) scanf("%lf%lf", &ps[i].x, &ps[i].y); if (n == 2) printf("%.2lf\n", ps[1] - ps[0]); else if (n == 1) printf("0\n"); else printf("%.2lf\n", GHS()); } return 0; }
Thanks……
相关文章推荐
- hdu1392-Surround the Trees
- HDU 1392 Surround the Trees(凸包)
- HDU 1392 Surround the Trees(凸包模板)
- hdu 1392 Surround the Trees (Convex Hull + Trick)
- hdu 1392 Surround the Trees 凸包裸题
- hdu 1392 Surround the Trees
- hdu 1392 Surround the Trees
- Surround the Trees HDU - 1392 凸包周长(Andrew)
- hdu 1392 Surround the Trees (凸包)
- hdu1392——Surround the Trees
- hdu 1392 Surround the Trees
- HDU1392 Surround the Trees
- hdu 1392 Surround the Trees
- HDU 1392 Surround the Trees
- HDU 1392 Surround the Trees(凸包)
- HDU-1392 Surround the Trees,凸包入门!
- HDU 1392 Surround the Trees
- HDU1392--Surround the Trees--凸包
- HDU 1392 Surround the Trees (Graham求凸包周长)
- HDU 1392 Surround the Trees(凸包模板)