码迷,mamicode.com
首页 > 其他好文 > 详细

Aizu 1258 Book Replacement deque+模拟

时间:2015-03-31 16:10:39      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接

Book Replacement

The deadline of Prof. Hachioji’s assignment is tomorrow. To complete the task, students have to copy pages of many reference books in the library.

All the reference books are in a storeroom and only the librarian is allowed to enter it. To obtain a copy of a reference book’s page, a student should ask the librarian to make it. The librarian brings books out of the storeroom and makes page copies according to the requests. The overall situation is shown in Figure 1.

Students queue up in front of the counter. Only a single book can be requested at a time. If a student has more requests, the student goes to the end of the queue after the request has been served.

In the storeroom, there are m desks D1, ... , Dm, and a shelf. They are placed in a line in this order, from the door to the back of the room. Up to c books can be put on each of the desks. If a student requests a book, the librarian enters the storeroom and looks for it on D1, ... , Dm in this order, and then on the shelf. After finding the book, the librarian takes it and gives a copy of a page to the student.

技术分享

Then the librarian returns to the storeroom with the requested book, to put it on D1 according to the following procedure.

  • If D1 is not full (in other words, the number of books on D1 < c), the librarian puts the requested book there.
  • If D1 is full, the librarian
    • temporarily puts the requested book on the non-full desk closest to the entrance or, in case all the desks are full, on the shelf,
    • finds the book on D1 that has not been requested for the longest time (i.e. the least recently used book) and takes it,
    • puts it on the non-full desk (except D1 ) closest to the entrance or, in case all the desks except D1 are full, on the shelf,
    • takes the requested book from the temporary place,
    • and finally puts it on D1 .

Your task is to write a program which simulates the behaviors of the students and the librarian, and evaluates the total cost of the overall process. Costs are associated with accessing a desk or the shelf, that is, putting/taking a book on/from it in the description above. The cost of an access is i for desk Di and m + 1 for the shelf. That is, an access to D1, ... , Dm , and the shelf costs 1, ... , m, and m + 1, respectively. Costs of other actions are ignored.

Initially, no books are put on desks. No new students appear after opening the library.

Input

The input consists of multiple datasets. The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

The format of each dataset is as follows.

      m c n
      k1
      b11 . . . b1k1
      .
      .
      .
      kn
      bn1 . . . bnkn

Here, all data items are positive integers. m is the number of desks not exceeding 10. c is the number of books allowed to put on a desk, which does not exceed 30. n is the number of students not exceeding 100. kiis the number of books requested by the i-th student, which does not exceed 50. bij is the ID number of the book requested by the i-th student on the j-th turn. No two books have the same ID number. Note that a student may request the same book more than once. bij is less than 100.

Here we show you an example of cost calculation for the following dataset.

3 1 2
3
60 61 62
2
70 60

In this dataset, there are 3 desks (D1D2D3 ). At most 1 book can be put on each desk. The number of students is 2. The first student requests 3 books of which IDs are 60, 61, and 62, respectively, and the second student 2 books of which IDs are 70 and 60, respectively.

The calculation of the cost for this dataset is done as follows. First, for the first request of the first student, the librarian takes the book 60 from the shelf and puts it on D1 and the first student goes to the end of the queue, costing 5. Next, for the first request of the second student, the librarian takes the book 70 from the shelf, puts it on D2, moves the book 60 from D1 to D3 , and finally moves the book 70 from D2 to D1 , costing 13. Similarly, the cost for the books 61, 60, and 62, are calculated as 14, 12, 14, respectively. Therefore, the total cost is 58.

Output

For each dataset, output the total cost of processing all the requests, in a separate line.

Sample Input

2 1 1
1
50
2 1 2
1
50
1
60
2 1 2
2
60 61
1
70
4 2 3
3
60 61 62
1
70
2
80 81
3 1 2
3
60 61 62
2
70 60
1 2 5
2
87 95
3
96 71 35
2
68 2
3
3 18 93
2
57 2
2 2 1
5
1 2 1 3 1
0 0 0

Output for the Sample Input

4
16
28
68
58
98
23

Source: ACM International Collegiate Programming Contest , Asia Regional Tokyo, Tokyo, Japan, 2005-11-04 
http://www2.teu.ac.jp/icpc/

题意:

第一行m c n

表示有m个小桌子,编号从D1-Dm,如图所示, 图书柜编号为Dm+1

小桌子每桌至多能放c本书。

有n个学生。

下面n*2行,每两行表示一个学生。

第一行表示这个学生需要打印书的数量。第二行给出要打印哪些书。

开始学生按1-n排好队,1先服务

每次学生打印完一本书如果还要再打印就会排到队伍最后

管理员若从第Di 拿起书或放入书花费是i,寻找不需要花费

服务一个学生,管理员会从D1开始找,一直找到Dm+1,直到找到这本书。

把这本书给学生打印,打印完了归还这本书book:

1、若D1是空的就把book放在D1,

2、否则则先把book放在离门最近的不满的桌子上(若桌上都满了则放Dm+1里)

把D1中放在最底下的书(即最久未使用)拿出来,若从第Di 拿起书或放入书花费是i
放在离门最近的不满的桌子上(若桌上都满了则放Dm+1里)

再把book拿出来放在D1上。

==看这题的应该是多是来看题意的

剩下就是模拟了。。


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if(c=getchar(),c==EOF) return 0;
	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
	sgn=(c=='-')?-1:1;
	ret=(c=='-')?0:(c-'0');
	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
	ret*=sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
    if (x <0) {
        putchar('-');
        x = -x;
    }
    if(x>9) pt(x/10);
    putchar(x%10+'0');
}
using namespace std;
const int inf = (int)1e8;
const int N = 55;
typedef pair<int, int> pii;

int n, m, c;

deque<int> Q, q[15];
deque<int>::iterator it;
vector<int>G[105];
int id[105];
void input(){
	while(!Q.empty())Q.pop_front();
	for(int i = 1, num, j; i <= n; i++){
		G[i].clear();
		rd(num); while(num--){rd(j); G[i].push_back(j);	}
		reverse(G[i].begin(), G[i].end());
		if(G[i].size())Q.push_back(i);
	}
	for(int i = 1; i <= m; i++) while(!q[i].empty())q[i].pop_front();
	for(int i = 1; i <= 100; i++)id[i] = m+1;
}
int putbook(int book, int from){
	for(int i = from; i <= m; i++){
		if(q[i].size()<c){
			q[i].push_back(book);
			id[book] = i;
			return i;
		}
	}
	id[book] = m+1;
	return m+1;
}
int takebook(int book){
	if(id[book] == m+1)return m+1;
	for(it = q[id[book]].begin(); it != q[id[book]].end(); it++)
		if(*it == book){
			q[id[book]].erase(it); break;
		}
	return id[book];
}
int main() {
	while(~scanf("%d %d %d", &m, &c, &n), n + c+m){
		input();
		int ans = 0;
		while(!Q.empty()){
			int stu = Q.front();	Q.pop_front();
			int book = G[stu][G[stu].size()-1];
			G[stu].erase(G[stu].end()-1, G[stu].end());
			if(G[stu].size()) Q.push_back(stu);
			ans += takebook(book);
			ans += putbook(book, 1);
			if(id[book]!=1){
				int pre = q[1].front();
				q[1].pop_front();
				ans++;
				ans += putbook(pre, 2);
				ans += takebook(book);
				ans += putbook(book, 1);
			}
		}
		pt(ans); puts("");
	}
    return 0;
}


若从第Di 拿起书或放入书花费是i

Aizu 1258 Book Replacement deque+模拟

标签:

原文地址:http://blog.csdn.net/qq574857122/article/details/44779249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!