hdu Hard challenge (几何题)

探讨了在平面上给定点集的情况下,如何通过绘制特定直线以最大化该直线所穿越线段的权值总和的问题。文章介绍了算法设计思路、关键步骤及实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hard challenge

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1207    Accepted Submission(s): 495

 

 

Problem Description

There are n points on the plane, and the ith points has a value vali, and its coordinate is (xi,yi). It is guaranteed that no two points have the same coordinate, and no two points makes the line which passes them also passes the origin point. For every two points, there is a segment connecting them, and the segment has a value which equals the product of the values of the two points. Now HazelFan want to draw a line throgh the origin point but not through any given points, and he define the score is the sum of the values of all segments that the line crosses. Please tell him the maximum score.

 

 

Input

The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
The first line contains a positive integer n(1≤n≤5×104).
The next n lines, the ith line contains three integers xi,yi,vali(|xi|,|yi|≤109,1≤vali≤104).

 

 

Output

For each test case:
A single line contains a nonnegative integer, denoting the answer.

 

 

Sample Input

 

2 2 1 1 1 1 -1 1 3 1 1 1 1 -1 10 -1 0 100

 

 

Sample Output

 

1
1100

 

 

题意:

平面直角坐标系上有n个整点,第i个点有一个点权vali​​,坐标为(xi,yi),其中不存在任意两点连成的直线经过原点。这些整点两两之间连有一条线段,线段的权值为其两端点的权值之积。你需要作一条过原点而不过任意一个给定整点的直线,使得和这条直线相交的线段的权值和最大。

 

解析:

 

这道题主要的思路就是一条直线,在这条直线两边的点连起来的线段一定经过这一条直线


所以我们就可以枚举每一条待求的直线,来看每条直线两边的点的分布情况来计算这条直线经过的线段权值和
而枚举每一条直线的时候,两边点的分布情况是一个个区间分布的,也就是说在一段区间内的直线两边点(题目给的那些点)的分布情况是相同的,在进一步看可以发现这一段段区间是按每一个点斜率分布的

那么这样就可以通过枚举每一个点的斜率来求解,O(n)。


那么首先就需要将这些点按斜率从小到大排序(代码中用他们与x轴正半轴的夹角来表示斜率)
在每一次枚举一个点时,可以看作是画一条过原点斜率大于改点又小于下一点的直线(其实就可以看成一条稍稍偏离改点,偏向下一个点的直线)
然后,每一次枚举就只需要将这条直线左边点的权值和lsum*右边点的权值和rsum
这里枚举每一个区间找直线左右两边的点的时候,只需要一开始第一个点的时候找n-1次,之后只需要将遍历到的那个点从原来的区间换到另一个区间就行了(例如,遍历到第三个点时,这个点在原来是在左区间(右区间),那么将他拿出来放到右区间(左区间)就可以了)

 

这里题目中给的任意两个点的连线不经过原点的条件是表明在每一个点的斜率是重复的,就是你从一开始不断往上扫的过程中不会同时碰到两个点(即这条直线
经过两个点)

 

 

 

 

 

#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<set>
using namespace std;

typedef long long ll;

const int MAXN = 50000+10;

typedef struct node
{
	ll mu;  //x
	ll zi;  //y 
	ll val;
	double angle;
}node;

node grad[MAXN];


ll cross(ll x1,ll y1,ll x2,ll y2)
{
	return (x1*y2-y1*x2);
}

ll compare(node a,node b,node c)
{
	return cross((b.mu-a.mu),(b.zi-a.zi),(c.mu-a.mu),(c.zi-a.zi));
}


bool cmp(node a,node b)
{
	return a.angle<b.angle;
}



int main()
{
	int t,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			scanf("%lld%lld%lld",&grad[i].mu,&grad[i].zi,&grad[i].val);
			grad[i].angle=atan(1.0*grad[i].zi/grad[i].mu);   //计算角度
		}
		sort(grad+1,grad+n+1,cmp);

		ll rsum,lsum;
		set<int>a,b;  //a表示左区间内的点,b表示右区间内的点
		node c;
		c.mu=0;
		c.zi=0;
		rsum=lsum=0;   
		for(int i=2;i<=n;i++)  //初始化左右区间
		{
			if(compare(grad[1],grad[i],c)>0)
			{
				lsum+=grad[i].val;
				a.insert(i);
			}
			else
			{
				rsum+=grad[i].val;
				b.insert(i);
			}
		}
		rsum+=grad[1].val;
		b.insert(1);
		ll ans;
		ans=lsum*rsum;  
		for(int i=2;i<=n;i++)  //开始遍历每一个点
		{
			if(a.count(i))    //i之前在左区间
			{
				lsum-=grad[i].val;
				rsum+=grad[i].val;
				ans=max(ans,lsum*rsum);
				a.erase(i);
				b.insert(i);
			}
			else   //i之前在右区间
			{
				lsum+=grad[i].val;
				rsum-=grad[i].val;
				ans=max(ans,lsum*rsum);
				b.erase(i);
				a.insert(i);
			}
		}
		printf("%lld\n",ans);


	}
	return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值