MUSICAL CHAIRS
时间限制: 1 Sec 内存限制: 128 MB
提交: 386 解决: 76
[提交] [状态] [命题人:admin]
题目描述
Musical chairs is a game frequently played at children’s parties. Players are seated in a circle facing outwards. When the music starts, the players have to stand up and move clockwise round the chairs. One chair is removed, and when the music stops the players all have to try to sit down on one of the chairs. The player who does not manage to sit down is out, and the game continues until there is just one player left, who is the winner.
输入
The first line contains a single integer, N which is the number of players (1 < N <= 15).
The next N lines have the names of the players.
The next line contains R, an integer which tells how many rounds are to be processed (0 < R < N). The next R lines each contain a pair of integers S and M, separated by a space. S is the number of the seat to be removed. Seats are numbered from 1 to the number of seats remaining in a clockwise direction.
M is the number of moves made before the music stops (0 < M <= 30). A move takes a player from one seat to the next in a clockwise direction. A move from the highest seat number takes a player to seat 1.
输出
After each round has taken place, output a line
has been eliminated.
where is the name of the person who does not find a seat. This will be the person who would have ended up at the seat which was removed.
At the end of the specified number of moves, output a line which either says
has won.
where a single player remains, or
Players left are .
where the game is not yet finished.
contains the name of each player not yet eliminated in the same order as in the input. The names are separated by spaces.
样例输入
复制样例数据
5
Anne
Bill
Chen
Di
Everet
4
3 6
2 8
1 5
2 6
样例输出
Bill has been eliminated.
Anne has been eliminated.
Chen has been eliminated.
Everet has been eliminated.
Di has won.
题目大意:
先输入一个数字nnn,代表有nnn个人参加游戏,其下nnn行输入每个参与者的名字,从111到nnn依次编号,然后这nnn个人围绕nnn个凳子旋转,每回合将抽掉一个凳子,而这代表着将有一个人被淘汰,在每回合中,所有人绕着凳子顺时针旋转,且每回合都将凳子重新从111开始编号。其后先输入一个数字ttt,代表游戏进行了ttt个回合,对于每回合,输入两个整数rrr和mmm,代表此回合抽掉的凳子编号以及玩家旋转的次数。输出每回合淘汰掉的人,按照最后剩下的人的个数输出不同的格式。
解题思路:
此题知道意思后直接模拟即可,我认为用双端队列比较好模拟。
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
string str[20];
deque<int> q;
int arr[100];
int main()
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);int n;cin>>n;string s;rep(i,1,n) {cin>>s;str[i]=s;}lep(i,n,1)q.push_back(i);int t;cin>>t;int r,m;while(t--) {cin>>r>>m;while(m--) {int t=q.front();q.pop_front();q.push_back(t);}rep(k,1,r-1) {int t=q.back();q.pop_back();q.push_front(t);}int num=q.back();q.pop_back();rep(k,1,r-1) {int t=q.front();q.pop_front();q.push_back(t);}cout<<str[num]<<" has been eliminated."<<endl;}int cnt=0;while(!q.empty()) {arr[++cnt]=q.front();q.pop_front();}sort(arr+1,arr+1+cnt);if(cnt>1) {cout<<"Players left are";rep(i,1,cnt) {cout<<" "<<str[arr[i]];}cout<<"."<<endl;}if(cnt==1) {cout<<str[arr[1]]<<" has won."<<endl;}return 0;
}