zoukankan      html  css  js  c++  java
  • Gym 101666A Amsterdam Distance(思维)

    Gym 101666A Amsterdam Distance

    Description

    Your friend from Manhattan is visiting you in Amsterdam. Because she can only stay for a short while, she wants to see as many tourist attractions in Amsterdam in as little time as possible. To do that, she needs to be able to figure out how long it takes her to walk from one landmark to another. In her hometown, that is easy: to walk from point m = (mx, my) to point n = (nx, ny) in Manhattan you need to walk a distance |nx − mx| + |ny − my|, since Manhattan looks like a rectangular grid of city blocks. However, Amsterdam is not well approximated by a rectangular grid. Therefore, you have taken it upon yourself to figure out the shortest distances between attractions in Amsterdam. With its canals, Amsterdam looks much more like a half-disc, with streets radiating at regular angles from the center, and with canals running the arc of the circle at equally spaced intervals. A street corner is given by the intersection of a circular canal and a street which radiates from the city center.

    Figure 1: The first sample input.
    Depending on how accurately you want to model the street plan of Amsterdam, you can split the city into more or fewer half rings, and into more or fewer segments. Also, to avoid conversion problems, you want your program to work with any unit, given as the radius of the half circle. Can you help your friend by writing a program which computes the distance between any two street corners in Amsterdam, for a particular approximation?

    Input

    The input consists of
    • One line with two integers M, N and a floating point number R.
    – 1 ≤ M ≤ 100 is the number of segments (or ‘pie slices’) the model of the city is
    split into.
    4 Problem A: Amsterdam Distance
    – 1 ≤ N ≤ 100 is the number of half rings the model of the city is split into.
    – 1 ≤ R ≤ 1000 is the radius of the city.
    • One line with four integers, ax, ay, bx, by, with 0 ≤ ax, bx ≤ M, and 0 ≤ ay, by ≤ N, the
    coordinates of two corners in the model of Amsterdam.

    Output

    Output a single line containing a single floating point number, the least distance needed to
    travel from point a to point b following only the streets in the model. The result should have
    an absolute or relative error of at most 10−6

    Sample Input 1

    6 5 2.0
    1 3 4 2
    

    Sample Output 1

    1.65663706143592
    

    Sample Input 2

    9 7 3.0
    1 5 9 5
    

    Sample Output 2

    4.28571428571429
    

    Sample Input 3

    10 10 1.0
    2 0 6 0
    

    Sample Output 3

    0
    

    题解

    题意

    仿照曼哈顿距离定义了阿姆斯特丹距离,要求求得两点的阿姆斯特丹距离最小。

    思路

    注意的关键是弧长和向内走的距离。实际上,策略只有两种,直接走到圆心,或者走到靠内的半径上再走一个弧长。原因很简单:对于是否走内圈的弧长我们可以知道弧长是theta*r,而向内路线是2*r。

    代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<string>
    #include<vector>
    #include<stack>
    #include<bitset>
    #include<cstdlib>
    #include<cmath>
    #include<set>
    #include<list>
    #include<deque>
    #include<map>
    #include<queue>
    using namespace std;
    typedef long long ll;
    const long double PI = acos(-1.0);
    const double eps = 1e-6;
    const int INF = 0x3f3f3f3f;
    int T;
    
    long double M,N,R;
    long double ax,ay,bx,by;
    
    int main() {
    
        cin >> M>>N>>R>>ax>>ay>>bx>>by;
    	long double r = R/N;
    	long double ans1 = r * (fabs(ay - by)) + acos(-1) * r * min(ay, by) * fabs(ax - bx) / M;
    	long double ans2 = r*fabs(ay+by);
    	printf("%.14Lf
    ",min(ans1,ans2));
    }
    
    
  • 相关阅读:
    CMake 从文件路径中提取文件名
    std::multimap 按照key遍历---
    Windows / Linux 一件编译zlib库
    C++ 11 可变模板参数的两种展开方式
    cmake 生成VS项目文件夹
    C++ 利用文件流复制文件
    利用 getsockname 和 getpeername 来获取某一个链接的本地地址和远端地址
    Windows 用VS编译libevent源码
    揭示同步块索引(上):从lock开始
    C手写一个多线程,供java调用
  • 原文地址:https://www.cnblogs.com/caomingpei/p/9694137.html
Copyright © 2011-2022 走看看