Problem F Triangles

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.
题目大意:
一个圆上有n个点,则给出其中有n隔间的长度,求组成等边三角形的方案数
思路:1到n个点遍历,从当前点到三分之一弧度长的地方应该有一个点,否则构不成等边三角形,有则下一个三分之一弧度长的地方应该有一个点,若有则方案数加一,最后输出ans/3(每一个方案遍历了三次);
重点是求出每一个点之后长度表示,前缀和与lower_bound来求区间长度降低复杂度。
附ac代码:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[200005],pre[200005];
int main() {
	int n,sum=0,ev,ans=0,k=0,count;
	cin>>n;
	for(int i=0; i<n; i++) {
		cin>>a[i];
		sum+=a[i];
		a[i+n]=a[i];
	}
	pre[0]=a[0];
	for(int i=1; i<2*n; i++) {
		pre[i]=pre[i-1]+a[i];
	}
	ev=sum/3;
	if(ev*3!=sum) {
		cout<<0;
		return 0;
	}

	for(int i=0; i<n; i++) {

		if(i)
			count=ev+pre[i-1];
		else
			count=ev;
		k=lower_bound(pre,pre+2*n,count)-pre;
		if(k>=i+n) {
			continue;
		}
		count=pre[k]+ev;
		k=lower_bound(pre,pre+2*n,count)-pre;
		if(k>=i+n) {
			continue;
		}
		count=pre[k]+ev;
		k=lower_bound(pre,pre+2*n,count)-pre;
		if(k>=i+n) {
			continue;
		}
		ans++;
	}
	cout<<ans/3;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值