• HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)


    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.

     题目大意:有n*m个座位,每个座位有一个权,每个学生来到之后会占座位,他占的座位一点在同一行而且连续,他一定会做在那排连续座位的最左边。在占到他想占的数量座位的前提下,他会找权值最大的座位来坐。如果不能帮别人占座位,他会自己选择一个权值最大的座位来坐而不帮朋友占座了。如果连自己的座位都没有,他会选择离开。现在有k个学生分别来占座,问他们占到的自己座位的坐标是什么,若离开了就输出-1.

    思路:大水题,对每个学生的到达时间线排个序(不排会WA我试过了O(∩_∩)O),然后对每一个学生,先暴力枚举连续q个座位看能不能坐,能则选最大的,不能则再次暴力枚举空座位,选最大的,还是不能就只能滚粗了……最后按原来给的顺序输出答案即可。

     代码(15MS):

      1 #include <cstdio>
      2 #include <algorithm>
      3 #include <iostream>
      4 #include <cstring>
      5 using namespace std;
      6 
      7 const int MAXN = 35;
      8 const int MAXK = 55;
      9 
     10 struct Node {
     11     int id, t, q;
     12     void read(int i) {
     13         int hh, mm;
     14         scanf("%d:%d %d", &hh, &mm, &q);
     15         t = hh * 60 + mm;
     16         id = i;
     17     }
     18     bool operator < (const Node &rhs) const {
     19         return t < rhs.t;
     20     }
     21 };
     22 
     23 Node a[MAXK];
     24 int mat[MAXN][MAXN];
     25 bool use[MAXN][MAXN];
     26 int ans[MAXK][2], leave[MAXK];
     27 int n, m, k;
     28 
     29 void init() {
     30     memset(use, 0, sizeof(use));
     31 }
     32 
     33 bool check(int x, int y, int l) {
     34     for(int i = 0; i < l; ++i)
     35         if(use[x][y + i]) return false;
     36     return true;
     37 }
     38 
     39 void make_use(int x, int y, int l) {
     40     for(int i = 0; i < l; ++i)
     41         use[x][y + i] = true;
     42 }
     43 
     44 void solve() {
     45     int max_comf, ans_i, ans_j;
     46     bool flag;
     47     for(int x = 1; x <= k; ++x) {
     48         flag = false;
     49         for(int i = 1; i <= n; ++i) {
     50             for(int j = 1; j <= m - a[x].q + 1; ++j) {
     51                 if(!flag || mat[i][j] > max_comf) {
     52                     if(!check(i, j, a[x].q)) continue;
     53                     flag = true;
     54                     ans_i = i; ans_j = j;
     55                     max_comf = mat[i][j];
     56                 }
     57             }
     58         }
     59         if(flag) {
     60             leave[a[x].id] = false;
     61             ans[a[x].id][0] = ans_i;
     62             ans[a[x].id][1] = ans_j;
     63             make_use(ans_i, ans_j, a[x].q);
     64             continue;
     65         }
     66         for(int i = 1; i <= n; ++i) {
     67             for(int j = 1; j <= m; ++j) {
     68                 if(!flag || mat[i][j] > max_comf) {
     69                     if(use[i][j]) continue;
     70                     flag = true;
     71                     ans_i = i; ans_j = j;
     72                     max_comf = mat[i][j];
     73                 }
     74             }
     75         }
     76         if(flag) {
     77             leave[a[x].id] = false;
     78             ans[a[x].id][0] = ans_i;
     79             ans[a[x].id][1] = ans_j;
     80             use[ans_i][ans_j] = true;
     81             continue;
     82         }
     83         else leave[a[x].id] = true;
     84     }
     85 }
     86 
     87 int main() {
     88     while(scanf("%d%d%d", &n, &m, &k) != EOF) {
     89         if(n == 0 && m == 0 && k == 0) break;
     90         for(int i = 1; i <= n; ++i)
     91             for(int j = 1; j <= m; ++j) scanf("%d", &mat[i][j]);
     92         for(int i = 1; i <= k; ++i) a[i].read(i);
     93         sort(a + 1, a + k + 1);
     94         init();
     95         solve();
     96         for(int i = 1; i <= k; ++i) {
     97             if(leave[i]) puts("-1");
     98             else printf("%d %d
    ", ans[i][0], ans[i][1]);
     99         }
    100     }
    101 }
    View Code
  • 相关阅读:
    LeetCode OJ Remove Duplicates from Sorted Array II
    LeetCode OJ 75. Sort Colors
    LeetCode OJ 74. Search a 2D Matrix
    LeetCode OJ 73. Set Matrix Zeroes
    Taglist
    NERDTree
    Beyond Compare 4
    Beyond compare vs kdiff3
    切換 java compiler 版本
    PE+ 1.0 ( Pump Express Plus 1.0 )
  • 原文地址:https://www.cnblogs.com/oyking/p/3262577.html
Copyright © 2020-2023  润新知