Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1258 Accepted Submission(s): 533
Problem Description
There are
n
points on the plane, and the
i
th points has a value
val
i![]()
, and its coordinate is
(x
i
,y
i
)
. 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×10
4
)
.
The next n
lines, the
i
th line contains three integers
x
i
,y
i
,val
i
(|x
i
|,|y
i
|≤10
9
,1≤val
i
≤10
4
)
.
For each test case:
The first line contains a positive integer n(1≤n≤5×10
The next n
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
Source
Recommend
题目大意:给定 n 个点,和权值,他们两两相连,每条边的权值就是他们两个点权值的乘积,任意两点之间的直线不经过原点,让你从原点划一条直线,使得经过的直线的权值和最大。
解题思路:极角排序后扫描一圈比大小。
AC代码
#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-8;
const double pi=acos(-1.0);
const int K=1e6+7;
const int mod=1e9+7;
struct Point
{
LL x,y,v;
}pt[K],st;
LL cross(const Point &po,const Point &ps,const Point &pe)
{
return (ps.x-po.x)*(pe.y-po.y)-(pe.x-po.x)*(ps.y-po.y);
}
bool cmp(const Point &ta,const Point &tb)
{
return cross(st,ta,tb)>0;
}
int main(void)
{
int t,n;cin>>t;
while(t--)
{
int se;
LL s1=0,s2=0,ans=0,mx=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%lld%lld%lld",&pt[i].x,&pt[i].y,&pt[i].v);
pt[i+n].x=-pt[i].x;
pt[i+n].y=-pt[i].y;
pt[i+n].v=0;
s2+=pt[i].v;
}
if(n==1) {printf("0\n");continue;}
sort(pt,pt+2*n,cmp);
s1+=pt[0].v;
for(int i=1;i<2*n;i++)
if(cross(st,pt[0],pt[i])<0)
{
se=i-1;break;
}
else
s1+=pt[i].v;
s2-=s1;
mx=ans=s1*s2;
for(int i=1,r=se;i<se;i++)
{
ans+=-pt[i].v*s2+(s1-pt[i].v)*pt[i].v;
s2+=pt[i].v,s1-=pt[i].v;
while(r+1<2*n&&cross(st,pt[i],pt[r+1])>=0)
{
r++;
ans+=-pt[r].v*s1+(s2-pt[r].v)*pt[r].v;
s1+=pt[r].v,s2-=pt[r].v;
}
mx=max(mx,ans);
}
printf("%lld\n",mx);
}
return 0;
}