Hard challenge
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 572 Accepted Submission(s): 232
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
题意:
平面上有n个点,每个点有一个value值,每两个点之间都有一条线,这条线的value是两点的value之积,给出这些点的·坐标,并且这些直线不过原点,求过原点一条线,切割的线的value之和最大。
解题思路:
因为过原点的直线不过给出的任何一点,所以给出的点要么在直线的一侧,要么在另一侧。此时切割的线的value之和为左边各个点的权重和L,与右边的各个点的权重和R的乘积R*L。
先求出各个点的斜率,根据斜率对他们进行排序,先让直线指向第一个点(看作枚举的这条直线偏向这个点右边一点进行处理),求出L
和R,ans=l*R,再枚举直线指向其他已排好序点,按照同样的方法处理,并且更新ans=max(ans,L*R),当把每个点都枚举一遍,答案就出来了。
代码:
C++ Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#include<bits/stdc++.h>
#define maxn 50005 #define pi 4*atan( 1. 0) #define ll long long using namespace std; ll sum_L, sum_R; int n; struct Node { int x, y, v; double s; bool is_up; ///值为1时,表示在x轴上方 } node[maxn]; double get_s( int x, int y) { if(x < 0 && y > 0) return pi - atan(-( double)y / ( double)x); if(x > 0 && y < 0) return pi - atan(-( double)y / ( double)x); return atan(( double)y / ( double)x); } double get_up( int x, int y) { if(y > 0) return 1; else if(y < 0) return 0; else { if(x < 0) return 1; else return 0; } } bool cmp(Node a, Node b) { return a.s < b.s; } ll work() { ll ans = sum_L * sum_R; double cur_s = 0; int i = 1; while(node[i].s == 0) i++; while(i <= n) { cur_s = node[i].s; while(node[i].s == cur_s) { if(node[i].is_up == 1) { sum_L -= node[i].v; sum_R += node[i].v; } else { sum_L += node[i].v; sum_R -= node[i].v; } i++; } ans = max(ans, sum_L * sum_R); } return ans; } int main() { int T; scanf( "%d", &T); while(T--) { sum_L = sum_R = 0; scanf( "%d", &n); for( int i = 1; i <= n; i++) { scanf( "%d%d%d", &node[i].x, &node[i].y, &node[i].v); node[i].s = get_s(node[i].x, node[i].y); ///计算各个点的斜率 node[i].is_up = get_up(node[i].x, node[i].y); ///判断在x轴上方还是下方,枚举的第一条直线为x轴 if(node[i].is_up == 1) sum_L += node[i].v; else sum_R += node[i].v; } sort(node + 1, node + 1 + n, cmp); ///根据斜率进行排序 ll ans = work(); printf( "%lld\n", ans); } return 0; } |