poj 1723 SOLDIERS 带权中位数
题目
http://poj.org/problem?id=1723
题解
带权中位数类型的题目~
可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y。比较麻烦的是在x坐标上最后是要列成一排,而不是单独的一个点,我们可以假设最后集合的最左边的点是x,采用贪心的策略列出公式:res_x=sum(abs(xi-x-i))(i belongs to [0,n-1])。令zi=xi-i,就转化为res_x=sum(abs(zi-x)),这相当于求zi数列的中位数(因为我们没有把重叠的点合并,所以不是带权了,直接求中位数就好,一样的意思)。
代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=10010;
pair<int,int> pt[maxn];
bool cmp(const pair<int,int>& a,const pair<int,int>& b){
return a.second<b.second;
}
int n;
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d%d",&pt[i].first,&pt[i].second);
}
sort(pt,pt+n,cmp);
int res=0;
for(int i=0;i<n;i++){
res+=abs(pt[i].second-pt[n/2].second);
}
sort(pt,pt+n);
for(int i=0;i<n;i++){
pt[i].first=pt[i].first-i;
}
sort(pt,pt+n);
for(int i=0;i<n;i++){
res+=abs(pt[i].first-pt[n/2].first);
}
printf("%d\n",res);
}
return 0;
}
poj 1723 SOLDIERS 带权中位数的更多相关文章
- 2019.03.28 bzoj3598: [Scoi2014]方伯伯的商场之旅(带权中位数+数位dp)
传送门 题意咕咕咕自己读吧挺简单的 思路: 由带权中位数的性质可以得到对于每个数放在每个二进制位的代价一定是个单调或者单峰函数,因此我们先把所有的数都挪到第一个位置,然后依次向右枚举峰点(极值点)把能 ...
- POJ 1723 SOLDIERS (中位数)
题目大意: 平面上有N(N<=10000)个点,求这些点变成一条水平线的最小移动步数. 算法讨论: 表示自己太弱弱了,打算从今天开始提高一下智商. 我们考虑,既然是要成一条水平线,那么这条直线的 ...
- K - Find them, Catch them POJ - 1703 (带权并查集)
题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...
- A Bug's Life POJ - 2492 (带权并查集)
A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...
- POJ - 2912 Rochambeau (带权并查集+枚举)
题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...
- poj 1723 Soldiers【中位数】By cellur925
题目传送门 题目大意:平面上有n个士兵,给出每个士兵的坐标,求出使这些士兵站好所需要的最少移动步数.站好要求:所有士兵y相等,x相邻.即达到 (x,y), (x+1, y), (x+2,y)……的状态 ...
- OpenJudge/Poj 1723 SOLDIERS
1.链接地址: http://bailian.openjudge.cn/practice/1723/ http://poj.org/problem?id=1723 2.题目: 总时间限制: 1000m ...
- POJ 1723 SOLDIERS
SOLDIERS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...
- POJ 2631 DFS+带权无向图最长路径
http://poj.org/problem?id=2631 2333水题, 有一个小技巧是说随便找一个点作为起点, 找到这个点的最远点, 以这个最远点为起点, 再次找到的最远点就是这个图的最远点 证 ...
随机推荐
- matplotlib numpy scipy 的安装
一:windows 端的安装 #cmd指令 python -m pip install --user numpy scipy matplotlib ipython jupyter pandas sym ...
- leetcode 5. Longest Palindromic Substring [java]
public String longestPalindrome(String s) { String rs = ""; int res = 0; for(int i = 0; i& ...
- bootstrap-datepicker default value
$('.selectDate').datepicker({ format : "yyyy/mm/dd", autoclose : true, startDate : new Dat ...
- 简明 Vim 练级攻略------转自陈皓coolshell
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- swift函数的调用约定
The convention of the function, indicated by the attribute. This is similar to the language-level @c ...
- flare-spork: 自己维护的Pig on Spark项目
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zbf8441372/article/details/24726501 关于flare-spork 非 ...
- IOS - 执行时 (经常使用函数)
能够通过NSObject的一些方法获取运行时信息或动态运行一些消息: /*Returns a Boolean value that indicates whether the receiving cl ...
- nested exception is org.apache.ibatis.reflection.ReflectionExceptio
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.Reflecti ...
- springboot+mybatis+springmvc整合实例
以往的ssm框架整合通常有两种形式,一种是xml形式,一种是注解形式,不管是xml还是注解,基本都会有一大堆xml标签配置,其中有很多重复性的.springboot带给我们的恰恰是“零配置”,&quo ...
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...