1650 uestc Electric System Restore

题目链接:1650 uestc  Electric System Restore


比赛的时候没有搞出来,中位数神马性质什么的也不知道,贴下解题报告:

当k=0 时,基站的横纵坐标应为对应的中位数。而删 k个点之后,中位数最多向上下左右移k/2+1个位置。所以我们可以先枚举基站的可能位置。对每个位置,假设一开始所有点都是用基站去供应的,那么把到基站的距离与自己供应的花费的差值作为关键字,取最大的 k个(可以取不满)删去。复杂度O(n*k^2*log k)


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>


using namespace std;
#define inf 0x3fffffffffffffff
const int maxn=1010;
struct Point
{
    int x,y,cost;
}p[maxn];
int n,x[maxn],y[maxn],d[maxn],k;

int abs1(int a)
{
    return a>0?a:-a;
}
long long check(int x,int y)
{
    long long ret=0;
    for(int i=0;i<n;i++)
    {
        int tt=(abs1(x-p[i].x)+abs1(y-p[i].y));
        d[i]=p[i].cost-tt;
        ret+=tt;
    }
    sort(d,d+n);
    for(int i=0;i<k;i++)
      if(d[i]<0) ret+=d[i];
      else break;
    return ret;
}

int main()
{
    int T,ca=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&k);
        for(int i=0;i<n;i++)
          scanf("%d",&p[i].cost);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&p[i].x,&p[i].y);
            x[i]=p[i].x;
            y[i]=p[i].y;
        }
        sort(x,x+n);
        sort(y,y+n);
        int l=max(0,n/2-k/2-1);
        int r=min(n-1,n/2+k/2+1);
        long long ans=inf;

        for(int i=l;i<=r;i++)
        for(int j=l;j<=r;j++)
        {
            long long tmp=check(x[i],y[j]);
            ans=min(ans,tmp);
        }
        printf("Case #%d: %lld\n",ca++,ans);
    }
    return 0;
}


你可能感兴趣的:(1650 uestc Electric System Restore)