Wall
Time Limit: 1000MS |
Memory Limit: 10000K |
|
Total Submissions: 21731 |
Accepted: 7132 |
Description
Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.
Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.
The task is somewhat simplified by the fact, that the King's castle has a
polygonal shape and is situated on a flat ground. The Architect has already
established a Cartesian coordinate system and has precisely measured the
coordinates of all castle's vertices in feet.
Input
The first
line of the input file contains two integer numbers N and L separated by a
space. N (3 <= N <= 1000) is the number of vertices in the King's castle,
and L (1 <= L <= 1000) is the minimal number of feet that King allows for
the wall to come close to the castle.
Next N lines describe coordinates of castle's vertices in a clockwise order.
Each line contains two integer numbers Xi and Yi separated by a space (-10000
<= Xi, Yi <= 10000) that represent the coordinates of ith vertex. All
vertices are different and the sides of the castle do not intersect anywhere
except for vertices.
Output
Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around the castle to satisfy King's requirements. You must present the integer number of feet to the King, because the floating numbers are not invented yet. However, you must round the result in such a way, that it is accurate to 8 inches (1 foot is equal to 12 inches), since the King will not tolerate larger error in the estimates.
Sample Input
9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output
1628
Hint
结果四舍五入就可以了
Source
解题报告:题意就是修一圈的城墙,把凸包围起来,并且城墙离凸包的距离不小于R,求最小需要多长;就是求凸包的周长 + 圆的周长即可!
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int MAX = 1010; const double PI = acos(-1.0); double r, ans; int n, top; struct Point//存储点的坐标 { double x; double y; }p[MAX]; int convex[MAX];//记录形成凸包的点的次序按逆时针 double cmp(struct Point a, struct Point b)//先按坐标y排序y坐标相同时再按x排序 { if (a.y == b.y) return a.x < b.x; else return a.y < b.y; } double Dis(int i, int j)//计算两点间的距离 { return sqrt((p[i].x - p[j].x) * (p[i].x - p[j].x) + (p[i].y - p[j].y) * (p[i].y - p[j].y)); } double Multi(Point p1, Point p2, Point p3) { return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); } void Graham()//选取点形成凸包(不包含边上的点)选取的点数是最小的 { int i, count; top = 1; sort(p, p + n, cmp); for (i = 0; i < 3; ++i) { convex[i] = i; } for (i = 2; i < n; ++i) { while (top && Multi(p[i], p[convex[top]], p[convex[top - 1]]) >= 0) { top --; } convex[++ top] = i; } count = top; convex[++ top] = n - 2; for (i = n - 3; i >= 0; i --) { while (top != count && Multi(p[i], p[convex[top]], p[convex[top - 1]]) >= 0) { top --; } convex[++ top] = i; } } int main() { int i; while (scanf("%d%lf", &n, &r) != EOF) { for (i = 0; i < n; ++i) { scanf("%lf%lf", &p[i].x, &p[i].y); } ans = 2.0 *PI * r; Graham(); for (i = 0; i < top; ++i) { ans += Dis(convex[i], convex[(i + 1) % top]); } printf("%.0lf\n", ans); } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· Apifox不支持离线,Apipost可以!
· 历时 8 年,我冲上开源榜前 8 了!
· 零经验选手,Compose 一天开发一款小游戏!
· Trae 开发工具与使用技巧
· 通过 API 将Deepseek响应流式内容输出到前端