http://vjudge.net/contest/view.action?cid=47807#problem/B
Description
All cinema halls in Berland are rectangles with K rows of K seats each, and K is an odd number. Rows and seats are numbered from 1 toK. For safety reasons people, who come to the box office to buy tickets, are not allowed to choose seats themselves. Formerly the choice was made by a cashier, but now this is the responsibility of a special seating program. It was found out that the large majority of Berland's inhabitants go to the cinema in order to watch a movie, that's why they want to sit as close to the hall center as possible. Moreover, a company of M people, who come to watch a movie, want necessarily to occupy M successive seats in one row. Let's formulate the algorithm, according to which the program chooses seats and sells tickets. As the request for M seats comes, the program should determine the row number x and the segment [yl, yr] of the seats numbers in this row, where yr - yl + 1 = M. From all such possible variants as a final result the program should choose the one with the minimum function value of total seats remoteness from the center. Say,
— the row and the seat numbers of the most "central" seat. Then the function value of seats remoteness from the hall center is
. If the amount of minimum function values is more than one, the program should choose the one that is closer to the screen (i.e. the row number x is lower). If the variants are still multiple, it should choose the one with the minimum yl. If you did not get yet, your task is to simulate the work of this program.
Input
The first line contains two integers N and K (1 ≤ N ≤ 1000, 1 ≤ K ≤ 99) — the amount of requests and the hall size respectively. The second line contains N space-separated integers Mi from the range [1, K] — requests to the program.
Output
Output N lines. In the i-th line output «-1» (without quotes), if it is impossible to find Mi successive seats in one row, otherwise output three numbers x, yl, yr. Separate the numbers with a space.
Sample Input
2 1 1 1
1 1 1 -1
4 3 1 2 3 1
2 2 2 1 1 2 3 1 3 2 1 1
最小值,分配的座位必须在同一行且连续。若该最小值相等取i 较小的,还相等取j较小的。如果不能成功分配输出-1.若能,输出所在的行和所在列的起止点。
解题思路:范围0~10,暴力即可
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int mm=111;
int s[mm][mm],g[mm][mm];
int i,j,k,n,m;
int ab(int x)
{
return x<0?-x:x;
}
void solve(int m)
{
int i,j,x,y,tmp,ans=1e9;
for(i=1;i<=k;++i)
{
for(j=1;j<=k;++j)
if(!g[i][j])
{
if(j>=m&&s[i][j]-s[i][j-m]<ans)
{
ans=s[i][j]-s[i][j-m];
x=i,y=j-m+1;
}
}
else break;
for(j=k-1;j>=0;--j)
if(!g[i][j+1])
{
if(j+m<=k&&s[i][j+m]-s[i][j]<ans)
{
ans=s[i][j+m]-s[i][j];
x=i,y=j+1;
}
}
else break;
}
if(ans<1e9)
{
printf("%d %d %d\n",x,y,y+m-1);
while(m--)g[x][y++]=1;
}
else puts("-1");
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
memset(s,0,sizeof(s));
memset(g,0,sizeof(g));
for(i=1;i<=k;++i)
for(j=1;j<=k;++j)
{
s[i][j]=ab(i-(k+1)/2)+ab(j-(k+1)/2);
s[i][j]+=s[i][j-1];
}
while(n--)
{
scanf("%d",&m);
solve(m);
}
}
return 0;
}
本文介绍了一种用于电影票务系统的座位分配算法。该算法的目标是在k*k的电影院中,为顾客分配尽可能靠近中心的连续座位,通过计算每个座位到中心的距离来确定最优解。当遇到多个解时,选择距离屏幕更近且起始位置更靠前的座位。

581

被折叠的 条评论
为什么被折叠?



