1251. Cemetery Manager

Time limit: 1.0 second
Memory limit: 64 MB
There is a tradition at the USU championships to call the most hard-to-solve problems coffins. But to distribute coffins is also a very difficult problem. Consider a cemetery with places arranged in the form of a rectangle having N rows and M columns (1 ≤ NM ≤ 100). At the initial moment of time (t = 0) the cemetery is empty. Incoming coffins are put in the row with empty places that has a minimal number; if there are several empty spaces in this row, then the column with the minimal number is chosen. From time to time the cemetery's clients are visited by their living friends and relatives; it is considered to be a pleasure for the clients. But it's only a headache for the cemetery manager, since because of these visitors he cannot give to new clients places that have been used. Happily, visitors are not perfect, so after some time they forget where their friends have been lying. That is why if a client was not visited for more than successive 1000 days, then on the 1001st day the manager regards the grave as empty. However, relatives of the adjacent clients (of those for whom the differences in the numbers of rows and columns are not greater than 1) may notice strange changes, so the manager puts a new client on a used place only if all the neighboring graves have not been visited for the last 100 days (this is a period of time sufficient for a neighbor's friends to forget who was lying next to him or her). If, notwithstanding all the efforts of the manager, there is no place where he can put a new client, then the client is sent to a crematorium.
We have a complete list of arriving clients and coming visitors for some period starting from the foundation of the cemetery. Basing on this information, you should determine how many clients have been sent to a crematorium.

Input

The first input line contains numbers N and M that describe the size of the cemetery. Each of the next lines describes an event. A description starts with the time of the event measured in days from the foundation of the cemetery. Then the type of the event is given: either d (arrival of a new client) or v (a visit of friends or relatives) followed with the number of the client who has visitors. The events are ordered according to their time. The input contains not more than 15000 events, and not more than 10000 of them describe arrivals of new clients.

Output

The program should find the number of clients that have been sent to a crematorium.

Sample

input output
2 2
1 d
1 d
1 d
1 d
300 d
500 v 2
1001 d
1002 d
1002 d
1003 v 3
1003 d
1003 d
1236 v 2
2032 v 2
2033 d
3

Notes

  1. Each tomb has 2 to 8 neighbors.
  2. If a client was buried on day T then the tomb may be dug over on day T+1001 and may not be dug over on day T+1000.
  3. If a tomb was visited on day T then its neighbors may be dug over on day T+101 and may not be dug over on day T+100.
  4. A tomb is dug over as soon as there is an opportunity (see items 2 and 3).
  5. During a funeral relatives notice nothing including the neighbors.
  6. The clients are numbered in the the order that they arrive (including those who was sent to crematorium).
  7. If there is already no tomb or the client has been sent to the crematorium immediately or there is no client with the required number then a visit affects nothing.
  8. The next in turn client may be always burried in an empty tomb inspite of the neighbor tombs visits (the neighbors' relatives wouldn't be surprised having found out that the adjacent empty tomb is already occupied).
Problem Author: Stanislav Vasilyev
Problem Source: Open collegiate programming contest for student teams, Ural State University, March 15, 2003
Difficulty: 1522
 
题意:自己看题吧。比较复杂,注意看题后面的tips。
分析:可以用两个优先队列搞一下就可以了。
一个维护当前的空墓地,按照x,y的顺序存进优先队列。
一个维护当前墓地的有效时间(即即将被铲掉的时间)。
如果这个有效时间被修改,其实可以打个标记什么的,将新时间扔进优先队列,不需要删除原来那个。这题并不会爆空间。
这题比较麻烦,居然能够1A。也是蛮幸运的。
 /**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , MAXINDEX = , LEN = , ILEN = ;
class Node
{
private :
int x, y; public :
Node() {}
Node(int tx, int ty)
{
x = tx, y = ty;
} inline bool operator <(const Node &t) const
{
if(x != t.x) return x > t.x;
return y > t.y;
} inline int GetRow()
{
return x;
} inline int GetCol()
{
return y;
} inline int GetTime()
{
return x;
} inline int GetIndex()
{
return y;
}
} ;
class Heap
{
private :
priority_queue<Node> Store; public :
inline void Push(int x, int y)
{
Store.push(Node(x, y));
} inline void Push(const Node &x)
{
Store.push(x);
} inline void Pop()
{
Store.pop();
} inline Node GetTop()
{
return Store.top();
} inline bool Empty()
{
return Store.empty();
}
} deadtime, emptylist;
int n, m, cnttombs;
int endtime[MAXINDEX], graph[N][N];
Node where[MAXINDEX];
bool have[MAXINDEX];
int ans; inline void Input()
{
scanf("%d%d", &n, &m);
} inline void Dug(int now)
{
while(!deadtime.Empty())
{
Node t = deadtime.GetTop();
int idx = t.GetIndex(), deadline = t.GetTime();
if(!have[idx] || endtime[idx] != deadline)
deadtime.Pop();
else if(deadline >= now) break;
else
{
emptylist.Push(where[idx]);
graph[where[idx].GetRow()][where[idx].GetCol()] = -;
where[idx] = Node(-, -);
endtime[idx] = -, have[idx] = ;
deadtime.Pop();
}
}
} inline bool AddTomb(int now)
{
bool ret = ;
cnttombs++;
while(!ret && !emptylist.Empty())
{
Node t = emptylist.GetTop();
int x = t.GetRow(), y = t.GetCol();
graph[x][y] = cnttombs, where[cnttombs] = t;
have[cnttombs] = , endtime[cnttombs] = now + LEN;
deadtime.Push(endtime[cnttombs], cnttombs);
emptylist.Pop();
ret = ;
}
return ret;
} inline bool Check(int x, int y)
{
if(x < || x >= n || y < || y >= m) return ;
if(!graph[x][y] || !have[graph[x][y]]) return ;
return ;
} inline void Visit(int now, int idx)
{
const int DX[] = {-, , , , -, -, , },
DY[] = {, -, , , -, , -, };
if(!have[idx]) return;
int x = where[idx].GetRow(), y = where[idx].GetCol();
endtime[idx] = max(endtime[idx], now + LEN);
deadtime.Push(endtime[idx], idx);
for(int t = ; t < ; ++ t)
{
int dx = x + DX[t], dy = y + DY[t];
if(!Check(dx, dy)) continue;
endtime[graph[dx][dy]] = max(endtime[graph[dx][dy]], now + ILEN);
deadtime.Push(endtime[graph[dx][dy]], graph[dx][dy]);
}
} inline void Solve()
{
for(int i = ; i < n; i++)
for(int j = ; j < m; j++)
emptylist.Push(i, j); char type;
int t, idx;
while(scanf("%d", &t) == )
{
for(type = ' '; type != 'v' && type != 'd'; type = getchar());
Dug(t);
if(type == 'd')
{
bool ret = AddTomb(t);
ans += !ret;
}
else
{
scanf("%d", &idx);
Visit(t, idx);
}
} printf("%d\n", ans);
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}

ural 1251. Cemetery Manager的更多相关文章

  1. ural 1255. Graveyard of the Cosa Nostra

    1255. Graveyard of the Cosa Nostra Time limit: 1.0 secondMemory limit: 64 MB There is a custom among ...

  2. ural 1252. Sorting the Tombstones

    1252. Sorting the Tombstones Time limit: 1.0 secondMemory limit: 64 MB There is time to throw stones ...

  3. ural 1249. Ancient Necropolis

    1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bit ...

  4. URAL 1252 ——Sorting the Tombstones——————【gcd的应用】

    Sorting the Tombstones Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  5. URAL ——1249——————【想法题】

     Ancient Necropolis Time Limit:5000MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u ...

  6. Windows下Redis缓存服务器的使用 .NET StackExchange.Redis Redis Desktop Manager

    Redis缓存服务器是一款key/value数据库,读110000次/s,写81000次/s,因为是内存操作所以速度飞快,常见用法是存用户token.短信验证码等 官网显示Redis本身并没有Wind ...

  7. 如何重新注册VMware Update Manager(VUM)至vCenter Server中

    在VMware的vSphere化境中,VUM的角色相当于Windows 环境中的WSUS(Windows 更新服务器),可以批量,自动化的完成所管辖ESXi主机的大版本迁移,小版本升级的任务,深受管理 ...

  8. 使用tomcat manager 管理和部署项目

    在部署tomcat项目的时候,除了把war文件直接拷贝到tomcat的webapp目录下,还有一种方法可以浏览器中管理和部署项目,那就是使用tomcat manager. 默认情况下,tomcat m ...

  9. Ubuntu管理开机启动服务项 -- 图形界面的Boot-up Manager

    有时学习时安装的服务太多,比如mysql.mongodb.redis.apache.nginx等等,它们都是默认开机启动的,如果不想让它们开机启动,用到时再自己手工启动怎么办呢? 使用sysv-rc- ...

随机推荐

  1. CountdownLatchTest

    import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java ...

  2. UINavigationController导航条是否挡住下面的内容

    控制 UINavigationController 导航条是否挡住下面的内容 if ([[[UIDevice currentDevice] systemVersion] floatValue] > ...

  3. linux eclipse3.6.1 maven安装

    linux maven安装及 eclipse maven插件安装,有需要的朋友可以参考下. 1. maven的安装(apache-maven-3.0.5为例):  a.官网地址:http://mave ...

  4. 二、JavaScript语言--JS基础--JavaScript进阶篇--选项卡切换效果

    利用JavaScript知识,实现选项卡切换的效果. 效果图: 文字素材: 房产: 275万购昌平邻铁三居 总价20万买一居     200万内购五环三居 140万安家东三环     北京首现零首付楼 ...

  5. Asp.Net - 8.多线程

    8.1 概念 进程(Process):是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.进程之间是相对独立的,一个进程无法直接访问另一个进程的数据(除非利用分布式计算方式),一个 ...

  6. SQL在INNER JOIN时,也可以将子查询加入进来

    这个语法有点神奇,记下. 但觉得用处有限吧. mysql> SELECT a.account_id, a_cust_id, a.open_date, a.product_cd -> FRO ...

  7. Swipe JS – 移动WEB页面内容触摸滑动类库

    想必做移动前端的同学经常会接到这样子的一个需求,就是在移动设备页面上的banner图能够用手指触摸左右或上下的滑动切换,这在移动设备是个很常见的一个效果,其用户体验远甚于点击一个按钮区域,通过手指的触 ...

  8. 使用RMAN DUPLICATE...FROM ACTIVE DATABASE创建物理standby database

    Applies to: Oracle Server - Enterprise Edition - Version 11.1.0.6 to 11.2.0.4 [Release 11.1 to 11.2] ...

  9. proxifier 代理bluestack

    proxycap 可以很方便的代理bluestack, 但是proxycap 的破解版现在越来越不好用了,而且不小心还会中个病毒,这个时候免费的proxifier就显得更加的可爱了. 但是有个问题,就 ...

  10. Thinkphp 解决写入配置文件的方法

    在/Application/Common/Common创建function.php,然后添加以下代码: <?php /** * [writeArr 写入配置文件方法] * @param [typ ...