链接:

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. 在iis中部署网站出现的错误

    第一次错误: 解决的方案:点击网站新建一个同名的应用池,选择网站的基本设置,选中同名的应用池 接下来可能会有第二个错误 错误摘要HTTP 错误 500.21 - Internal Server Err ...

  2. Java 总结篇2

    第02章:数据类型和运算符 一.概述: 1.数据类型:int.float.char.boolean 2.运算符:算术运算符.赋值运算符.关系运算符.逻辑运算符.位运算符(了解即可).条件运算符 3.基 ...

  3. -Dmaven.test.skip=true 和 -DskipTests

    -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例,也不编译测试 ...

  4. python爬虫-《笔趣看》网小说《悟空看私聊》

    小编是个爱看小说的人,哈哈 # -*- coding:UTF-8 -*- ''' 类说明:下载<笔趣看>网小说<悟空看私聊> ''' from bs4 import Beaut ...

  5. vue3生命周期

    介绍 vue3的生命周期函数,可以按需导入到组件中,且只能在 setup() 函数中使用 示例 import { onMounted, onUpdated } from '@vue/compositi ...

  6. 记录:拷贝gitblit里的项目使用git命令clone、pull、push等,出现一直在加载,卡住没反应的问题

    俺想克隆别人gitblit里的其中一个版本库(俺在别人gitblit有权限) 懂得git的道友们,都应该知道克隆一个公共项目,随便找到,打开git终端,输入git clone 项目地址就行了 到了俺这 ...

  7. Spring Spring boot 获取IOC中的bean,ApplicationContext

    https://blog.csdn.net/weixin_38361347/article/details/89304414 https://www.jianshu.com/p/9ea13b00b1d ...

  8. Oracle---视图插参数

    1.创建一个参数Package create or replace package p_view_param is -- Author  : ALANN  -- Created : 2017/12/2 ...

  9. 文件属性的生成操作build action

    文件属性的生成操作build action Action 说明 None 资源既不会被集成到程序集内,也不会打包到xap包中.不过我们可以通过设置CopyToOutputDirectory选项让其自动 ...

  10. java List分组和排序处理

    在一些应用中,需要将List中的对象按某种情况分组或者排序处理.做个小结如下: 1. 如一个List中存放了ProductDoing对象,productDoing对象有rawTypeId 现在要求将r ...