码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 6127 Hard challenge (极角扫描)

时间:2017-08-15 19:45:23      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:++   txt   opera   log   amp   cos   math   operator   print   

题意:给定 n 个点,和权值,他们两两相连,每条边的权值就是他们两个点权值的乘积,任意两点之间的直线不经过原点,让你从原点划一条直线,使得经过的直线的权值和最大。

析:直接进行极角扫描,从水平,然后旋转180度,就可以计算出一个最大值,因为题目说了任意直线不是经过原点的,所以就简单了很多,每次碰到的肯定是一个点,而不是多个点。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}

struct Node{
  int x, y, val;
  double v;
};
Node a[maxn];

struct Point{
  double v;
  int id;
  Point(){ }
  Point(double vv, int i) : v(vv), id(i) { }
  bool operator < (const Point &p) const{
    return v < p.v;
  }
};

vector<Point> v;

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d", &n);
    v.clear();
    LL up = 0, down = 0;
    for(int i = 1; i <= n; ++i){
      scanf("%d %d %d", &a[i].x, &a[i].y, &a[i].val);
      if(a[i].y < 0)  down += a[i].val;
      else if(a[i].y > 0)  up += a[i].val;
      else if(a[i].x < 0)  up += a[i].val;
      else down += a[i].val;
      a[i].v = atan2(a[i].y, a[i].x);
      if(a[i].v < 0.0)  v.push_back(Point(a[i].v + PI, i));
      else v.push_back(Point(a[i].v, i));
    }
    v.push_back(Point(0, 0));
    sort(v.begin(), v.end());
    LL ans = up * down;

    for(int i = 1; i < v.size(); ++i){
      if(v[i].v == v[i-1].v)  continue;
      if(a[v[i].id].v >= 0.0){
        up -= a[v[i].id].val;
        down += a[v[i].id].val;
      }
      else {
        up += a[v[i].id].val;
        down -= a[v[i].id].val;
      }
      ans = max(ans, up * down);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

  

 

HDU 6127 Hard challenge (极角扫描)

标签:++   txt   opera   log   amp   cos   math   operator   print   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7366883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!