Hard challenge(数学)
2017 Multi-University Training Contest - Team 7
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 367 Accepted Submission(s): 131
Problem Description
There are points on the plane, and the th points has a value , and its coordinate is . 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 , denoting the number of test cases.
For each test case:
The first line contains a positive integer .
The next lines, the th line contains three integers .
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
Source
2017 Multi-University Training Contest - Team 7
以x轴为分界线对点进行分区,第一次以x坐标为分区依据。
对斜率进行排序,依y轴为依据进行扫描。求最大值。
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long int ll;
const int maxx=1e5;
struct node
{
double x,y,value;
}as[maxx];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int xx,yy,cc;
int ss=0,sss=0;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&xx,&yy,&cc);
if(xx==0) as[i].x=yy<0?-1e10:1e10;//如果xx==0 斜率就 0.0
else
as[i].x=yy*1.0/(xx*1.0);//保存斜率
as[i].y=xx;//保存 Xi
as[i].value=cc;
if(xx>=0) ss+=cc;//ss保存的是当Xi>=0时间的value的值的和
else sss+=cc;//sss保存的是当Xi<0时间的value的值的和
}
ll ans=ss*sss;
sort(as,as+n,cmp);//对斜率排序
for(int i=0;i<n;i++)//扫描一圈
{
if(as[i].y>=0)
ss-=as[i].value,sss+=as[i].value;
else
ss+=as[i].value,sss-=as[i].value;
ans=max(ans,(ll)ss*sss);
}
printf("%lld\n",ans);
}
return 0;
}