Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1103 Accepted Submission(s): 457
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
解:乘积的和等于和的乘积,首先分成左右两部分,然后遍历所有的点
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<string>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N =1e5+10;
const int inf = 0x3f3f3f3f;
struct node
{
LL v;
double x, y, deg;
bool operator<(const node &A)const
{
return deg>A.deg;
}
}p[N], a[N], b[N];
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
int k1=0, k2=0;
LL s1=0, s2=0;
for(int i=0;i<n;i++)
{
scanf("%lf %lf %lld", &p[i].x,&p[i].y,&p[i].v);
if(p[i].x<=0)
{
if(p[i].x==0)
{
p[i].deg=inf;
if(p[i].y<0) p[i].deg=-p[i].deg;
}
else p[i].deg=p[i].y/p[i].x;
a[k1++]=p[i],s1+=p[i].v;
}
else
{
p[i].deg=p[i].y/p[i].x;
b[k2++]=p[i],s2+=p[i].v;
}
}
sort(a,a+k1);
sort(b,b+k2);
LL ans=(s1*s2);
int l=0;
LL s3=s1, s4=s2;
for(int i=0;i<k2;i++)
{
while(l<k1&&a[l].deg>=b[i].deg)
{
s1-=a[l].v,s2+=a[l].v;
ans=max(ans,s1*s2);
l++;
}
s1+=b[i].v,s2-=b[i].v;
ans=max(ans,s1*s2);
}
l=0,s1=s3,s2=s4;
for(int i=0;i<k1;i++)
{
while(l<k2&&b[l].deg>=a[i].deg)
{
s2-=b[l].v,s1+=b[l].v;
ans=max(ans,s1*s2);
l++;
}
s1-=a[i].v,s2+=a[i].v;
ans=max(ans,s1*s2);
}
printf("%lld\n",ans);
}
return 0;
}