链接:

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. AX 2012 model应用

    说句实话,AX2012 model真心不好用,当你开发好,把Model到入到客户环境时, 要防止有冲突,假如出现冲突的话,还必须去删除另外一个Model里面冲突的代码,真心麻烦. 下面给一个ax 导入 ...

  2. 在Firefox中操作iframe的一个小问题

    在做一个 Web 的打印功能时,需要将被打印的文档写到 iframe 的 document 中. <!doctype html> <html lang="en"& ...

  3. quartz2.3.0(七)调度器中断任务执行,手动处理任务中断事件

    job任务类 package org.quartz.examples.example7; import java.util.Date; import org.slf4j.Logger; import ...

  4. java之struts2的action优化配置

    当一个Action处理类中处理多个业务时,action的配置 文件将会急剧增加,导致配置文件很臃肿的问题. struts2提供了两种方案来解决这个问题.一种是动态方法调用,另一种是使用通配符来配置Ac ...

  5. ADO.NET 七(一个例子)

    通过一个完整的实例实现课程信息管理功能的操作,包括查询.修改.删除课程信息等操作. 1) 创建课程信息表 create table StuCourse ( id int primary key ide ...

  6. 分享linux系统more基本命令python源码

    此python源码是linux系统more基本命令的实现. 实现linux中more的基本功能,当more后加一个文件名参数时候,分屏显示按空格换页,按回车换行',在左下角显示百分比; 以处理管道参数 ...

  7. Swift枚举的全用法

    鉴于昨天开会部门会议讨论的时候,发现有些朋友对枚举的用法还是存在一些疑问,所以就写下这个文章,介绍下Swift下的枚举的用法. 基本的枚举类型 来,二话不说,我们先贴一个最基本的枚举: enum Mo ...

  8. 【新手可看懂】ubuntu配置appium环境

    1.node安装: 在node官网:https://nodejs.org/en/download/ 下载对应的安装包(这里建议下载最新的版本)下载好后放在liunx指定路径下 我这里放到opt这个文件 ...

  9. 写Shell脚本自动生成首行

    送给经常写shell脚本的兄弟们常写shell脚本的时候,大家一定都有困扰,怎么样能让.sh文件的表头自己生成,不用我们自己去敲呢 首先我们要编写一下/etc/vimrc执行 vim /etc/vim ...

  10. springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5

    近期在整合springboot + redis 的功能,本来想用原生的jedit api,最后想想有点 low,搜了一把,boot已经提供给我们操作的方法,那就是 使用 redisTemplate 或 ...