LightOJ 1349 Aladdin and the Optimal Invitation(中位数)
题目链接: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(中位数)的更多相关文章
- LightOJ - 1349 - Aladdin and the Optimal Invitation
链接: https://vjudge.net/problem/LightOJ-1349 题意: Finally Aladdin reached home, with the great magical ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- LightOJ 1348 Aladdin and the Return Journey
Aladdin and the Return Journey Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged ...
- [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
题目链接: https://vjudge.net/problem/LightOJ-1341 题目描述: 问有几种边长为整数的矩形面积等于a,且矩形的短边不小于b 算数基本定理的知识点:https:// ...
- LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. 思路 ...
- LightOJ 1341 - Aladdin and the Flying Carpet 基本因子分解
http://www.lightoj.com/volume_showproblem.php?problem=1341 题意:给你长方形的面积a,边最小为b,问有几种情况. 思路:对a进行素因子分解,再 ...
- 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 ...
- 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 ...
- LightOJ 1341 - Aladdin and the Flying Carpet
题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
随机推荐
- hMailServer配置图文详细教程
https://www.hmailserver.org/viewtopic.php?f=4&t=6
- 酷炫的loading
今天分享一下,怎么通过用css写出一个酷炫的loading. meta: <meta name="viewport" content="width=device-w ...
- mysql-python安装时EnvironmentError: mysql_config not found
mysql-python安装时EnvironmentError: mysql_config not found 在安装 mysql-python时,会出现: 复制代码 sh: mysql_config ...
- Time travel HDU - 4418(高斯消元)
Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to ...
- RBAC权限管理系统
RBAC--基于角色的权限管理系统 优势: 1. 简化了用户和权限的关系 2. 易扩展,易于维护 3. RBAC不用给用户单个分配权限,只用指向对应的角色就会有对应的权限,而且分配权限和收回权限都很方 ...
- 第四十篇-private,public,protected的区别
1.public: public表明该数据成员.成员函数是对所有用户开放的,所有用户都可以直接进行调用 2.private: private表示私有,私有的意思就是除了class自己之外,任何人都不可 ...
- python 学习地址
本章主要记录学习python过程中借鉴的一些网站,并感谢这些博主辛勤付出. python官网:https://www.python.org/ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...
- 1.2浅谈Spring-Spring结构
时隔很多天的我又回来....最近发展了一下自己的爱好,所以拖了很长时间. 前面我们从概念性上分析了spring的特性 这里我们附上Spring框架的结构图 我们简单的来说一些这个框架图 我们从下往上看 ...
- Hadoop记录-JMX参数
Yarn metrics参数说明 获取Yarn jmx信息:curl -i http://xxx:8088/jmx Hadoop:service=ResourceManager,name=FSOpDu ...
- Vim使用技巧:撤销与恢复撤销
在使用VIM的时候,难免会有输错的情况,这个时候我们应该如何撤销,然后回到输错之前的状态呢?答案:使用u(小写,且在命令模式下)命令. 但如果有时我们一不小心在命令模式下输入了u,然后刚输入的一大片代 ...