Hard challenge

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1083    Accepted Submission(s): 444

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
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6132 6131 6130 6129 6128 
 
 
 
题目大意
给出n个点的坐标和权值val,任意两点之间都有线段连接,线段的权值是两端点权值的乘积(题目保证,没有两个点坐标相同,并且没有两个点的连线过原点)。现在要求过原点做一条直线,使直线穿过的所有线段的权值和最大。
题解:极角排序后扫描线
 
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
const int maxn=;
const double pi=acos(-1.0);
ll ans,sum,half;
int T,n; struct node
{
ll x,y,val;
double angle;
// bool operator <(const node &p)const//重载运算符 <
// {
// return angle<p.angle;
// }
//后面sort(a+1;a+1+n);
}a[maxn]; bool cmp(node a,node b)
{
return a.angle<b.angle;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
sum=; half=;
for(int i=;i<=n;i++)
{
scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].val);
if(a[i].x==) a[i].angle=pi/2.0;
else a[i].angle=atan(a[i].y*1.0/a[i].x);
sum+=a[i].val;
}
sort(a+,a++n,cmp); //角度排序,并非所说的极角排序
for(int i=;i<=n;i++)
if (a[i].x>=) half+=a[i].val;
ans=half*(sum-half);
for(int i=;i<=n;i++)
{
if (a[i].x>=) half-=a[i].val;
else half+=a[i].val;
ans=max(ans,half*(sum-half));
}
printf("%lld\n",ans);
}
return ;
}

stl函数:  atan & atan2

在C语言的math.h或C++中的cmath中有两个求反正切的函数atan(double x)与atan2(double y,double x)  他们返回的值是弧度 要转化为角度再自己处理下。

前者接受的是一个正切值(直线的斜率)得到夹角,但是由于正切的规律性本可以有两个角度的但它却只返回一个,因为atan的值域是从-90~90 也就是它只处理一四象限,所以一般不用它。

第二个atan2(double y,double x) 其中y代表已知点的Y坐标 同理x ,返回值是此点与远点连线与x轴正方向的夹角,这样它就可以处理四个象限的任意情况了,它的值域相应的也就是-180~180了

如:

例1:斜率是1的直线的夹角

cout<<atan(1.0)*180/PI;//45°

cout<<atan2(1.0,1.0)*180/PI;//45° 第一象限

cout<<atan2(-1.0,-1.0)*180/PI;//-135°第三象限

后两个斜率都是1 但是atan只能求出一个45°

极角排序:

利用arctan计算极角大小。(范围『-180,180』)

1 bool cmp(const Point& p1, const Point& p2)
2 {
3 return atan2(p1.y, p1.x) < atan2(p2.y, p2.x);
4 }

hdu 6127 Hard challenge(极角/角度排序+枚举+结构体排序新写法)的更多相关文章

  1. go语言的排序、结构体排序

    原文:https://studygolang.com/articles/1598 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜索 searc ...

  2. 2017多校第7场 HDU 6127 Hard challenge 极角排序,双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6127 题意:平面直角坐标系上有n个整点,第i个点有一个点权val​,坐标为(xi,yi),其中不存在任 ...

  3. HDU 6127 Hard challenge (极角扫描)

    题意:给定 n 个点,和权值,他们两两相连,每条边的权值就是他们两个点权值的乘积,任意两点之间的直线不经过原点,让你从原点划一条直线,使得经过的直线的权值和最大. 析:直接进行极角扫描,从水平,然后旋 ...

  4. HDU 1263 水果 结构体排序

    解题报告:一个结构体排序的题,用了一个运算符重载,要注意的是不同的地方可能会产相同的水果,一开始没注意. #include<cstdio> #include<cstring> ...

  5. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. 转载 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    转载自:http://www.cnblogs.com/cj695/p/3863142.html sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在 ...

  7. 【转】 从最简单的vector中sort用法到自定义比较函数comp后对结构体排序的sort算法

    sort函数在使用中非常好用,也非常简单,而且效率与冒泡或者选择排序不是一个数量级.本文就sort函数在vector中的用法分为sort函数入门用法与自定义comp比较函数比较结构体这两个最基本的功能 ...

  8. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  9. hdu1263 水果(结构体排序)

    Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样J ...

随机推荐

  1. 【查看】mysql 常规书写注意事项(那些坑)

    mysql 常规书写注意事项,mysql注意事项 1. 注释:  -- 后面一定要加一个空格,否则会报错 2.注释:/*! content */ 在mysql中是会执行的,但是其他数据库不会.   例 ...

  2. c/c++ json使用

    比如出名的有CJson,c++一般用jsoncpp http://sourceforge.net/projects/jsoncpp/ jsoncpp:http://www.cnblogs.com/fe ...

  3. visual studio多工程开发配置

    文章:带你玩转Visual Studio——带你多工程开发 带你玩转Visual Studio——带你理解微软的预编译头技术 通过上一篇文章带你玩转Visual Studio——带你多工程开发的讲解, ...

  4. Android中解析XML格式数据的方法

    XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...

  5. ng-深度学习-课程笔记-10: 机器学习策略2(Week2)

    1 误差分析( Carrying out error analysis ) 假设你训练了一个猫的二分类模型,在开发集上的错误率是10%,你想分析这10%的错误率来自哪里,怎么做呢? 先把这些错分的图片 ...

  6. 裸眼3D全攻略3:拍摄3D—瞳距、镜距、视角偏转与空间感

    http://sd89.blog.163.com/blog/static/356041322014112532958728/ 3D图片的拍摄,与平面有着全新的不同要求,那就是空间感的表现. 简单来说, ...

  7. HDU1503Advanced Fruits

    /*给出两串,求一个最小的字符串包含这两个子串,子串在这个字符串中的顺序不变, 做法:定义两个数组,分别标记公共部分在第一个串和第二个串中的位置,在输出是判断一下,输出一个串两个公共部分之间的部分,不 ...

  8. Linux 笔记 #04# Installing Tomcat 8 on Debian

    失败一 ※ 失败二  ※ 失败三 ※ 完 1- 确认机型: root@iZwz:~# lsb_release -a LSB Version: core-2.0-amd64:core-2.0-noarc ...

  9. pyDay10

    内容来自廖雪峰的官方网站. 1.python的赋值语句:a, b, c = x, y, z 相当于 a = x, b = y, c = z.(事实上等式右边是一个tuple) 2.获得genarato ...

  10. c++语言中的遍历

    原文地址:http://www.cnblogs.com/xylc/p/3653036.html 随着C++11标准的出现,C++标准添加了许多有用的特性,C++代码的写法也有比较多的变化. vecto ...