hihocoder-1812-圆的最大权值

hihocoder-1812-圆的最大权值

#1812 : 圆的最大权值

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

平面上有N个点,其中第i个点的坐标是(Xi, Yi),并且具有权值Wi。  

现在你可以以原点为圆心,任意非负实数半径画一个圆。我们将圆内和圆上所有点的权值和记作这个圆的权值。

请你计算如何画圆可以使权值最大,并输出最大的权值。

输入

第一行包含一个整数N。  

以下N行每行三个整数Xi,Yi和Wi。  

1 <= N <= 100000  

0 <= Xi, Yi <= 1000000  

-1000000 <= Wi <= 1000000

输出

一个整数表示答案

样例输入
3  
0 1 1  
1 0 1  
1 1 -1
样例输出
2

题解:

  这个题表面上很直观简单,但是内在的坑挺多的,

  1, 注意数字的范围,使用long long 

  2, 注意圆的原点(0, 0), 和 相等距离的点。 

#include <cstdio> 
// #include <cstring>  
#include <cstdlib>
 

const int MAXN = 100000 + 10; 

struct Node
{
    long long dist; 
    long long w; 
}; 

int N; 
Node node[MAXN]; 

int cmp(const void *a, const void *b)
{
    Node *aa = (Node *)a; 
    Node *bb = (Node *)b; 
    if(aa->dist > bb->dist){
        return 1; 
    }else{
        return -1; 
    }
}

int main(){ 
    freopen("in.txt", "r", stdin);  

    long long ans, cnt, x, y, w;  
    scanf("%d", &N);  
    ans = 0; 
    for(int i=0; i<N; ++i)
    {
        scanf("%lld %lld %lld", &x, &y, &w); 
        if(x == 0 && y == 0)
        {
            ans += w; 
        }
        node[i].dist = x*x + y*y; 
        node[i].w = w; 
    } 
    qsort(node, N, sizeof(Node), cmp); 
    cnt = 0; 
    for(int i=0; i<N; ++i)
    {
        cnt += node[i].w; 
        if(i+1<N && node[i].dist == node[i+1].dist){
            continue; 
        } 
        if(cnt > ans)
        {
            ans = cnt; 
        }  
    }  
    printf("%lld\n", ans);  
    return 0; 
} 

  

猜你喜欢

转载自www.cnblogs.com/zhang-yd/p/9615423.html
今日推荐