URAL1795. Husband in a Shop(模拟,首次探访俄罗斯的oj)

1795. Husband in a Shop

Time limit: 1.0 second

Memory limit: 64 MB

Mark comes home after a day of hard work. His wife, instead of feeding him, sends him to buy some bread. When Mark comes to a local shop, there is a long line of equally unhappy husbands. Mark joins
the line. Fortunately, the line is moving rather quickly because each husband is asked to buy only one kind of a product.

When each husband comes to the counter, he asks for some amount of the product he needs to buy. If this amount of the product is available, he buys it and leaves the shop. If he is told that this product
is unavailable, he gets terribly upset and also leaves the shop. If the available amount of the product is less than he needs, he doesn‘t know what to do and calls his wife for advice. In order to make a call without delaying the line, he lets one person come
forward and calls his wife while standing second in line. The wife tell him to buy all the remaining products. After that he comes to the counter once again and, if the amount of the product is still the same, buys it. Otherwise, he lets one more person come
forward and calls his wife again. If it happens so that the product is no longer available after a call, the customer leaves the shop empty-handed.

Mark is eager to return home and have dinner at last. Your task is to determine how many minutes will pass before he gets to the counter for the first time. Each customer spends exactly one minute at
the counter and has enough time to call his wife if necessary while the following in line customer speaks to the shopgirl.

Input

The first line contains the number m of different products sold at the shop (1 ≤ m ≤ 1000). Each of the following m lines
describes one product. The descriptions have the form “amount of name”, where name is the name of the product (a nonempty string of length at most 20 consisting of lowercase English letters) and amount is
the amount of units of this product left by the moment Mark entered the shop (it is a positive integer not exceeding 1000). All the products have distinct names.

In the following line you are given the number n of customers standing in the line before Mark (1 ≤n ≤ 1000). The following n lines
describe the products these customers want to buy in the same format in which the products in the shop are described. There can be more than one customer who want to buy the same product. Customers may want to buy a product that is not available at the shop.
The information on the customers is given in the order from the beginning of the line to its end.

Output

Output the number of minutes that Mark will stand in line before he gets to the counter for the first time.

Sample

input output
3
2 of sweets
4 of milk
1 of sausage
4
2 of milk
3 of sweets
3 of milk
1 of cheese
6

Problem Author: Andrew Gein

Problem Source: Ural Regional School Programming Contest 2010

Tags: none  (

hide tags for unsolved problems
)

周末的比赛拉题拉到这里了,特地去查了下Timus是什么。结果如下:

TIMUS在线评测系统是俄罗斯最大的Online Judge。问题大多来自于乌拉尔联邦大学,乌拉尔锦标赛,乌拉尔ACM ICPC竞赛和彼得罗扎沃茨克训练营举行的比赛。

这题就是模拟,就是模拟。注意下结束条件,其实也没什么难的,但自己还是浪费了时间在这题,读题要认真啊,别看一半就做。

#include<iostream>
#include<deque>
#include<map>
#include<cstring>
#include<string>
#include<utility>
using namespace std;
struct C
{
	string s;
	int nu;
};
int main()
{
	int n, m;
	while (cin >> n)
	{
		int num;
		string em;
		string name;
		map<string, int>shop;
		deque<C>que;
		for (int i = 0; i < n; i++)
		{
			cin >> num >> em >> name;
			shop.insert(pair<string, int>(name, num));
		}
		cin >> m;
		C c;
		for (int i = 0; i < m; i++)
		{
			cin >> num >> em >> name;
			c.s = name;
			c.nu = num;
			que.push_back(c);
		}
		C d;
		int t = 0;
		while (!que.empty())
		{
			t++;
			c = que.front();
			if (que.size() == 1)
			{
				break;
			}
			else if (shop[c.s] == 0)
			{
				que.pop_front();
			}
			else if (c.nu > shop[c.s])
			{
				c.nu = shop[c.s];
				que.pop_front();
				d = que.front();
				que.pop_front();
				que.push_front(c);
				que.push_front(d);
			}
			else
			{
				shop[c.s] = shop[c.s] - c.nu;
				que.pop_front();
			}
		}
		cout << t << endl;
	}
}
时间: 2024-10-08 20:24:47

URAL1795. Husband in a Shop(模拟,首次探访俄罗斯的oj)的相关文章

hdu4884TIANKENG’s rice shop(模拟)

题目:hdu4884TIANKENG's rice shop(模拟) 题目大意:一家餐厅买炒饭,给你客人来的时间和要买的炒饭的数量种类,并且也知道炒一次炒饭最多的分数和时间,问这些客人最早的离开时间.注意:每次炒饭的那段时间是不接单的. 解题思路:模拟.题意好坑,还有根本不会有23:59 , 00:00这样的输入数据,只是输出可能会跨一天.然后就是模拟了.好恶心的题目.详细的看注释把. 代码: #include <cstdio> #include <cstring> #includ

HDU TIANKENG’s rice shop(模拟)

HDU 4884 TIANKENG's rice shop 题目链接 题意:模拟题.转一篇题意 思路:就模拟就可以.注意每次炒完之后就能够接单 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1005; int T, n, t, k, m; struct Person { int t, p, num, ans; } p[N];

[ACM] HDU 4884 TIANKENG’s rice shop (模拟)

TIANKENG's rice shop Problem Description TIANKENG managers a pan fried rice shop. There are n kinds of fried rice numbered 1-n. TIANKENG will spend t time for once frying. Because the pan is so small, TIANKENG can fry k bowls of fried rice with same

hdu 4884 TIANKENG’s rice shop(模拟)

# include <cstdio> # include <algorithm> # include <cstring> # include <cstdlib> using namespace std; int max(int a,int b) { return a>b?a:b; } int main() { int T,n,t,k,m,i,hh,min,id,num,x; int last[1010];//最后一次開始炒饭的时间 int cot[10

iOS 【Multithreading-多图下载数据展示案例(二级缓存)/模拟SDWebImage内部实现】

#import "ViewController.h" #import "WZYApp.h" @interface ViewController () // 数据模型数组 @property (nonatomic, strong) NSArray *apps; // 保存操作对象的字典 @property (nonatomic, strong) NSMutableDictionary *operations; // 内存缓存 @property (nonatomic,

动态分区分配

一.目的 加深对动态分区分配的理解,进一步掌握首次适应算法和最佳适应算法的理解.了解动态分区分配方式中使用的数据结构和分配算法,进一步加深对动态分区存储管理方式及其实现过程的理解.提高学生设计实验.发现问题.分析问题和解决问题的能力. 学会可变式分区管理的原理是在处理作业过程中建立分区,使分区大小正好适合作业的需求. 当一个作业执行完成后,作业所占的分区应归还给系统. 二.原理 首次适应算法 以空闲分区链为例来说明采用FF算法时的分配情况.FF算法要求空闲分区链以地址递增的次序链接.在分配内存时

spring实现读写分离

(转自:http://www.cnblogs.com/surge/p/3582248.html) 现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应用中,数据库都是读多写少(读取数据的频率高,更新数据的频率相对较少),而读取数据通常耗时比较长,占用数据库服务器的CPU较多,从而影响用户体验.我们通常的做法就是把查询从主库中抽取出来,采用多个从库,使

Spring 实现数据库读写分离

现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应用中,数据库都是读多写少(读取数据的频率高,更新数据的频率相对较少),而读取数据通常耗时比较长,占用数据库服务器的CPU较多,从而影响用户体验.我们通常的做法就是把查询从主库中抽取出来,采用多个从库,使用负载均衡,减轻每个从库的查询压力. 采用读写分离技术的目标:有效减轻Master库的压力,又可以

Spring配置多个数据源

Spring 配置多数据源实现数据库读写分离 博客分类: Spring 数据库 现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应用中,数据库都是读多写少(读取数据的频率高,更新数据的频率相对较少),而读取数据通常耗时比较长,占用数据库服务器的CPU较多,从而影响用户体验.我们通常的做法就是把查询从主库中抽取出来,采用多个从库,使用负载均衡,减轻每