• LightOJ


    链接:

    https://vjudge.net/problem/LightOJ-1349

    题意:

    Finally Aladdin reached home, with the great magical lamp. He was happier than ever. As he was a nice boy, he wanted to share the happiness with all people in the town. So, he wanted to invite all people in town in some place such that they can meet there easily. As Aladdin became really wealthy, so, number of people was not an issue. Here you are given a similar problem.

    Assume that the town can be modeled as an m x n 2D grid. People live in the cells. Aladdin wants to select a cell such that all people can gather here with optimal overall cost. Here, cost for a person is the distance he has to travel to reach the selected cell. If a person lives in cell (x, y) and he wants to go to cell (p, q), then the cost is |x-p|+|y-q|. So, distance between (5, 2) and (1, 3) is |5-1|+|2-3| which is 5. And the overall cost is the summation of costs for all people.

    So, you are given the information of the town and the people, your task to report a cell which should be selected by Aladdin as the gathering point and the overall cost should be as low as possible.

    思路:

    二维坐标取中点,直接中位数,或者暴力计算每个位置的花费计算一个最小值。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    #include<map>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 5e4+10;
    const int MOD = 1e9+7;
    
    struct Node
    {
        int p, n;
        bool operator < (const Node &rhs) const
        {
            return this->p < rhs.p;
        }
    }nodex[MAXN], nodey[MAXN];
    int Numx[MAXN], Numy[MAXN];
    int n, m, q;
    
    int Cal(int num, int Num[], int bor)
    {
        int tmp = 0;
        for (int i = 1;i <= bor;i++)
        {
            tmp += Num[i];
            if (tmp >= (num+1)/2)
                return i;
        }
        return 0;
    }
    
    int main()
    {
        int t, cnt = 0;
        scanf("%d", &t);
        while(t--)
        {
            printf("Case %d:", ++cnt);
            memset(Numx, 0, sizeof(Numx));
            memset(Numy, 0, sizeof(Numy));
            scanf("%d%d%d", &n, &m, &q);
            int sum = 0;
            int u, v, w;
            for (int i = 1;i <= q;i++)
            {
                scanf("%d%d%d", &u, &v, &w);
                nodex[i].p = u, nodex[i].n = w;
                nodey[i].p = v, nodey[i].n = w;
                Numx[u] += w;
                Numy[v] += w;
                sum += w;
            }
            sort(nodex+1, nodex+1+q);
            sort(nodey+1, nodey+1+q);
            int rx = Cal(sum, Numx, n);
            int ry = Cal(sum, Numy, m);
            printf(" %d %d
    ", rx, ry);
        }
    
        return 0;
    }
    
  • 相关阅读:
    qt QTimer 计时器
    qt DateTime 计算时间
    c++ win32 关机 注销 重启
    uniapp 修改meta:viewport
    初次使用 VUX
    移动端web app自适应布局探索与总结
    前端js模版 预编译工具Tmod js使用入门
    谷歌 Uncaught SecurityError: Failed to execute 'replaceState' on 'History 错误
    H5 前端页面适配响应式
    微信video标签全屏无法退出bug 本文系转载
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11841456.html
Copyright © 2020-2023  润新知