ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Rolling The Polygon (2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest)(The

2019-09-01 09:00:53  阅读:306  来源: 互联网

标签:Provincial getlen point Programming 2019 line include dp define


Bahiyyah has a convex polygon with nn vertices P0,P1,⋯,Pn−1P0,P1,⋯,Pn−1 in the counterclockwise order. Two vertices with consecutive indexes are adjacent, and besides, P0P0 and Pn−1Pn−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 Pi−1Pi−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 (3≤n≤50)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 xi−1xi−1 and yi−1yi−1, which are the coordinates of point Pi−1Pi−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

分析:暴力附加两个数据,dp[0] = dp[n] , dp[n+1]=dp[1],按余弦定理计算就能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;
}

 




分析:数学签到题,( 可以重点关注一下 小技巧 (i+1)%n 和 (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;
}

 

 

 

标签:Provincial,getlen,point,Programming,2019,line,include,dp,define
来源: https://www.cnblogs.com/Shallow-dream/p/11441375.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有