Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 915 Accepted Submission(s): 372
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(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 i th line contains three integers xi,yi,vali(|xi|,|yi|≤109,1≤vali≤104) .
For each test case:
The first line contains a positive integer n(1≤n≤5×104) .
The next n lines, the i th 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.
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个点的坐标和点的价值,任意两个点之间连成线段,线段的权值为两点的价值之积,且线段所延长成的直线不会经过原点,一条经过原点的直线穿过线段就得到了这条线段的权值,且直线不许经过端点,求直线能得到权值和的最大值
思路:若现有一条直线,那么直线左侧的点的价值和为suml,右侧的点的价值和为sumr,那直线能获得的权值和就是suml*sumr(根据乘法的分配律)。根据题中所说的任意两点之间的直线不经过原点,这一条件我们可以直接对点的极角进行排序,然后从极角小的枚举到极角大的,每一次枚举仅是在sumr或suml更新了新的一个点,每次枚举只会有一个点有变化
我们先根据x<=0和x>0将点分到suml和sumr中(相当于最初的直线是以y为直线的),然后我们根据排序好的点,依次枚举相当于逆时针旋转直线,直线先到1点,那么把1点放到suml集合中sumr去点1点,最开始时1点在sumr中,得到一个新的ans,我们这样依次枚举点,进行加点去点操作,即得到了所有的情况,那么取其中最大值即可,复杂度就是O(n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int maxn=50005;
struct point
{
int x,y;
int val;
double angle;
} a[maxn];
bool cmp(point a,point b)
{
return a.angle<b.angle;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].val);
a[i].angle=atan(1.0*a[i].y/a[i].x);
}
sort(a,a+n,cmp);
long long suml=0,sumr=0,ans;
for(int i=0; i<n; i++)
if(a[i].x>0)
sumr+=a[i].val;
else
suml+=a[i].val;
ans=suml*sumr;
for(int i=0; i<n; i++)
{
if(a[i].x>0)
sumr-=a[i].val,suml+=a[i].val;
else
sumr+=a[i].val,suml-=a[i].val;
ans=max(ans,suml*sumr);
}
printf("%lld\n",ans);
}
return 0;
}