没想到竟然是暴力,再一次菜哭在角落。
题意:给你n个点(不包括原点),每个点都有价值,任意两个点两成的线段的价值是两点的乘积。现有一条过原点的直线。求这条直线穿过的所有嫌多的总价值。
以y轴开始顺时针遍历所有点,l代表直线左边的点的和,r代表直线右边的所有点的和。遍历过程中,如果l*r大于之前的乘积,就更新乘积。
code:
#include<cstdio>
#include<string.h>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include <set>
//a&3==a%4
using namespace std;
#define ll long long
#define mem(a) memset(a,0,sizeof(a))
const double eps=1e-8;
const int maxn=50050;//须填写
const int inf=0x3f3f3f3f;
ll max(ll a,ll b)
{
return a>b?a:b;
}
struct Point{
ll x,y;
ll value;
bool operator < (const Point &b)const
{
ll ax=x;
ll ay=y;
ll bx=b.x;
ll by=b.y;
if(ax<0)
{
ax=-ax;
ay=-ay;
}
if(bx<0)
{
bx=-bx;
by=-by;
}
return ay*bx<by*ax;
}
}p[maxn];
int main()
{
// freopen("1008.in", "r", stdin);
//freopen("sample.out", "w", stdout);
int kase,n;
ll l,r;
ll res;
scanf("%d",&kase);
while(kase--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lld%lld%lld",&p[i].x,&p[i].y,&p[i].value);
sort(p,p+n);
l=0;
r=0;
for(int i=0;i<n;i++)
{
if(p[i].x<0)
l+=p[i].value;
else if(p[i].x>0)
r+=p[i].value;
else if(p[i].y>0)
l+=p[i].value;
else
r+=p[i].value;
}
res=l*r;
for(int i=0;i<n;i++)
{
if(p[i].x>0)
{
l+=p[i].value;
r-=p[i].value;
}
else if(p[i].x<0){
l-=p[i].value;
r+=p[i].value;
}
ll tmp=l*r;
res=max(tmp,res);
}
cout<<res<<endl;
}
return 0;
}