CodeForces - 10B Cinema Cashier【模拟】

【题目描述】
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 to K. 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.

【输入】
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 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.

【样例输入1】
2 1
1 1

【样例输出1】
1 1 1
-1

【样例输入2】
4 3
1 2 3 1

【样例输出2】
2 2 2
1 1 2
3 1 3
2 1 1

题目链接:https://codeforces.com/contest/10/problem/B

有一个K*K的正方形影院,一共有n批人,假设某一批为m,这m个人必须坐连续的一排位置,且要求到影院中心点的曼哈顿距离之和最小,如果有多种情况优先坐行数小的,还有多种情况优先列数小的
暴力模拟即可

代码如下:

#include 
#include 
using namespace std;
static const int MAXN=100+10;
bool vis[MAXN][MAXN];
int n,k,m;
int cen;
int main()
{
     
    cin>>n>>k;
    cen=k+1>>1;
    for(int Kase=1;Kase<=n;Kase++)
    {
     
        cin>>m;
        int _min=0x3f3f3f3f;
        int x,y;
        for(int i=1;i<=k;i++)
            for(int j=1;j<=k-m+1;j++)
            {
     
                bool ok=true;
                int cnt=0;
                for(int l=j;l<=j+m-1;l++)
                    if(vis[i][l])
                    {
     
                        ok=false;
                        break;
                    }
                    else cnt+=abs(i-cen)+abs(l-cen);
                if(ok && cnt<_min)
                {
     
                    _min=cnt;
                    x=i;
                    y=j;
                }
            }
        if(_min==0x3f3f3f3f) cout<<"-1"<<endl;
        else
        {
     
            cout<<x<<" "<<y<<" "<<y+m-1<<endl;
            for(int i=y;i<y+m;i++) vis[x][i]=true;
        }
    }
    return 0;
}

你可能感兴趣的:(模拟,codeforces)