题目链接

Problem Description
There are n points on the plane, and the ith 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 ith 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.
 
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 个点,每个点有个权值,点与点之间可以连成线段,线段的权值就是两个端点的权值乘积。任意两个点与原点不可能在一条直线上,求一条直线穿过的线段的最大权值和?
 
思路:我们可以想到,有一条过原点的直线,那么直线穿过的线段都是由直线两侧的点互相连线组成的线段,进一步发现线段的权值和就是两侧的点权值和的乘积。有了前面的简化,我们可以对所有的点按照斜率进行排序,从最小斜率的点开始遍历计算,每次以过当前点的原点的直线为直线,那么我们需要计算两侧点权值和的乘积,那么复杂度是O(n*n)。我们可以优化:第一次以O(n) 遍历计算直线两侧的权值和,那么紧接着的下一次的直线划分的上下两侧只有上次的那个点不同,所以只需要O(1)的考虑上次直线上的那么点是应该加入上侧还是下侧。 所以这部分的做法是O(n) 的,但因为斜率排序是O(n*logn)的,所以整体的复杂度是
O(n*logn)的。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const LL N=5e4+;
const double INF=1e18;
struct Node{
LL x,y;
LL v;
double f;
}a[N]; void cal(Node& t)
{
LL x=t.x;
LL y=t.y;
if(x==) t.f=INF;
else{
t.f=(double)y*1.0/(double)x;
}
}
LL cmp(const Node s1,const Node s2)
{
return s1.f<s2.f;
}
bool check(Node a,Node b)
{
if(((a.x*b.y-a.y*b.x)*1.0/a.x)>=0.0)
return true;
return false;
} int main()
{
LL T; cin>>T;
while(T--)
{
LL n; scanf("%lld",&n);
LL tot=;
for(LL i=;i<=n;i++)
{
scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].v);
tot+=a[i].v;
cal(a[i]);
}
if(n==) { puts(""); continue; }
sort(a+,a+n+,cmp);
LL ans=,tmp1=,tmp2=;
tmp1=a[].v;
for(LL i=;i<=n;i++)
{
if(!check(a[],a[i]))
tmp1+=a[i].v;
}
tmp2=tot-tmp1;
ans=max(tmp1*tmp2,ans);
if(a[].x<) tmp1-=a[].v;
for(LL i=;i<=n;i++)
{
if(a[i].x>=) tmp1+=a[i].v;
tmp2=tot-tmp1;
ans=max(tmp1*tmp2,ans);
if(a[i].x<) tmp1-=a[i].v;
}
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. 20_学生选课数据库SQL语句练习题1
  2. IOS开发UI基础 UIDatePicker的属性
  3. 2016/7/30 div位置设置,表格大小统一
  4. How to install Wordpress 4.0 on CentOS 7.0
  5. MDI窗体 的再度思考
  6. Delphi与Qt在Windows下使用共享内存进程间通信
  7. css3学习--css3动画详解二(3d效果)
  8. 【hihocoder1255 Mysterious Antiques in Sackler Museum】构造 枚举
  9. 在线API大全
  10. java_XML_STAX
  11. JAVA自学笔记24
  12. 把源码放到服务器部署webservice调用出错 MVC C#
  13. 【CSS】clear清除浮动
  14. 01: socket模块
  15. 信息学奥赛(NOIP)复赛学习方法推荐
  16. DELPHI中的快捷方式一览(完全版)
  17. jquery实现拖拽进度条并显示百分比
  18. 使用InstallShield打包VS程序
  19. 2017.10.23 Java 面向对象深入学习---final 关键字、static关键字、匿名对象等
  20. Appium+python自动化7-输入中文【转载】

热门文章

  1. Python中import, from...import,import...as的区别
  2. 9. Palindrome Number (JAVA)
  3. Eclipse Android 模拟器启动过慢
  4. ltp 分析 fail testcase
  5. BigDecimal用法总结
  6. RobotFramework 模拟http接口登录自动化脚本
  7. linux就该这么学,第五课,
  8. ABP 权限拦截 第二篇
  9. 数据库-SQL语句:删除和修改语句-列类型-列约束
  10. numpy版本查看以及升降