ACM训练联盟周赛 K. Teemo's reunited

Teemo likes to drink raspberry juice.  He even spent some of his spare time tomake the raspberry juice himself. The way to make the raspberries juice is simple. You just have to press the raspberries through a fine sieve.

Unfortunately,today Teemo was splited in several pieces by the sieve which was used to makethe raspberry juice. The pieces were losted in the huge two-dimensional map. Onlywhen all the pieces gather, can Teemo drink the raspberry juice he made today. 

Teemo's piece can only move parallel to the x or y axis, or he would be hated by theraspberry Queen and will not be able to have raspberry juice any more. One of the piece of Teemo should carry the raspberry juice.In order to avoid spilling, this piece cannot move anymore. 

Teemo’spiece are lazy, they’d like to make the sum of paths be the minimal. Your task is to find the minimal sum of the paths.

InputFormat
The first line contains a integer n (1<=n<=100000) represent the number of thepieces. Then next n lines. Each line contains the pairs of xi, yi(-1000000000<xi,yi<1000000000) in turn as points by order. 

OutputFormat
Printa single line contains the minimal sum of the paths. 

样例输入1

3
1 0
2 0
3 0

样例输出1

2

样例输入2

5
4 1
4 4
9 2
2 9
2 6

样例输出2

21


复制代码
//必须要先排序
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <map>
#include <vector>
#include <stack>
using namespace std;
#define  ll long long 
#define  N   100009
#define  M 5000000000000009
#define  gep(i,a,b)  for(int  i=a;i<=b;i++)
#define  gepp(i,a,b) for(int  i=a;i>=b;i--)
#define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
#define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
#define  mem(a,b)  memset(a,b,sizeof(a))
#define  ph  push_back
#define  mod  1000000007
struct Node{
    ll id,x,y;
}nod[N];
ll sumx[N],sumy[N],l[N],r[N];
bool cmpx(Node a,Node b){
    return a.x<b.x;
}
bool cmpy(Node a,Node b)
{
    return a.y<b.y;
}
ll n;
int main()
{
    scanf("%lld",&n);
    gep1(i,1,n) {
        scanf("%lld%lld",&nod[i].x,&nod[i].y);
        nod[i].id=i;
    }
    sort(nod+1,nod+1+n,cmpx);
    gep(i,1,n){
        sumx[i]=sumx[i-1]+nod[i].x;
    }
    ll sx=0;
    // 1 2 3 4 5 (x)
    // 3   3-1+3-2 +   4-3+5-3
    gep(i,1,n){
        sx=nod[i].x*(i-1)-sumx[i-1]+sumx[n]-sumx[i]-nod[i].x*(n-i);
        l[nod[i].id]=sx;
    }    
    sort(nod+1,nod+1+n,cmpy);    
    gep(i,1,n){
        sumy[i]=sumy[i-1]+nod[i].y;
    }
    ll sy=0;
    gep(i,1,n){
        sy=nod[i].y*(i-1)-sumy[i-1]+sumy[n]-sumy[i]-nod[i].y*(n-i);
        r[nod[i].id]=sy;//每一次的排序都会造成id的变化,id :最初那个数据在当前的位置
    }    
    ll MIN=M;
    gep(i,1,n){
        MIN=min(MIN,l[i]+r[i]);    
    }
    printf("%lld\n",MIN);
    return 0;
}    
复制代码

 

posted on   cltt  阅读(171)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示