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 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
随机推荐
- 求100之内的素质并输出(最优算法)-PHP面试题
曾经第一次面试题中的题目, 今天碰巧看到整理一下 当时用了最基本的算法写出来了, 两个for循环, 一个一个取余, 是质数就放进结果数组中 代码如下, 检查代码运行时间的代码是来对比三种不同算法的优劣 ...
- Python中的正则表达式教程
本文http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html 正则表达式经常被用到,而自己总是记不全,转载一份完整的以备不时之需. 1. ...
- Python——pickle模块(永久存储)
一.作用 讲字典.列表.字符串等对象进行持久化,存储到磁盘上,方便以后使用. 二.dump()方法 pickle.dump(对象,文件,[使用协议]) 作用:将要持久化的数据“对象”,保存到“文件中” ...
- 【题解】放球游戏A
题目描述 校园里在上活动课,Red和Blue两位小朋友在玩一种游戏,他俩在一排N个格子里,自左到右地轮流放小球,每个格子只能放一个小球.每个人一次只能放1至5个球,最后面对没有空格而不能放球的人为输. ...
- 指数型生成函数 及 多项式求ln
指数型生成函数 我们知道普通型生成函数解决的是组合问题,而指数型生成函数解决的是排列问题 对于数列\(\{a_n\}\),我们定义其指数型生成函数为 \[G(x) = a_0 + a_1x + a_2 ...
- JDK源码分析(7)String
String String表示字符串,Java中所有字符串的字面值都是String类的实例,例如"ABC".字符串是常量,在定义后不能被改变,字符串缓冲区支持可变的字符串.因为St ...
- 树&堆
树 什么是树? 大概像下面这样: 树的概念 树的每个点被称为节点: 连接的两个点,一个为父节点,一个为子节点,例如上图中,\(1\)是\(4\)的父节点,\(4\)是\(1\)的子节点: 没有父节点的 ...
- IIS运行一段时间不响应的解决方法
查找事件记录,提示为应用程序池 'DefaultAppPool' 提供服务的进程关闭时间超过了限制 解决方案: Internet 信息服务(IIS)管理器->应用程序池->DefaultA ...
- Js中常用知识点(typeof、instanceof、动态属性、变量作用域)
1.Js中各类型的常量表示形式:Number:number String:string Object:objec 2.typeof运算符在Js中的使用:用于判断某一对象是何种类型,返回值 ...
- ES6部分知识点总结
注:本文通过yck前端面试小册学习整理而得,记录下来供自己查阅 1.var 变量提升 使用var声明的变量,声明会被提升到作用域的顶部 举几个例子: eg1: console.log(a) // un ...