Description

继在鹅厂工作的DJ训练完鹅厂的企鹅们之后,DJ发明了一个新游戏。该游戏在nxn的棋盘上进行,其中恰好有n个企鹅,企鹅向四个方向之一移动一格算作一步。DJ希望用最少的总步数把这些企鹅变成一排,即所有企鹅都在同一行或同一列(由于DJ目不斜视,他认为所有企鹅在同一对角线上的情况不算一列)。 企鹅们为了避免DJ的毒打需要尽快排成一排,你能帮可怜的小企鹅们解决这个问题吗。注意任意时刻不能有两个企鹅在同一格。

Input

第一行一个整数n(n<=6*10^5)表明棋盘大小。 接下来n行,每行两个整数Xi,Yi(1<=Xi,Yi<=n),表示第i只企鹅的坐标。保证初始 企鹅的坐标互不相同。

Output

一行一个整数表示最少需要的步数。

Sample Input

5
1 2
2 4
3 4
5 1
5 3

Sample Output

6

思路:
先放在同一列,或同一行,然后散开。
放在同一列时,位置选x坐标的中位数是最优解。
收到前期思路的影响,我的solve应该是写复杂了。
 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int n;
int x[maxn],y[maxn];
ll sumx[maxn],sumy[maxn];
ll numx[maxn],numy[maxn];
ll xx,yy,ansx,ansy;
void solve(){
ll sum=numx[n];
for(int i=;i<=n;i++){
if(numx[i]>sum/){
xx=i;
break;
}
} sum=numy[n];
for(int i=;i<=n;i++){
if(numy[i]>sum/){
yy=i;
break;
}
} // fuck(xx)
ansx=numx[xx-]*xx-sumx[xx-];
// fuck(ansx)
ansx+=(sumx[n]-sumx[xx])-(numx[n]-numx[xx])*xx;
// fuck(sumx[n]-sumx[xx]) ansy=numy[yy-]*yy-sumy[yy-];
ansy+=(sumy[n]-sumy[yy])-(numy[n]-numy[yy])*yy;
// fuck(yy) } int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
sort(x+,x+n+);
sort(y+,y+n+); int tot=;
for(int i=;i<=n;i++){
sumx[i]=sumx[i-];
numx[i]=numx[i-];
while (x[tot]==i){
sumx[i]+=i;
numx[i]++;
tot++;
}
} // for(int i=1;i<=n;i++){
// cout<<sumx[i]<<" ";
// }
// cout<<endl; tot=;
for(int i=;i<=n;i++){
sumy[i]=sumy[i-];
numy[i]=numy[i-]; while (y[tot]==i){
sumy[i]+=i;
numy[i]++;
tot++;
}
}
solve();
ll ans=Inf; // fuck(ansx)fuck(ansy)
ll ans1,ans2;
ans1=ans2=; for(int i=;i<=n;i++){
ans1+=abs(i-x[i]);
ans2+=abs(i-y[i]);
// fuck(x[i])
} ans=min(ans1+ansy,ans2+ansx); printf("%lld\n",ans); return ;
} /**********************************************************************
Problem: 2323
User: 232574
Language: C++
Result: AC
Time:520 ms
Memory:25464 kb
**********************************************************************/

CSU 2323 疯狂的企鹅II (中位数的性质)的更多相关文章

  1. 【LeetCode】寻找两个有序数组的中位数【性质分析+二分】

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 ...

  2. Codeforces 433C #248_div1_A 中位数的应用

    擦..今天这套题好尼玛难啊,做了一个小时,连一题都没做出来,而且还没什么头绪 查了下出题人,师大附中的 14年毕业 13年拿到的国家集训队资格 保送清华 题意是 给一串序列,计算一个值,这个值是 相邻 ...

  3. 2019.03.28 bzoj3598: [Scoi2014]方伯伯的商场之旅(带权中位数+数位dp)

    传送门 题意咕咕咕自己读吧挺简单的 思路: 由带权中位数的性质可以得到对于每个数放在每个二进制位的代价一定是个单调或者单峰函数,因此我们先把所有的数都挪到第一个位置,然后依次向右枚举峰点(极值点)把能 ...

  4. CodeForces 433C Ryouko's Memory Note (中位数定理)

    <题目链接> 题目大意:给你一堆数字,允许你修改所有相同的数字成为别的数字,不过只能修改一次,问你修改后序列相邻数字的距离和最小是多少. 解题分析: 首先,修改不是任意的,否则那样情况太多 ...

  5. E1.Send Boxes to Alice(Easy Version)//中位数

    发送盒子给Alice(简单版本) 题意:准备n个盒子放巧克力,从1到n编号,初始的时候,第i个盒子有ai个巧克力. Bob是一个聪明的家伙,他不会送n个空盒子给Alice,换句话说,每个盒子里面都有巧 ...

  6. POJ2010 Moo University - Financial Aid(二分法)

    题目地址 分析:如果用二分法,关键是score和aid分开排序,score排序是为了充分利用中位数的性质,这样就可以确定m左右必须各选N/2个,到这之后有人是用dp求最优解,可以再次按照aid排序一次 ...

  7. POJ 2010 - Moo University - Financial Aid 初探数据结构 二叉堆

    考虑到数据结构短板严重,从计算几何换换口味= = 二叉堆 简介 堆总保持每个节点小于(大于)父亲节点.这样的堆被称作大根堆(小根堆). 顾名思义,大根堆的数根是堆内的最大元素. 堆的意义在于能快速O( ...

  8. Leetcode 4. Median of Two Sorted Arrays(二分)

    4. Median of Two Sorted Arrays 题目链接:https://leetcode.com/problems/median-of-two-sorted-arrays/ Descr ...

  9. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

随机推荐

  1. @codeforces - 932G@ Palindrome Partition

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个字符串 s,求有多少种方案可将其划分成偶数个段 \(p_ ...

  2. Minimum Depth of Binary Tree最短深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. 五.获得MYSQL数据库自动生成的主键

    测试脚本如下: 1 create table test1 2 ( 3 id int primary key auto_increment, 4 name varchar(20) 5 ); 测试代码: ...

  4. MacOS局域网访问Windows7共享文件

    配置步骤 Windows7 进入[控制面板]-[网络和共享中心]-[高级共享设置] 启用网络发现 启用文件和打印机共享 选择要共享的文件或文件夹,点击[共享]-[特定用户] 选择Windows7当前登 ...

  5. dnspython

    dnspython 一个Python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验. 安装 pip install dnspython 解析域名为IP from dns ...

  6. 记忆化搜索(DFS)--How many ways

    How many ways 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起始点所标有的能量.2. ...

  7. 微信服务号获得openid 跟用户信息

    https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxxxxxxxxxxxx&redirect_uri=http://www. ...

  8. LightOJ 1370 Bi-shoe and Phi-shoe【欧拉函数 && 质数】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题意: 给定值,求满足欧拉值大于等于这个 ...

  9. python 字典创建

  10. oracle函数 TO_MULTI_BYTE(c1)

    [功能]将字符串中的半角转化为全角 [参数]c1,字符型 [返回]字符串 [示例] SQL> select to_multi_byte('高A') text from dual; test -- ...