HDU 6127 Hard challenge【计算机几何】【思维题】

19 篇文章 0 订阅
7 篇文章 0 订阅

Hard challenge

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


Problem Description
There are  n  points on the plane, and the  i th 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(1T5) , denoting the number of test cases.
For each test case:
The first line contains a positive integer  n(1n5×104) .
The next  n  lines, the  i th line contains three integers  xi,yi,vali(|xi|,|yi|109,1vali104) .
 

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),其中不存在任意两点连成的直线经过原点。这些整点两两之间连有一条线段,线段的权值为其两端点的权值之积。你需要作一条过原点而不过任意一个给定整点的直线,使得和这条直线相交的线段的权值和最大。

则这条最优的线一定是逼近于一个点,根据斜率的大小,枚举每一个点,判断每次左边的点数和和右边的点数和,每次优化ans即可

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ms(x,y) memset(x,y,sizeof(x))
using namespace std;

typedef long long ll;

const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 5e4 + 5;

struct Node
{
	ll x, y, val;
} a[maxn];

bool cmp(Node a, Node b)
{
	ll x1 = a.x, x2 = b.x, y1 = a.y, y2 = b.y;
	if (x1 < 0)
	{
		x1 = -x1;
		y1 = -y1;
	}
	if (x2 < 0)
	{
		x2 = -x2;
		y2 = -y2;
	}
	return y1 * x2 < x1 * y2;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
			scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].val);
		}
		sort(a, a + n, cmp);
		ll l = 0, r = 0;
		for (int i = 0; i < n; i++)
		{
			if (a[i].x < 0)
			{
				l += a[i].val;
			}
			else r += a[i].val;
		}
		ll ans = l * r;
		for (int i = 0; i < n; i++)
		{
			if (a[i].x < 0)
			{
				l -= a[i].val;
				r += a[i].val;
			}
			else
			{
				l += a[i].val;
				r -= a[i].val;
			}
			ans = max(ans, l * r);
		}
		printf("%lld\n", ans);
	}
	return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值