HDU 3687 National Day Parade

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3687

题目大意:怎么说捏,就是把乱七八糟的队形用最短步数将他们排成矩形,人只能左右动。

做法:枚举起点。

好像就这些...

代码:

View Code
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 200000
int n,m;
int map[60][210];
void init()
{
int x,y;
memset(map,0,sizeof(map));
for (int i=1;i<=n*n;i++)
{
scanf("%d%d",&x,&y);
map[x][0]++;
map[x][map[x][0]]=y;
}
for (int i=1;i<=n;i++)
sort(map[i]+1,map[i]+map[i][0]+1);
}
void work()
{
int Max=0,ans=INF,tans,Min=INF;
for (int i=2;i<=n;i++)
{
if (Max<map[i][map[i][0]]) Max=map[i][map[i][0]];
if (Min>map[i][1]) Min=map[i][1];
}

for (int k=1;k<=m-n;k++)
{
tans=0;
for (int i=1;i<=n;i++)
for(int j=1;j<=map[i][0];j++)
tans+=abs(map[i][j]-(k+j-1));
ans=min(ans,tans);
}
printf("%d\n",ans);
}
int main()
{
freopen("in","r",stdin);
freopen("out","w",stdout);
scanf("%d%d",&n,&m);
while (n>0 && m>0)
{
init();
work();
scanf("%d%d",&n,&m);
}
fclose(stdin);fclose(stdout);
return 0;
}

我最近总刷水题啊...哎呀堕落了。

你可能感兴趣的:(HDU)