Rolling The Polygon (2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest)(The 2019 Asia Yinchuan First Round Online Programming)

Bahiyyah has a convex polygon with nn vertices P0,P1,,Pn1P0,P1,⋯,Pn−1 in the counterclockwise order. Two vertices with consecutive indexes are adjacent, and besides, P0P0 and Pn1Pn−1 are adjacent. She also assigns a point QQ inside the polygon which may appear on the border.

Now, Bahiyyah decides to roll the polygon along a straight line and calculate the length of the trajectory (or track) of point QQ.

To help clarify, we suppose Pn=P0,Pn+1=P1Pn=P0,Pn+1=P1 and assume the edge between P0P0 and P1P1 is lying on the line at first. At that point when the edge between Pi1Pi−1 and PiPi lies on the line, Bahiyyah rolls the polygon forward rotating the polygon along the vertex PiPi until the next edge (which is between PiPi and Pi+1Pi+1) meets the line. She will stop the rolling when the edge between PnPn and Pn+1Pn+1 (which is same as the edge between P0P0 and P1P1) meets the line again.

Input

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, the first line contains an integer n (3n50)n (3≤n≤50) indicating the number of vertices of the given convex polygon. Following nn lines describe vertices of the polygon in the counterclockwise order. The ii-th line of them contains two integers xi1xi−1 and yi1yi−1, which are the coordinates of point Pi1Pi−1. The last line contains two integers xQxQ and yQyQ, which are the coordinates of point QQ.

We guarantee that all coordinates are in the range of 103−103 to 103103, and point QQ is located inside the polygon or lies on its border.

Output

For each test case, output a line containing Case #x: y, where x is the test case number starting from 11, and y is the length of the trajectory of the point QQ rounded to 33 places. We guarantee that 44-th place after the decimal point in the precise answer would not be 44 or 55.

 

input
Copy
4
4
0 0
2 0
2 2
0 2
1 1
3
0 0
2 1
1 2
1 1
5
0 0
1 0
2 2
1 3
-1 2
0 0
6
0 0
3 0
4 1
2 2
1 2
-1 1
1 0
output
Copy
Case #1: 8.886
Case #2: 7.318
Case #3: 12.102
Case #4: 14.537

Analysis: Violence two additional data, dp [0] = dp [n], dp [n + 1] = dp [1], can be calculated according to the law of cosines AC.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define cn cin>>
#define ct cout<<
#define ed <<endl;
#define ok return 0 ;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {cin>>a[i] ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl; 
typedef long long ll;
const int INT=1e6+5;
const int maxn=10000+5;
const int len = 1e5+5;

typedef struct node
{
    int x,y,ankle,T;
}node;
node dp[len]; 
double getlen(node xx,node yy) { return ( (xx.x-yy.x)*(xx.x-yy.x) +(xx.y-yy.y)*(xx.y-yy.y) ); } //计算两点间距离 
int main()
{
    TLE;
    int t,n,k;
    cin>>t;

    for(int kk=1;kk<=t;kk++)
    {
        double sum=0;
        cin>>n;
        rep(1,n)
            cin>>dp[i].x>>dp[i].y;
        dp[0].x=dp[n].x;      dp[0].y=dp[n].y;
        dp[n+1].x=dp[1].x;    dp[n+1].y=dp[1].y;
        cin>>dp[n+2].x>>dp[n+2].y;
        rep(1,n)
        {
            double dis = getlen(dp[i],dp[n+2]);
            double d1 = getlen(dp[i],dp[i+1]);
            double d2 = getlen(dp[i],dp[i-1]); 
            double d3 = getlen(dp[i+1],dp[i-1]); 
            sum+= sqrt( dis )*abs(  acos(-1.0) - acos( ( d1+d2-d3)/(2*sqrt(d1)*sqrt(d2)) ) );
        }
        printf("Case #%d: %.3lf\n",kk,sum);
    }
    ok;
}

 




Analysis: Mathematics sign problem, (you can focus on what tips (i + 1)% n and (i-1)% n)


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define lson rt<<1, l, m  
#define rson rt<<1|1, m+1, r
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define no printf("NO\n");
#define gcd __gcd
#define cn cin>>
#define ct cout<<
#define ed <<endl;
#define ok return 0 ;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {cin>>a[i] ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define re return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl; 
typedef long long ll;
const int INT=1e6+5;
const int maxn=10000+5;
const int len = 1e5+5;

typedef struct node
{
    int x,y,ankle,T;
}node;
node dp[len]; 
double getlen(node xx,node yy) { return ( (xx.x-yy.x)*(xx.x-yy.x) +(xx.y-yy.y)*(xx.y-yy.y) ); } //计算两点间距离平方 
int main()
{
    TLE;
    int t,n,k;
    cin>>t;
    for(int kk=1;kk<=t;kk++)
    {
        double sum=0;
        cin>>n;
        rep(0,n-1)
            cin>>dp[i].x>>dp[i].y;
        cin>>dp[n+2].x>>dp[n+2].y;
        rep(0,n-1)
        {
            double dis = getlen(dp[i],dp[n+2]);
            double d1 = getlen(dp[i],dp[(i+1)%n]);
            double d2 = getlen(dp[i],dp[(i-1+n)%n]); 
            double d3 = getlen(dp[(i+1)%n],dp[(i-1+n)%n]);     
            sum+= sqrt( dis )*abs(  acos(-1.0) - acos( ( d1+d2-d3)/(2*sqrt(d1)*sqrt(d2)) ) );        
        }
        printf("Case #%d: %.3lf\n",kk,sum);
    }
    ok;
}

 

 

 

Guess you like

Origin www.cnblogs.com/Shallow-dream/p/11441375.html