2018暑期SICNU-ACM组集训报告(9)

题目:
Triangles
File: triangulos.[c|cpp|java]
You will be given N points on a circle. You must write a program to determine how many distinct
equilateral triangles can be constructed using the given points as vertices.
The figure below illustrates an example: (a) shows a set of points, determined by the lengths of
the circular arcs that have adjacent points as extremes; and (b) shows the two triangles which can be
built with these points.

image.png

Input
The first line of the input contains an integer N, the number of points given. The second line contains
N integers Xi
, representing the lengths of the circular arcs between two consecutive points in the
circle: for 1 ≤ i ≤ (N − 1), Xi represents the length of the arc between between points i and i + 1;
XN represents the length of the arc between points N and 1.
Output
Your program must output a single line, containing a single integer, the number of distinct equilateral
triangles that can be constructed using the given points as vertices.
Restrictions
• 3 ≤ N ≤ 105
• 1 ≤ Xi ≤ 103
, for 1 ≤ i ≤ N
Examples
Input
8
4 2 4 2 2 6 2 2
Output
2
Input
6
3 4 2 1 5 3
Output
1

原题链接:https://odzkskevi.qnssl.com/1627a677139fc8e306696b4e41953e0f?v=1533137884 //Problem F

题意:圆上N个点把弧长分为N条,问能构成几个等边三角形

解题思路:一来就打算用弧长算出边长再比较的我是真的思路清奇,弧长相等即可。
一开始过于复杂度太高超时,用前缀和和二分算法优化了;
但直接弧长后卡在第4组测试数据;
这是一个圆,是成圈圈的,不是只能到头,所以我判定两次之后再把答案整除三即可得到正确答案。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=10000005;
int a[maxn];
ll pre[maxn*2];
 
int main()
{
    int n,i,j,k;
    cin>>n;
    ll sum=0;
    for(i=0;i<n;i++)
    {
        
        cin>>a[i],sum+=a[i];
        a[i+n]=a[i];
    }
    pre[0]=a[0];
    for(i=1;i<2*n;i++)
    pre[i]+=pre[i-1]+a[i];
    if(sum%3)
    {
        cout<<"0"<<endl;
        return 0;
    }
    ll t=sum/3,s=0,temp;
    for(i=0;i<n;i++)
    {
        ll c;
        if(i)
            c=pre[i-1]+t,temp=i-1;
        else
            c=t,temp=i;

        bool flag=0;
        int x=lower_bound(pre,pre+2*n,c)-pre;       
        if(x>=i+n)
            flag=1;
        
        if(flag)
            continue;

        c=pre[x]+t;temp=x;
        x=lower_bound(pre,pre+2*n,c)-pre;

        if(x>=i+n)
            flag=1;
        if(flag)
            continue;
        c=pre[x]+t;temp=x;
        x=lower_bound(pre,pre+2*n,c)-pre;

        if(x>=i+n)
            flag=1;
        if(flag)
            continue;
        if(!flag)
            s++;
    }
    cout<<s/3;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 创业与人合作的基础是什么? 是诚信! 首先,互相之间要互相坦诚,各自的优劣,脾性,得失都要可以拿出来谈。能来诚布公...
    浅浅君子阅读 2,187评论 0 0
  • 《早》 破晓 天慢慢亮了 攒了两天的乌云一点点散去了 只留下参差各异的残影 蓝色的,白色的,红色的 五点十一分 我...
    眼白阅读 1,769评论 0 1
  • 不知道为何,睡的也不太早,竟然不到五点就醒了,然后抽了礼物送给小姐姐,不知道小姐姐看到了没有/doge。哎,有时候...
    永恒yxh阅读 1,328评论 0 1

友情链接更多精彩内容