链接:

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. WIP*更新生产批详细信息行产品配料

    DECLARE l_batch_header_rec gme_batch_header%ROWTYPE; l_material_detail_rec gme_material_details%ROWT ...

  2. golang程序因未知错误崩溃时如何记录异常

    开发服务器程序时如果未经过充分测试, 服务稳定运行一段时间后会突然崩溃退出.一般是因为程序中出现了某个未捕获的异常. 这类问题属于偶现的,且需要服务器运行一段时间之后才会出现,难以定位有问题的代码段. ...

  3. AS3.0频谱-01

    AS3.0频谱系列-01: package fengzi.spectrum { //import fengzi.colors.GetColor; import flash.display.Sprite ...

  4. python 获取mysql数据库列表以及用户权限

    一.需求分析 需要统计出当前数据库的所有数据库名,以及每个用户的授权信息. 获取所有数据库 在mysql里面,使用命令: show databases 就可以获取所有数据库了 获取所有用户 执行命令: ...

  5. 方法2:使用Jenkins构建Docker镜像 --SpringCloud

    前提意义: SpringCloud微服务里包含多个文件夹,拉取仓库的所有代码,然后过根据选项参数使用maven编译打包指定目录的jar,然后再根据这个目录的Dockerfile文件制作Docker镜像 ...

  6. Docker安装带中文全文搜索插件zhparser的Postgresql数据库

    上一篇讲了在已经安装了PG数据库的情况下,安装全文搜索插件zhparser遇到的问题.在一个全新的环境中安装带有全文搜索插件zhparser的PG数据库,可以使用已经做好的Docker镜像,在安装的过 ...

  7. java之hibernate之多对多双向关联映射

    1.比如在权限管理中,角色和权限之间的关系就是多对多的关系,表结构为: 2.类结构 Role.java public class Role implements Serializable{ priva ...

  8. 2019年北航OO第三次博客总结

    一.JML语言理论基础及其工具链 1. JML语言理论基础 JML是用于对Java程序进行规格化设计的一种表示语言,是一种行为接口规格语言(Behavior Interface Specificati ...

  9. 原子性atomic/nonatomic

    原子性:并发编程中确保其操作具备整体性,系统其它部分无法观察到中间步骤,只能看到操作前后的结果. 决定编译器生成的getter和setter是否原子(natomic)操作.   i 因此,atomic ...

  10. win10开启redis失败解决方案

    输入命令:redis-server redis.windows.conf 提示:解决redis无法启动,报错:无法将“redis-server”项识别为 cmdlet.函数.脚本文件或可运行程 序的名 ...