Codeforces Educational Round 1 C题

本文介绍了一种算法,用于找出一组向量中夹角最小的两个向量,并提供了两种实现方式及其性能对比。

C. Nearest vectors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given the set of vectors on the plane, each of them starting at the origin. Your task is to find a pair of vectors with the minimal non-oriented angle between them.

Non-oriented angle is non-negative value, minimal between clockwise and counterclockwise direction angles. Non-oriented angle is always between 0 and π. For example, opposite directions vectors have angle equals to π.

Input

First line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of vectors.

The i-th of the following n lines contains two integers xi and yi (|x|, |y| ≤ 10 000, x2 + y2 > 0) — the coordinates of the i-th vector. Vectors are numbered from 1 to n in order of appearing in the input. It is guaranteed that no two vectors in the input share the same direction (but they still can have opposite directions).

Output

Print two integer numbers a and b (a ≠ b) — a pair of indices of vectors with the minimal non-oriented angle. You can print the numbers in any order. If there are many possible answers, print any.

Sample test(s)
input
4
-1 0
0 -1
1 0
1 1
output
3 4
input
6
-1 0
0 -1
1 0
1 1
-4 -5
-4 -6
output
6 5


题意:有n个向量,起点都为原点,给出终点,让你求出这些向量中夹角最小的两个向量。

思路:赛中的思路是求出所有VEC与VEC(0,1)的夹角,再排序,然后扫一遍就OK了,赛中是一发过了,但是赛后被HACK掉了,看了别人的代码发现需要用long double否则精度不够,做计算几何一定要小心精度问题。

这是被hack的数据:

4
-9901 9900
-10000 9899
9899 9801
9899 9900

ans is    3   4
 ~真是牛逼啊,这么极端的数据 。

注意的地方:

1.我求角度时用的是大白书上面的模板,将double换成long double后会比较慢,看了别人的代码发现求角度可以用atan(y,x)这个函数。

2.科普一下  atan2(y,x):

atan结果为正表示从 X 轴逆时针旋转的角度,结果为负表示从 X 轴顺时针旋转的角度。
ATAN2(a, b) 与 ATAN(a/b)稍有不同,ATAN2(a,b)的取值范围介于 -pi 到 pi 之间(不包括 -pi),
而ATAN(a/b)的取值范围介于-pi/2到pi/2之间(不包括±pi/2)。

3.在改数组为long double 后,要记得把函数返回的参数也改成long double,否则然并卵。

下面是我改过的代码,用大白书上的模板,跑了1000ms +:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-15;

int dcmp(LD x){
    if(fabs(x)<=eps)  return 0;
    if(x<0)  return -1;
    return 1;
}

struct Point
{
    LD x,y;
    Point(){}
    Point(double _x,double _y){
        x = _x;y = _y;
    }
}p[100005];

typedef Point Vec;

LD Dot (Vec A,Vec B)  { return A.x*B.x+A.y*B.y; }

LD Length(Vec A)  { return sqrt(Dot(A,A)); }

LD Angle(Vec A,Vec B)  { return acos(Dot(A,B)/Length(A)/Length(B)); }

LD ang[100005];
int num[100005];

bool cmp(int a,int b){
    return ang[a]<ang[b];
}

int main (){
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++){
            cin>>p[i].x>>p[i].y;
            ang[i]=Angle(Vec(p[i].x,p[i].y),Vec(0,10000));
            if(ang[i]<pi&&p[i].x<0)
                ang[i]=2*pi-ang[i];
            num[i]=i;
        }
        cout.precision(100);
        //cout<<endl;
        sort(num+1,num+n+1,cmp);
        //for(int i=1;i<=n;i++)
        //    cout<<num[i]<<"   "<<ang[num[i]]<<endl;

        LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
        LD minn=mid;
        int ot1=num[1],ot2=num[n];
        //cout<<minn<<endl;
        for(int i=2;i<=n;i++){
            mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
            //cout<<mid<<endl;
            if(minn>mid){
                minn=mid;
                ot1=num[i-1];
                ot2=num[i];
                //cout<<ot1<<"   "<<ot2<<endl;
            }
        }
        printf("%d %d\n",ot1,ot2);
    }
    return 0;
}

下面是这场CF rank1的兄弟的代码,简洁明了,速度飞快 63ms:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
using namespace std;

typedef long double LD;
const LD pi=acos(-1.0);
const LD eps=1e-20;

int dcmp(LD x){
    if(fabs(x)<eps)
        return 0;
    else
        return x<0 ? -1 : 1;
}

LD ang[100005];
int num[100005];

bool cmp(int a,int b){
    return ang[a]<ang[b];
}

int main (){
    cout.precision(12);
    int n;
    while(scanf("%d",&n)!=EOF){
        int x,y;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            ang[i]=atan2(1.0*y,1.0*x);
            num[i]=i;
        }
        sort(num+1,num+n+1,cmp);
        LD mid=min(fabs(ang[num[1]]-ang[num[n]]),2*pi-fabs(ang[num[1]]-ang[num[n]]));
        LD minn=mid;
        int ot1=num[1],ot2=num[n];
        for(int i=2;i<=n;i++){
            mid=min(fabs(ang[num[i-1]]-ang[num[i]]),2*pi-fabs(ang[num[i-1]]-ang[num[i]]));
            if(minn>mid){
                minn=mid;
                ot1=num[i-1];
                ot2=num[i];
            }
        }
        printf("%d %d\n",ot1,ot2);
    }
    return 0;
}


基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究(Matlab代码实现)内容概要:本文围绕“基于可靠性评估序贯蒙特卡洛模拟法的配电网可靠性评估研究”,介绍了利用Matlab代码实现配电网可靠性的仿真分析方法。重点采用序贯蒙特卡洛模拟法对配电网进行长时间段的状态抽样与统计,通过模拟系统元件的故障与修复过程,评估配电网的关键可靠性指标,如系统停电频率、停电持续时间、负荷点可靠性等。该方法能够有效处理复杂网络结构与设备时序特性,提升评估精度,适用于含分布式电源、电动汽车等新型负荷接入的现代配电网。文中提供了完整的Matlab实现代码与案例分析,便于复现和扩展应用。; 适合人群:具备电力系统基础知识和Matlab编程能力的高校研究生、科研人员及电力行业技术人员,尤其适合从事配电网规划、运行与可靠性分析相关工作的人员; 使用场景及目标:①掌握序贯蒙特卡洛模拟法在电力系统可靠性评估中的基本原理与实现流程;②学习如何通过Matlab构建配电网仿真模型并进行状态转移模拟;③应用于含新能源接入的复杂配电网可靠性定量评估与优化设计; 阅读建议:建议结合文中提供的Matlab代码逐段调试运行,理解状态抽样、故障判断、修复逻辑及指标统计的具体实现方式,同时可扩展至不同网络结构或加入更多不确定性因素进行深化研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值