题目链接: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. HttpPost方式调用接口的3种方式

    第一种:需要httpclient的依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> < ...

  2. JS流程控制

    1.if...else... //if 语句:只有当指定条件为 true 时,该语句才会执行代码. //语法 if (condition) { 当条件为 true 时执行的代码 } //if...el ...

  3. 洛谷P1262间谍网络

    题目 我们首先考虑该题没有环应该怎么做,因为没有环所以是一个DAG,因此直接加上入度为0的罪犯,而有环则可以缩点,之后就成为了DAG,然后用一方法做就好了. \(Code\) #include < ...

  4. re 模块 分组特别说明

    关于分组优先以及 " | " 的细致练习 from django.test import TestCase import re # Create your tests here. ...

  5. Kafka遇到30042ms has passed since batch creation plus linger time at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:94)

    问题描述: 运行生产者线程的时候显示如下错误信息: Expiring 1 record(s) for XXX-0: 30042 ms has passed since batch creation p ...

  6. linux下串口函数

    tcgetattr(), tcsetattr(), tcdrain(),tcflush(), tcflow(), tcsendbreak(),cfmakeraw(), cfgetispeed(),cf ...

  7. 主机管理+堡垒机系统开发:strace命令用法详解(六)

    一.简单介绍 strace是什么? 按照strace官网的描述, strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进 ...

  8. Sobel 边缘检测算子

    转自:http://blog.csdn.net/xiaqunfeng123/article/details/17302003 Sobel 算子是一个离散微分算子 (discrete different ...

  9. 跨站请求伪造 | ajax

    一.简介 django为用户实现防止跨站请求伪造的功能,通过中间件 django.middleware.csrf.CsrfViewMiddleware 来完成.而对于django中设置防跨站请求伪造功 ...

  10. c#根据文件路径启动进程

    //根据文件路径启动进程 private static void StartProcessByFilePath(string path) { Process p = new System.Diagno ...