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; } }