链接:

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\n", rx, ry);
} return 0;
}

LightOJ - 1349 - Aladdin and the Optimal Invitation的更多相关文章

  1. LightOJ 1349 Aladdin and the Optimal Invitation(中位数)

    题目链接:https://vjudge.net/contest/28079#problem/N 题目大意:给一个mxn的平面,有q个位置,每个位置坐标为(u,v)有w人,求一个点在平面内使得所有人都到 ...

  2. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  3. LightOJ 1348 Aladdin and the Return Journey

    Aladdin and the Return Journey Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged ...

  4. [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))

    题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...

  5. LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)

    http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...

  6. LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解

    http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...

  7. Lightoj 1348 Aladdin and the Return Journey (树链剖分)(线段树单点修改区间求和)

    Finally the Great Magical Lamp was in Aladdin's hand. Now he wanted to return home. But he didn't wa ...

  8. LightOJ 1344 Aladdin and the Game of Bracelets

    It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a ...

  9. LightOJ 1341 - Aladdin and the Flying Carpet

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...

随机推荐

  1. 使用nohup命令编写shell脚本启动java应用,并使用外挂配置文件

    nohup nohup命令:如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. 在缺省情况下该作业的所 ...

  2. leetcode整数反转

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123输出: 321 示例 2: 输入: -123输出: -321 示例 3: 输入: 120输出: 21 ...

  3. backpropagation algorithm

    搞卷积神经网络的时候突然发现自己不清楚神经网络怎么训练了,满脸黑线,借此机会复习一下把. 首先放一位知乎大佬的解释.https://www.zhihu.com/question/27239198?rf ...

  4. 一张图看懂SharpImage

    通过下面的图片,可以瞬间看懂整个类库的脉络.图片比较大,如果看不清,可

  5. 把时间戳转换为 datatime 格式

    使用time timeStamp = 1381419600 timeArray = time.localtime(timeStamp) otherStyleTime = time.strftime(& ...

  6. 微服务与SpringCloud简介

    A.官网 https://spring.io/projects/spring-cloud B.简介 Spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用 ...

  7. linux 下 shell脚本报错:-bash: ./build.sh: /bin/sh^M: bad interpreter: No such file or directory

    主要原因是build.sh是在windows下编辑然后上传到linux系统里执行的..sh文件的格式为dos格式.而linux只能执行格式为unix格式的脚本. 我们可以通过vi编辑器来查看文件的fo ...

  8. 刚接触HTML5应该先学哪里才好?

    好吧,话不多说,直接来点干货吧! 刚接触html的小白都感觉摸不着头脑?应该怎么学习呢,其实HTML5可能对于还没有接触过的小白来说会比较的难,听起来也比较新颖.这是个什么骚东西!其实不然,这个就是构 ...

  9. javascript 数组排序原理的简单理解

    js内置的Array函数原型对象有个sort方法,这个方法能按照顺序排序数组. 例如: var arr1 = [6, 4, 2, 5, 2]; arr1.sort((x, y) => x - y ...

  10. scrapy 用pycharm调试

    1. 用pycharm打开scrapy项目,随便右击一个.py文件,选择Debug "***" 2. pycharm 右上角点击刚才debug的文件,选择Edit Configur ...