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):

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std; const int MAXN = ;
const int MAXK = ; struct Node {
int id, t, q;
void read(int i) {
int hh, mm;
scanf("%d:%d %d", &hh, &mm, &q);
t = hh * + mm;
id = i;
}
bool operator < (const Node &rhs) const {
return t < rhs.t;
}
}; Node a[MAXK];
int mat[MAXN][MAXN];
bool use[MAXN][MAXN];
int ans[MAXK][], leave[MAXK];
int n, m, k; void init() {
memset(use, , sizeof(use));
} bool check(int x, int y, int l) {
for(int i = ; i < l; ++i)
if(use[x][y + i]) return false;
return true;
} void make_use(int x, int y, int l) {
for(int i = ; i < l; ++i)
use[x][y + i] = true;
} void solve() {
int max_comf, ans_i, ans_j;
bool flag;
for(int x = ; x <= k; ++x) {
flag = false;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m - a[x].q + ; ++j) {
if(!flag || mat[i][j] > max_comf) {
if(!check(i, j, a[x].q)) continue;
flag = true;
ans_i = i; ans_j = j;
max_comf = mat[i][j];
}
}
}
if(flag) {
leave[a[x].id] = false;
ans[a[x].id][] = ans_i;
ans[a[x].id][] = ans_j;
make_use(ans_i, ans_j, a[x].q);
continue;
}
for(int i = ; i <= n; ++i) {
for(int j = ; j <= m; ++j) {
if(!flag || mat[i][j] > max_comf) {
if(use[i][j]) continue;
flag = true;
ans_i = i; ans_j = j;
max_comf = mat[i][j];
}
}
}
if(flag) {
leave[a[x].id] = false;
ans[a[x].id][] = ans_i;
ans[a[x].id][] = ans_j;
use[ans_i][ans_j] = true;
continue;
}
else leave[a[x].id] = true;
}
} int main() {
while(scanf("%d%d%d", &n, &m, &k) != EOF) {
if(n == && m == && k == ) break;
for(int i = ; i <= n; ++i)
for(int j = ; j <= m; ++j) scanf("%d", &mat[i][j]);
for(int i = ; i <= k; ++i) a[i].read(i);
sort(a + , a + k + );
init();
solve();
for(int i = ; i <= k; ++i) {
if(leave[i]) puts("-1");
else printf("%d %d\n", ans[i][], ans[i][]);
}
}
}

HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)的更多相关文章

  1. HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

    Description Ted has a new house with a huge window. In this big summer, Ted decides to decorate the ...

  2. HDU 3262 Seat taking up is tough (模拟搜索)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3262 题意:教室有n*m个座位,每个座位有一个舒适值,有K个学生在不同时间段进来,要占t个座位,必须是连 ...

  3. POJ 3829 Seat taking up is tough(——只是题目很长的模拟)

    题目链接: http://poj.org/problem?id=3829 题意描述: 输入矩阵的大小n和m,以及来占位置的人数k 输入n*m的教室座位矩阵,每个值表示该座位的满意度 输入每个人来占位置 ...

  4. HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  5. HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)

    Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...

  6. HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

  7. HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)

    Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...

  8. HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索

    题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析:  枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...

  9. HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]

    标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...

随机推荐

  1. Eclipse新导入的项目中ctrl+点击指定方法名或类名没有反应,不能跳转问题

    项目没有转成java项目 解决方法:右击项目名---选择properties----点击Project Facets,这样就可以实现ctrl+左键点击跳转了. 转成java项目后会报错 解决办法:选中 ...

  2. 浅谈Jquery和常用框架Vue变化

    区别 Vue数据与视图的分离 Vue数据驱动视图 Jquery 简单示例: <!DOCTYPE html> <html lang="en"> <hea ...

  3. ComboBox可搜索下拉框的使用注意事项,简单记录以及我遇到的一些奇怪的bug

    前几天做一个react的项目的时候需要用一个可搜索的下拉框ComboBox,上代码: <ComboBox // className={comboxClassName} items={storeA ...

  4. 小心使用replicate_do_db和replicate_ignore_db

    内容来源于网络 使用replicate_do_db和replicate_ignore_db时有一个隐患,跨库更新时会出错 如设置 replicate_do_db=testuse mysql;updat ...

  5. php源码建博客4--实现MVC结构微型框架

    主要: 常量优化路径 自动加载类 优化入口文件 安全访问项目目录 --------------文件结构:-------------------------------------- blog├─App ...

  6. 嵌入式GPIO接口及操作(二)

    目标:C语言实现点亮LED灯 首先是main函数,并不特殊,它是被系统调用来执行的,main函数结束后要返回调用main函数的地址处,那么裸机程序,没有操作系统做这些工作,就要自己写调用main函数的 ...

  7. 用树莓派做电视盒子,安装Android TV系统

    有位朋友问我,如何在树莓派上安装盒子系统,这期我就教大家如何安装Android系统,自动动手做一个机顶盒. 如何安装系统,我已经在 树莓派安装系统 这篇文章中了做介绍,有需要的请看这篇文章.安装系统需 ...

  8. golang 协程嵌套,会产生依赖关系(父子关系)么?

    编码时冒出一个问题:在一个协程内部,再创建一个或多个协程,是否会产生依赖关系? 做了一个小实验,这里随笔记录一下经过,备注后续深入研究. test代码: package main import ( & ...

  9. nodejs module/require

    1. wrap up a module using module.exports, name the file to a.js var fun1=function(){ var stylus = re ...

  10. Java设计模式(4)——创建型模式之单例模式(Singleton)

    一.概述 弥补一下之前没有给设计模式下的定义,先介绍一下设计模式(引用自百度百科): 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计 ...