题目链接:https://vjudge.net/contest/28079#problem/N

题目大意:给一个mxn的平面,有q个位置,每个位置坐标为(u,v)有w人,求一个点在平面内使得所有人都到这个点的曼哈顿距离之和最小(如 (x, y) 到 (p, q),那曼哈顿距离就是|x-p|+|y-q|)。

解题思路:分别按横纵坐标排序找出中位数即可(中位数请自行百度)。

代码:

 #include<cstdio>
#include<algorithm>
using namespace std;
const int N=5e5+; struct node{
int x,y,cnt;
}a[N]; bool cmp1(node a,node b){
return a.x<b.x;
} bool cmp2(node a,node b){
return a.y<b.y;
} int main(){
int T;
scanf("%d",&T);
int cas=;
while(T--){
int m,n,q,x,y;
scanf("%d%d%d",&m,&n,&q);
int xsum=,ysum=,count=;
for(int i=;i<=q;i++){
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].cnt);
count+=a[i].cnt;
}
int mid=(count+)/;
//按横坐标排序
sort(a+,a+q+,cmp1);
int sum=;
for(int i=;i<=q;i++){
sum+=a[i].cnt;
if(sum>=mid){
if(count&)
x=a[i].x;
else{
if(sum==mid)
x=(a[i].x+a[i+].x)/;
else
x=a[i].x;
}
break;
}
}
//按纵坐标排序
sort(a+,a+q+,cmp2);
sum=;
for(int i=;i<=q;i++){
sum+=a[i].cnt;
if(sum>=mid){
if(count&)
y=a[i].y;
else{
if(sum==mid)
y=(a[i].y+a[i+].y)/;
else
y=a[i].y;
} break;
}
}
printf("Case %d: %d %d\n",++cas,x,y);
}
}

LightOJ 1349 Aladdin and the Optimal Invitation(中位数)的更多相关文章

  1. LightOJ - 1349 - Aladdin and the Optimal Invitation

    链接: https://vjudge.net/problem/LightOJ-1349 题意: Finally Aladdin reached home, with the great magical ...

  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. jquery实现点击控制div的显示和隐藏

    我们使用点击显示.点击隐藏的时候,一般有两种可选方案 .示例 html <div class="index"> <h1> 首页 </h1> &l ...

  2. 【win7】安装php7.3及扩展

    php7.3插件php-redisphp-igbinaryphp-amqp [初识RabbitMQ,附RabbitMQ+PHP演示实例]https://www.cnblogs.com/miketwai ...

  3. Python——编译标准

    注意事项 1.关于包相关的导入语句也分为import和from ... import ...两种,但是无论哪种,无论在什么位置,在导入时都必须遵循一个原则:凡是在导入时带点的,点的左边都必须是一个包, ...

  4. docker的安装,升级,与删除(最新版)

    docker安装在ubuntu上 以前叫做 Docker engine安装现在叫做docker-ce的 第一种安装办法: root下执行,sudo su - root apt-get update - ...

  5. 02Lua入门

    前言: 语言学起来其实相似点很多,简单整理的知识点 目录: 1.使用控制台 2.Lua基础 3.变量 4.运算符 5.控制结构 1.使用控制台 Lua脚本是包含一系列Lua命令的简单脚本(扩展名为.l ...

  6. 洛谷P2634 聪明可可

    还是点分治 树上问题真有趣ovo,这道题统计模3为0的距离,可以把重心的子树分开统计,也可以一次性统计,然后容斥原理减掉重复的.. 其他的过程就是点分治的板子啦. #include <bits/ ...

  7. Quartus13.1全编译出现引脚错误(神级bug)

    BUG现象:分配完管脚后全编译出现如下错误. Error (171172):Detected confilicting assignments for the following nodes.Erro ...

  8. Flask 模板系统

    模板 基本数据类型 可以执行python语法,如:dict.get(), list['xx'] 比django 更加亲近于 python 传入函数 - django,自动执行 - flask,不自动执 ...

  9. Ubuntu和Windows双系统的安装

    本菜鸡的ACM生涯大概是结束了 最有希望的EC和焦作也顺利的铁了,一路走来还是怪自己不努力,整日整日的划水,算了,铁牌退役,也是自己应有的惩罚,静下心来吧 下面介绍如何装windows10和Ubunt ...

  10. GroupBox、TextBox、CheckBox、ToolStrip、RichTextBox、Timer控件

    GroupBox:划分窗体区域,内部可以拖放组件 TextBox:可编辑文本框,也可设置为只读 属性:ReadOnly(只读).PasswordChar(密码显示的符号,如*).Multiline(多 ...