zoukankan      html  css  js  c++  java
  • HDU 1348 Wall

    Problem 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.

    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.
     
    Sample Input
    1 9 100 200 400 300 400 300 300 400 300 400 400 500 400 500 200 350 200 200 200
     
    Sample Output
    1628


    题解:

    这题求解顺序可以乱搞,我选的极角序,直接像斜率优化中维护凸壳一样求凸包即可.

    然后答案为凸包周长+半径为L的圆的周长(为什么?看看样例的图就知道了)

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #define PI 3.1415926535
     8 using namespace std;
     9 const int N=1505;
    10 int n;
    11 struct P{
    12     int x,y;
    13     P(){};
    14     P(int _x,int _y){x=_x;y=_y;}
    15     int operator *(P A)const{
    16         return x*A.y-A.x*y;
    17     }
    18     P operator -(P A)const{
    19         return P(x-A.x,y-A.y);
    20     }
    21 }a[N];
    22 int dis(P i,P j){
    23     return (i.x-j.x)*(i.x-j.x)+(i.y-j.y)*(i.y-j.y);
    24 }
    25 int across(P C,P A,P B){
    26     return (A-C)*(B-C);
    27 }
    28 bool comp(P A,P B){
    29     int tmp=across(a[1],A,B);
    30     if(tmp>0)return true;
    31     if(tmp==0 && dis(a[1],A)<dis(a[1],B))return true;
    32     return false;
    33 }
    34 int top=0;P st[N];
    35 bool check(P A,P B,P C){
    36     int tmp=across(A,B,C);
    37     return tmp>0;
    38 }
    39 int jar(int n){
    40     top=0;
    41     if(n==0)return 0;st[++top]=a[1];
    42     if(n==1)return 1;st[++top]=a[2];
    43     if(n==2)return 2;
    44     for(int i=3;i<=n;i++){
    45         while(top>=2 && check(a[i],st[top],st[top-1]))
    46             top--;
    47         st[++top]=a[i];
    48     }
    49     return top;
    50 }
    51 int findit(){
    52     int ret=1;
    53     for(int i=2;i<=n;i++){
    54         if(a[ret].y>a[i].y)ret=i;
    55         else if(a[ret].y==a[i].y && a[ret].x>a[i].x)ret=i;
    56     }
    57     return ret;
    58 }
    59 void work()
    60 {
    61     int L;
    62     scanf("%d%d",&n,&L);
    63     for(int i=1;i<=n;i++){
    64         scanf("%d%d",&a[i].x,&a[i].y);
    65     }
    66     int sta=findit();
    67     swap(a[sta],a[1]);
    68     sort(a+2,a+n+1,comp);
    69     int cnt=jar(n);
    70     double ans=0;
    71     for(int i=2;i<=cnt;i++){
    72         ans+=sqrt(dis(st[i],st[i-1]));
    73     }
    74     if(cnt>=3)ans+=sqrt(dis(st[cnt],st[1]));
    75     ans+=2*L*PI;
    76     printf("%.lf
    ",ans);
    77 }
    78 
    79 int main()
    80 {
    81     
    82     int T;
    83     scanf("%d",&T);
    84     while(T--){
    85          work();
    86          if(T)printf("
    ");
    87      }
    88     return 0;
    89 }
  • 相关阅读:
    【STM32F407的DSP教程】第4章 Matlab简易使用之脚本文件
    【STM32H7的DSP教程】第3章 Matlab简易使用之基础操作
    【STM32F429的DSP教程】第3章 Matlab简易使用之基础操作
    【STM32F407的DSP教程】第3章 Matlab简易使用之基础操作
    【STM32H7的DSP教程】第2章 Matlab R2018a的安装
    【STM32F429的DSP教程】第2章 Matlab R2018a的安装
    【STM32F407的DSP教程】第2章 Matlab R2018a的安装
    【STM32H7的DSP教程】第1章 初学数字信号处理准备工作
    【STM32F429的DSP教程】第1章 初学数字信号处理准备工作
    【STM32F407的DSP教程】第1章 初学数字信号处理准备工作
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7282818.html
Copyright © 2011-2022 走看看