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;
}


内容概要:本文提出了一种针对大规模电动汽车接入电网的双层优化调度策略,并基于IEEE33节点系统进行了建模与仿真分析,配套提供了完整的Matlab代码实现。该策略构建了上层电网运行优化与下层电动汽车充电调度的双层协同模型,综合考虑电网负荷削峰填谷、电压稳定性维持以及电动汽车用户充电需求满足等多重目标,采用先进的优化算法实现对电动汽车集群的智能有序调度。研究详细阐述了双层模型的构建逻辑、目标函数设计、约束条件设定及迭代求解流程,有效降低了电网峰谷差,提升了配电系统对可再生能源的消纳能力,兼具扎实的理论深度与明确的工程应用前景。; 适合人群:电气工程、电力系统及其自动化、能源系统优化等相关专业的研究生、科研人员以及从事智能电网、电动汽车调度、分布式能源管理等领域工作的工程师和技术人员。; 使用场景及目标:①深入研究高比例电动汽车接入对配电网运行特性的影响机制;②掌握电力系统双层优化建模方法及其在实际系统中的求解技巧;③实现电动汽车集群的协同调度与车网互动(V2G)优化控制;④作为撰写学术论文、开展课研究或复现高水平期刊成果的技术参考与代码基础。; 阅读建议:建议读者结合所提供的Matlab代码逐行理解双层优化模型的数学表达与程序实现细节,重点剖析上下层模型之间的信息交互机制与收敛判据,可通过调整电动汽车渗透率、充电行为参数或引入分布式电源等场景进行拓展性仿真,以深化对智能调度策略适应性的认识。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值