Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1265 Accepted Submission(s): 535
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 integern(1≤n≤5×104) .
The nextn 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
The next
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
平面直角坐标系上有n个整点,第i个点有一个点权vali,坐标为(xi,yi),其中不存在任意两点连成的直线经过原点。这些整点两两之间连有一条线段,线段的权值为其两端点的权值之积。你需要作一条过原点而不过任意一个给定整点的直线,使得和这条直线相交的线段的权值和最大。
则这条最优的线一定是逼近于一个点,根据斜率的大小,枚举每一个点,判断每次左边的点数和和右边的点数和,每次优化ans即可
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define ms(x,y) memset(x,y,sizeof(x)) using namespace std; typedef long long ll; const double pi = acos(-1.0); const int mod = 1e9 + 7; const int maxn = 5e4 + 5; struct Node { ll x, y, val; } a[maxn]; bool cmp(Node a, Node b) { ll x1 = a.x, x2 = b.x, y1 = a.y, y2 = b.y; if (x1 < 0) { x1 = -x1; y1 = -y1; } if (x2 < 0) { x2 = -x2; y2 = -y2; } return y1 * x2 < x1 * y2; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while (t--) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld%lld%lld", &a[i].x, &a[i].y, &a[i].val); } sort(a, a + n, cmp); ll l = 0, r = 0; for (int i = 0; i < n; i++) { if (a[i].x < 0) { l += a[i].val; } else r += a[i].val; } ll ans = l * r; for (int i = 0; i < n; i++) { if (a[i].x < 0) { l -= a[i].val; r += a[i].val; } else { l += a[i].val; r -= a[i].val; } ans = max(ans, l * r); } printf("%lld ", ans); } return 0; }