试了很多种办法,最后还是选择了题解的办法,确实y轴来区分很简单方便,也省了很多事。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=5*1e4+5;
struct node
{
ll dx,dy,val;
} a[maxn];
bool cmp(node a,node b)
{
ll ax,ay,bx,by;
if(b.dx<0)
bx=-b.dx,by=-b.dy;
else
bx=b.dx,by=b.dy;
if(a.dx<0)
ax=-a.dx,ay=-a.dy;
else
ax=a.dx,ay=a.dy;
return ay*bx<by*ax;
}
int main()
{
freopen("C:\\Users\\Со·І\\Desktop\\КэѕЭ\\1008.in","r",stdin);
int t,n,i,j;
scanf("%d",&t);
while(t--&&scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%lld%lld%lld",&a[i].dx,&a[i].dy,&a[i].val);
sort(a,a+n,cmp);
ll l=0,r=0;
for(i=0;i<n;i++)
if(a[i].dx<0)
l+=a[i].val;
else if(a[i].dx>0)
r+=a[i].val;
else if(a[i].dy>0)
l+=a[i].val;
else
r+=a[i].val;
ll res=l*r;
for(i=0;i<n;i++)
{
if(a[i].dx<0)
l-=a[i].val,r+=a[i].val;
else if(a[i].dx>0)
r-=a[i].val,l+=a[i].val;
else if(a[i].dy>0)
l-=a[i].val,r+=a[i].val;
else
r-=a[i].val,l+=a[i].val;
res=max(res,l*r);
}
printf("%lld\n",res);
}
return 0;
}