LightOJ - 1349 - Aladdin and the Optimal Invitation
链接:
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的更多相关文章
- LightOJ 1349 Aladdin and the Optimal Invitation(中位数)
题目链接:https://vjudge.net/contest/28079#problem/N 题目大意:给一个mxn的平面,有q个位置,每个位置坐标为(u,v)有w人,求一个点在平面内使得所有人都到 ...
- 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 题意:给你地毯面积和最小可能边的长度,让你求有几种组合的可能. 题解:这题就厉害 ...
随机推荐
- vue通过ajax加载json数据
HTML <ul id="Hanapp"> <li class="styVue" v-for="item in actList&qu ...
- Python各个岗位的开发流程
根据张大美女提供资料微修改,在这谢谢张大美女! 1.python软件开发工程师 1.1 项目启动会 说明项目目标.阶段划分.组织结构.管理流程等关键事项. 1.2 需求调研 由用户提出,描述产品的功 ...
- flink ETL数据处理
Flink ETL 实现数据清洗 一:需求(针对算法产生的日志数据进行清洗拆分) 1. 算法产生的日志数据是嵌套json格式,需要拆分 2.针对算法中的国家字段进行大区转换 3.最后把不同类型的日 ...
- android使用http3
http3的github地址: https://github.com/cloudflare/quiche
- MongoDB 增删改查 Shell使用及操作
下载链接:https://robomongo.org/download 安装步骤省略,下一步下一步... 图形界面,连接默认,取个名字就行. 连接成功,可以愉快的使用了,不用总是敲命令了,简洁方便,多 ...
- 利用脚本一键执行脚本,创建SharePoint文档库列表
SharePoint基于文档库和列表上进行二次开发,生成新的文档库和新的列表模板 通过新的模板,创建新的文档库与列表 --定义site对象$site = SPSite http://dvt176/si ...
- Linux REDHAT 7 关闭、禁用防火墙服务
1 查看防火墙状态 [root@lvxinghao ~]# systemctl status firewalld 2 查看开机是否启动防火墙服务[root@lvxinghao ~]# systemct ...
- C语言的三套标准 C89(C90)、C99、C11
C语言最初由 Dennis Ritchie 于 1969 年到 1973 年在 AT&T 贝尔实验室里开发出来,主要用于重新实现 Unix 操作系统.此时,C语言又被称为 K&R C. ...
- IDEA设置项目文件自动Add到Svn/Git
1)配置自动Add 2)将未添加的文件添加到本地 3)取消已经添加的文件
- Android笔记(六十四) android中的动画——补间动画(tweened animation)
补间动画就是只需要定义动画开始和结束的位置,动画中间的变化由系统去补齐. 补间动画由一下四种方式: 1.AplhaAnimation——透明度动画效果 2.ScaleAnimation ——缩放动画效 ...