$Poj1723/AcWing123\ Soldiers$ 排序

$Poj$ $AcWing$

 

$Description$

 

 

$Sol$

分别处理$x$坐标和$y$坐标.$y$坐标显然很好处理,就是排个序然后取中位数就好了.$x$没有$y$那么直接叭.所以我首先写了个大暴力$ovo$,居然过了$AcWing$(太水了).当然过不了$Poj$.所以再观察一下,发现对于枚举的一个$x$作为一条平行线的左端点的$x$值,排序后,累计答案为$as+=abs(x[i]-(x+i-1))$,整理一下,$as+=abs((x[i]-i+1)-x)$,发现被减数是一定的,于是现在和$y$的情况是一样的辣.$over.$

 

$Code$

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define yes(i,a,b) for(Rg int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define db double
#define inf (1<<30)
using namespace std;
il int read()
{
    Rg int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
const int N=10010;
int n,x[N],y[N];
ll as=inf;
int main()
{
    n=read();go(i,1,n)x[i]=read(),y[i]=read();
    sort(x+1,x+n+1);sort(y+1,y+n+1);
    go(i,x[1]-n+1,x[n])
    {
        ll qvq=0;
        go(j,1,n)qvq+=abs(x[j]-(i+j-1));
        as=min(as,qvq);
    }
    go(i,1,n)as+=abs(y[i]-y[n/2+1]);
    printf("%lld\n",as);
    return 0;
}
View 暴力 Code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define yes(i,a,b) for(Rg int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define db double
#define inf (1<<30)
using namespace std;
il int read()
{
    Rg int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
const int N=10010;
int n,x[N],y[N];
ll as;
int main()
{
    n=read();go(i,1,n)x[i]=read(),y[i]=read();
    sort(x+1,x+n+1);sort(y+1,y+n+1);
    go(i,1,n)x[i]-=i;sort(x+1,x+n+1);
    go(i,1,n)as+=abs(y[i]-y[n/2+1])+abs(x[i]-x[n/2+1]);
    printf("%lld\n",as);
    return 0;
}
View AC Code

 

posted @ 2019-08-20 09:42  DTTTTTTT  阅读(187)  评论(0编辑  收藏  举报