HDU 3687 National Day Parade

來源:互聯網
上載者:User

傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=3687


題意:

給你一個n*m的方陣,裡面有n*n個人並給出座標,問你最少走多少步可以形成一個n*n的正方形方陣,每個人只能向左向右移動。

題解:

資料量不大,直接暴力搞定,n*n*m


AC代碼:

3687 46MS 384K 1535 B G++

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <iterator>#include <stack>#include <map>#include <set>#include <algorithm>#include <cctype>using namespace std;#define si1(a) scanf("%d",&a)#define si2(a,b) scanf("%d%d",&a,&b)#define sd1(a) scanf("%lf",&a)#define sd2(a,b) scanf("%lf%lf",&a,&b)#define ss1(s)  scanf("%s",s)#define pi1(a)    printf("%d\n",a)#define pi2(a,b)  printf("%d %d\n",a,b)#define mset(a,b)   memset(a,b,sizeof(a))#define forb(i,a,b)   for(int i=a;i<b;i++)#define ford(i,a,b)   for(int i=a;i<=b;i++)typedef long long LL;const int N=33;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);const double eps=1e-8;int dat[57][201],cnt[57];int n,m;int main(){//    freopen("input.txt","r",stdin);    while(scanf("%d%d",&n,&m),n||m)    {        int tol=n*n,i,j,x,y,k;        memset(cnt,0,sizeof(cnt));        for(i=1;i<=tol;i++)        {            scanf("%d%d",&x,&y);            dat[x][cnt[x]++]=y;        }        for(i=1;i<=n;i++)            sort(dat[i],dat[i]+n);        int ans=100000000;        for(i=1;i<=m;i++)        {            int sum=0;            for(j=1;j<=n;j++)                for(k=0;k<n;k++)                    sum+=abs(dat[j][k]-i-k);            if(ans>sum)                ans=sum;        }        printf("%d\n",ans);    }    return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.