LightOJ1203->求凸包最小内角角度

LightOJ1203


题意:

被转化后的问题其实就是求凸包最小的内角角度。

题解:

构造一个凸包,通过余弦定理遍历所有内角,求出最小角度。

代码:

#include <stdio.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <float.h>
using namespace std ;
#define MAX 100005
#define PI acos(-1.0)
const double eps = 1e-8 ;
int sgn(double x)
{
    if(fabs(x) < eps) return 0 ;
    if(x < 0) return -1 ;
    else return 1 ;
}
struct Point
{
    double x , y ;
    Point(){}
    Point(double _x , double _y)
    {
        x = _x ; y = _y ;
    }
    Point operator - (const Point &b) const
    {
        return Point(x - b.x , y - b.y) ;
    }
    double operator ^ (const Point &b) const
    {
        return x*b.y - y*b.x ;
    }
    double operator * (const Point &b) const
    {
        return x*b.x + y*b.y ;
    }
};
bool mult(Point sp,Point ep,Point op)
{
  return (sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
}
bool operator < (const Point &l, const Point &r)
{
   return l.y < r.y ||(l.y == r.y && l.x < r.x);
}
Point list[MAX] ;
Point Stack[MAX] ;
int top ;

int Graham(Point pnt[],int n, Point res[])//构造凸包
{
  int i,len ,k = 0,top = 1;
  sort(pnt,pnt+n);
  if(n == 0)return 0; res[0] = pnt[0];
  if(n == 1)return 1; res[1] = pnt[1];
  if(n == 2)return 2; res[2] = pnt[2];
  for(int i =2; i < n; ++ i){
    while(top && mult(pnt[i],res[top],res[top-1]))
        top--;
    res[++top] = pnt[i];
  }
  len = top; res[++top] = pnt[n - 2];
  for(i = n - 3; i >= 0; -- i){
    while(top!=len && mult(pnt[i],res[top],res[top-1]))
        top--;
    res[++top] = pnt[i];
  }
  return top;//返回凸包中点的个数
}
double len(Point A,Point B)//返回向量AB的模
{
  return hypot(A.x-B.x,A.y-B.y);
}

double dot(Point A,Point B,Point C)//点乘
{
  return (C.x-A.x)*(B.x-A.x)+(C.y-A.y)*(B.y-A.y);
}

double get(Point A,Point B,Point C)//余弦定理
{
  return acos(dot(A,B,C)/len(A,B)/len(A,C));
}
double minangle()//遍历凸包所有点,余弦定理求角
{
  if(top < 3) return 0;
  double ans = 2 * PI;
  Stack[top] = Stack[0];Stack[top+1] = Stack[1];
  for(int i = 1; i<= top; ++ i){
    ans = min(ans,get(Stack[i],Stack[i+1],Stack[i-1]));
  }
  return ans/PI*180;
}
int main()
{
    int T ,n ,a , b;
    scanf("%d" , &T) ;
    for(int cas = 1 ; cas <= T ; cas ++)
    {
        scanf("%d" , &n) ;
        for(int i = 0 ; i < n ; i ++)
        {
            scanf("%lf%lf" , &list[i].x,&list[i].y) ;
        }
        top = Graham(list ,n , Stack) ;
        printf("Case %d: %.10f\n" , cas , minangle()) ;
    }
    return 0 ;
}
/*
4
4
25 882
512 -762
-572 -942
647 -65
10
91 973
-980 -668
-509 993
295 -144
-524 397
-925 278
-720 584
-34 358
-815 922
-655 -83
4
-126006026 -879795176
-47582593 -8952
3275 -272575414
-458441662 5158
14
-285 157597519
980485785 763819290
-496900235 -476020953
-900802943 283817053
-375048494 -663706092
-664553421 9601
-9224 -923287608
137739042 -429347330
729407623 -105
134941774 -498256533
-546543123 -761186661
-136235155 -152798162
1159 -8360
-938193474 223721588

Case 1: 51.420756489
Case 2: 64.330894519
Case 3: 32.422781562
Case 4: 57.490203768
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值