HDU 3262 Seat taking up is tough

来源:互联网 发布:有淘宝店铺可以直播吗 编辑:程序博客网 时间:2024/05/29 17:37

Problem Description
Students often have problems taking up seats. When two students want the same seat, a quarrel will probably begin.

It will have very bad effect when such subjects occur on the BBS.
So, we urgently need a seat-taking-up rule. After several days of argument, the rule finally comes out:
As shown in the figure below, the seats in a classroom form a n×m grid( n rows and m columns), and every cell in the grid represents a seat. The coordinates of the seat in the north-west corner are (1,1) and the coordinates of the seat in the south-east corner seat are (n,m). As you know, some seats make you feel good and some seats don’t. So every seat has a “feeling index”.

Students can take up seats for himself and his friends. Of course, if a seat is already taken up by a student, it can’t be taken up by others.
For the convenience of communication between friends, when a student is trying to take up seats, he wants all the seats he needs to be consecutive and in the same row. If he can do that, he takes up all the seats he needs, and save the most western one for himself. For example, if a student wants to take up 3 seats, then taking (2,2),(2,3),(2,4) and saving (2,2) for himself is ok; but taking (2,2),(2,4),(2,5) is invalid because those seats are not consecutive. Under the precondition of accomplishing his seat-taking job, a student always wants the “feeling index” of his seat to be as large as possible.
However, if a student cannot take up all the seats he needs, he will just try to take up only one seat for himself because he doesn’t want to get into the trouble of explaining “Why they can get seats but I can’t?” to some of his friends. Of course he still wants the “feeling index” of his seat to be as large as possible in that situation.
Everyone wants to know where are the seats he can take up .This problem seems a little bit complicated for them. So they want you to write a program to solve the problem.
 

Input
There are several test cases and the input ended by a line of “0 0 0”.
For each test case:
The first line contains three integers: n , m and k ( 1 <= n,m<=30, 1<=k<=50). It means that there are n rows of seats in the classroom and there are m seats in every row. k is the number of students who come into the classroom trying to take up seats.
Following are n lines describing the seats by north to south order .Each line represents a row of seats and contains m integers, indicating the “feeling index” of every seat in that row, by west to east order. “Feeling index” can be fit in a 32-bit integer.
Then k lines follow (We call them k “student lines”). Each line is in the format of “hh:mm q” ( 0 <= hh < 24, 0 <=mm <= 59, 1<=q<=50 ) meaning that a student comes into the classroom at mm minutes past hh o’clock, trying to take up q seats. mm and hh are all two digit integers with possible leading zero, and q is also an integer. Please note that when a student enters the class room, he begins to do his seat taking job immediately and the job takes no time.
It is guaranteed that the “feeling index” of every seat is different and no students come into the classroom at the same time.
 

Output
You should output k lines for each test case, in the order that correspondent to the above mentioned k “student lines” in the input. Each line must contain two integers indicating the coordinates of the seat which is saved by the student for himself. If the student can’t take up any seats, just output a “-1” instead.
 

Sample Input
5 5 811 12 15 14 1321 22 25 24 2316 17 20 19 186 7 10 8 91 2 5 4 309:00 2 09:01 5 09:02 5 09:03 5 09:04 5 09:05 3 09:06 2 09:07 3 0 0 0
 

Sample Output
2 33 11 14 15 12 52 1-1
 

Source
2009 Asia Ningbo Regional Contest Hosted by NIT
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3265 3263 3269 3260 3261 

 

题意:如果有连续q个座位,先进去的选一个权值最大的,旁边再坐q-1个人。。如果没有的话,就自己一个人坐。

水题,题意理解错卡了一个多小时。

一个trick是要按输入的顺序输出答案。

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;typedef long long ll;#define inf -0x7f7f7fstruct node{    int s,t;    int people;    int id;    int x,y;} f[10005];bool cmp(node a,node b){    if(a.s!=b.s)        return a.s<b.s;    else        return a.t<b.t;}bool cmp1(node a,node b){    return a.id<b.id;}ll a[520][520];bool vis[520][520];int b[520][520];int main(){    int n,m,k;    while(cin>>n>>m>>k,n,m,k)    {        int i,j,ppp,pp;        int hang,lie;        int q;        int iii,uuu;        memset(vis,0,sizeof(vis));        memset(b,0,sizeof(b));        for(i=1; i<=n; i++)            for(j=1; j<=m; j++)                cin>>a[i][j];        for(i=1; i<=k; i++)        {            scanf("%d:%d%d",&f[i].s,&f[i].t,&f[i].people);            f[i].id=i;        }        sort(f+1,f+k+1,cmp);//        for(i=1; i<=k; i++)//         printf("%d %d %d\n",f[i].s,f[i].t,f[i].people);        for(int xxx=1; xxx<=k; xxx++)        {            hang=lie=-1;            ll max=-1e18;            ll sum=-1e18;            for(i=1; i<=n; i++)            {                for(j=1; j<=m; j++)                {                    for(ppp=j; ppp<=j+f[xxx].people-1 &&j+f[xxx].people<=m+1; ppp++)                    {                        if(vis[i][ppp])                            break;                    }                    //cout<<sum<<endl;                    if(ppp==j+f[xxx].people &&!vis[i][j])                    {                        if(a[i][j]>sum)                        {                            sum=a[i][j];                            lie=j;                            hang=i;                        //    cout<<q<<endl;                        }                        // cout<<sum<<endl;                        //   cout<<hang<<" "<<lie<<endl;                    }                }            }            for( ppp=lie; ppp<lie+f[xxx].people; ppp++)            {                vis[hang][ppp]=1;                a[hang][ppp]=-1e18;            }            if(hang==-1 &&lie==-1)            {                int max=-1;                int flag1=0;                for(i=1; i<=n; i++)                    for(j=1; j<=m; j++)                    {                        if(!vis[i][j])                        {                            if(a[i][j]>max)                            {                                flag1=1;                                max=a[i][j];                                iii=i;                                uuu=j;                            }                        }                    }                if(flag1)                {                    vis[iii][uuu]=1;                    // cout<<iii<<" "<<uuu<<endl;                    f[xxx].x=iii;                    f[xxx].y=uuu;                }                else                {                    f[xxx].x=-1;                    //  printf("-1\n");                }            }            else            {                //   cout<<hang<<" "<<lie<<endl;                f[xxx].x=hang;                f[xxx].y=lie;            }        }        sort(f+1,f+k+1,cmp1);        for(i=1; i<=k; i++)        {            if(f[i].x!=-1)                cout<<f[i].x<<" "<<f[i].y<<endl;            else                cout<<-1<<endl;        }    }    return 0;}

0 0
原创粉丝点击