题目链接

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+5;
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==0) 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=0;for(LL i=1;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==1) { puts("0"); continue; }sort(a+1,a+n+1,cmp);LL ans=0,tmp1=0,tmp2=0;tmp1=a[1].v;for(LL i=2;i<=n;i++){if(!check(a[1],a[i]))tmp1+=a[i].v;}tmp2=tot-tmp1;ans=max(tmp1*tmp2,ans);if(a[1].x<0) tmp1-=a[1].v;for(LL i=2;i<=n;i++){if(a[i].x>=0) tmp1+=a[i].v;tmp2=tot-tmp1;ans=max(tmp1*tmp2,ans);if(a[i].x<0) tmp1-=a[i].v;}printf("%lld\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/chen9510/p/7367878.html

hdu 6127---Hard challenge(思维)相关推荐

  1. HDU 6127 Hard challenge(极角 + 二分)

    Hard challenge 思路 通过极角排序,这里通过修改后,所有点的角度在[0,2π)[0, 2 \pi)[0,2π)之间, 然后O(n)O(n)O(n)扫一趟,对当前在的级角加上π\piπ就是 ...

  2. HDU 6127 Hard challenge

    题目链接 题目意思 平面上有(n)个点,已知每个点的坐标为((x,y)),以及该点的权值为(val),任意两点之间可以连接成一条不经过原点的边,该边的权值是两个端点的权值的乘积.现在要求画一条经过原点 ...

  3. HDU - 6486 Flower(思维)

    HDU - 6486 Flower 题目大意:有n堆草每次只能对n-1堆操作每次只能减1问最少操作几次能把这些草剪到相同高度如果不能输出-1 我们让n-1个数减1实际上可以看成使得剩下的那一个数加1. ...

  4. HDU - 5637 Transform (思维、bfs预处理)

    HDU - 5637 题目大意: 给出n个数的序列a,对于一个整数x,有两种操作: 1.改变x二进制中任一位 2.将x变为x^a[i] m次查询,每次查询输入两个整数x和y,问x最少经过多少次操作可以 ...

  5. HDU - 6438(贪心+思维)

    链接:HDU - 6438 题意:给出 n ,表示 n 天.给出 n 个数,a[i] 表示第 i 天,物品的价格是多少.每天可以选择买一个物品,或者卖一个已有物品,也可以什么都不做,问最后最大能赚多少 ...

  6. Hdu 5339 Untitled (数学思维)

    题意:给一个数a和n个数b1,b2,...,bn. 从n个数中选择一些数重新排列成c1,c2,...,cm使得a%c1%c2%...%cm=0. 如果能选出则输出最少需要几个数,否则输出-1. 分析: ...

  7. HDU 6047 Maximum Sequence 思维

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目描述: 有数组a, b长度都为n ......解释起来好麻烦, 自己看题吧 解题思路: 由 ...

  8. hdu 6400 Parentheses Matrix思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6400 这题有坑.. hw的最小值小于等于4是一种情况,否则是另外一种情况. 第一种情况可以 ((((( ...

  9. hdu 6851 Vacation(思维+贪心)

    传送门 •题意 有编号0到n,n+1辆车排队过红绿灯,从0到n离交通灯线越来越近 每辆车都有一个最大速度v,车身长度l,和离交通灯线的距离s, 一辆车头到达线则说明这辆车已到达线 如果一辆车前面没有紧 ...

  10. HDU - 6184 Counting Stars(思维+三元环)

    题目链接:点击查看 题目大意:给出一个 nnn 个点 mmm 条边组成的无向图,问图中有多少个"三元环对","三元环对"指的是两个三元环共用了一条边 题目分析: ...

最新文章

  1. 导航能力堪比GPS!动物们是这样做到的
  2. php导入excel表格数据,php页面导入excel表格数据-php导入excel 怎么获取excel表格数据...
  3. 【小菜日志】用C#完成Allen Lee's Magic大虾推荐的F#作业F#学习中
  4. 【绝对有用】Syntax error on token “throws“, @ expected after this token
  5. koa --- 扩展hbs方法
  6. tomcat限制用域名访问 禁止 ip访问
  7. python文件编译_编译Python文件
  8. 微信独立精彩互换抢红包系统源码ThinkPHP开源版
  9. buck电路 dac stm32_STM32定时器学习---基本定时器
  10. Windows 7 切换Python版本
  11. java 算数运算符
  12. 苹果Mac如何限制进程 CPU 资源占用?
  13. 配置百度云CDN加速
  14. 10款开源网上教学课程管理系统
  15. 最受欢迎的9个前端UI框架
  16. Blender建模模块:超实用插件LoopTools
  17. win7打印机服务器修改ip,win7系统电脑更换IP地址后打印机不能打印文件了的解决方法...
  18. 剑指Offe6-反转链表
  19. 视频讲解Agora视频通话SDK| 掘金技术征文
  20. 新手如何学习学嵌入式开发?

热门文章

  1. seci-log1.02日志分析软件版本升级了
  2. tomcat 域名的配置
  3. C#设计模式系列:享元模式(Flyweight)
  4. 计算机及其系统的泄密渠道之三
  5. 3 tables in management a company
  6. powerbi learning: look up table and data table
  7. 空场景在安卓上的渲染消耗问题
  8. NLP的一些学习资料
  9. redis和memcache的高可用的探索
  10. python 学习源