UVa 402 M*A*S*H (STL&list)
2013-11-03 23:26
309 查看
402 - M*A*S*H
Time limit: 3.000 secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=343
Corporal Klinger is a member of the 4077th Mobile Army Surgical Hospital in the Korean War; and he will do just about anything to get out. The U.S. Army has made an offer for a lottery that will choose some number of lucky people (X) to return to
the states for a recruiting tour. Klinger needs your help getting out.
The lottery is run by lining up all the members of the unit at attention and eliminating members by counting off the members from 1 toN where
N is a number chosen by pulling cards off of the top of a deck. Every timeN is reached, that person falls out of the line, and counting begins again at 1 with the next person in line. When the end of the line has been reached (with whatever
number that may be), the next card on the top of the deck will be taken, and counting starts again at 1 with the first person in the remaining line. The last
X people in line get to go home.
Klinger has found a way to trade a stacked deck with the real deck just before the selection process begins. However, he will not know how many people will show up for the selection until the last minute. Your job is to write a program that will use the
deck Klinger supplies and the number of people in line that he counts just before the selection process begins and tell him what position(s) in the line to get in to assure himself of a trip home.
A simple example with 10 people, 2 lucky spots, and the numbers from cards 3, 5, 4, 3, 2 would show that Klinger should get in positions 1 or 8 to go home.
Input
For each selection, you will be given a line of 22 integers. The first integer () tells how many people will participate in the
lottery. The second integer (
) is how many lucky "home" positions will be selected. The next 20 integers are the values of the
first 20 cards in the deck. Card values are interpretted to integer values between 1 and 11 inclusive.
Output
For each input line, you are to print the message ``Selection #A" on a line by itself whereA is the number of the selection starting with 1 at the top of the input file. The next line will contain a list of ``lucky" positions that Klingershould attempt to get into. If Klinger's deck doesn't get the job done by the time the 20th card is used, print all remaining people in line. The list of ``lucky" positions is then followed by a blank line.
Sample Input
10 2 3 5 4 3 2 9 6 10 10 6 2 6 7 3 4 7 4 5 3 2 47 6 11 2 7 3 4 8 5 10 7 8 3 7 4 2 3 9 10 2 5 3
Sample Output
Selection #1 1 8 Selection #2 1 3 16 23 31 47
直接用list模拟。
完整代码:
/*0.156s*/ #include<cstdio> #include<list> using namespace std; #define sf scanf #define pf printf const int maxn = 20; int card[maxn], x; list<int> li; list<int>::iterator iter; void solve() { int i, j; for (i = 0; i < maxn; ++i) { for (iter = li.begin(), j = 1; iter != li.end(); ++j) { if (j == card[i]) { if (li.size() == x) return; iter = li.erase(iter); ///erase之后,指向被删除节点的迭代器会失效 ///erase(iterator position)返回指向所删除节点的下一个位置的迭代器(若删除的是最后一个节点则返回end迭代器) j = 0; } else ++iter; } } } int main() { int n, i, cas = 0; while (~sf("%d%d", &n, &x)) { li.clear(); for (i = 1; i <= n; ++i) li.push_back(i); for (i = 0; i < maxn; ++i) sf("%d", &card[i]); solve(); pf("Selection #%d\n", ++cas); for (iter = li.begin(), i = 0; i < li.size() - 1; ++i) pf("%d ", *iter++); pf("%d\n\n", *iter); } return 0; }
相关文章推荐
- UVa 540 Team Queue (STL list&queue模拟插队)
- STL list "list iterator not incrementable"
- UVA 127 - "Accordian" Patience---用STL做的(TLE超时)
- STL-容器间的复制list&vector
- UVALive 7146-贪心&数据结构stl-Defeat the Enemy
- UVa 506 - System Dependencies <图的拓扑序+STL应用>
- UVA 10815 Andy's First Dictionary 安迪的字典 stl应用 set+string做法
- 【留坑】UVA246 模拟题,STL容器组合_deque&&(set判重 OR hash判重)
- UVa 156 Ananagrams (用STL multimap&set处理字典中的重复元素)
- 网易2009年校园招聘--一道关于<list>的STL题目
- UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
- Brute Force & STL --- UVA 146 ID Codes
- (使用STL自带的排序功能进行排序7.3.12)UVA 11057 Exact Sum(在一组数据中找到这么两个数,使得他们的和等于指定数&&且这两个数之间的差值最小)
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- STL-容器间的复制list&vector
- UVA 10815 Andy's First Dictionary(STL)
- UVA 10815-Andy's First Dictionary(字符串模拟+排序+重复删除||STL-set)
- UVa 496 Simply Subsets (STL&set_intersection)
- STL(4)----<list>
- UVa 10282 / POJ 2503 Babelfish / ZOJ 1109 Language of FatMouse (STL&map)