2021.7.22--牛客补题

F Musical Chairs

题目内容:

链接:https://ac.nowcoder.com/acm/contest/18453/F
来源:牛客网

Professor O’Dagio of the music department at Faber College has come up with a rather interesting way of selecting its department chair. All n members of the music faculty line up, then the first one in line calls out an integer k corresponding to the opus number of his or her favorite musical composition by Faber College’s most illustrious alumnus, composer I. M. Tondeff. The department members then “count off,” starting with the first in line and cycling back to the beginning of the line if necessary. When the count reaches k, that person steps out of the line and is relieved (in more than one sense!) of chairmanship duty for that year. 
The next person in line then calls out his or her favorite opus number (this becomes the new value of k) and the count restarts at “1,” and continues until the next person is eliminated, and so on. When only one faculty member is left standing, this is the new department chair. To prevent cheating, everyone’s favorite number is announced in advance and no one is allowed to choose Tondeff’s Opus 1 (the famous drinking song Rhapsody in Brew). 
For instance, suppose the professors are numbered 1 through 4 and lined up in that order; suppose their favorite opus numbers are, respectively, opus 8 (The Four Sneezings), opus 2 (Concerto for Kazoo and Cigar Box Banjo), opus 4 (The Taekwondo Rondo), and opus 2 (again). Figure F.1 shows the process by which the new chair is selected.

Figure F.1: Example of Selection Process
输入描述:

The first line of input contains an integer n (2 ≤ n ≤ 104 ), the number of faculty members. The next line contains n integers k1 . . . kn (2 ≤ ki ≤ 106 for each i), where ki is the favorite opus number of professor i.

输出描述:

Output the number of the new department chair.

示例1
输入
复制

4
8 2 4 2

输出
复制

3
View Code

题意:n个人排成一列,第一个人说出他喜欢的作品编号,从排队的第一个开始计数,当计数达到编号k时,这个人就退出队伍,然后在该人后面的人再说出他喜欢的编号,计数从 1 开始,一直持续到下一个淘汰的人,以此类推,当剩下一个人的时候,输出他在队伍中的序号,必要时循环回到队伍开头。

思路:约瑟夫环问题。利用vector< pair<int,int> >mp保存编号以及下标位置,

第一个人说出他的编号a=k并从1开始计数,第(a-1)%n个人会被淘汰,对应就是第 (a-1)%n个人出局,然后就有n-1个人继续;

出局人的下一位t+1喊出编号a=mp[(t+1)%n], 队列为

t+1, .....n, 1, .....,t-1===》1,......,t-1, ,t+1,.....,n ==>t+1前面有(t--)个人,所以 第(t+a)%(n-1)的人出

局 ,以此类推。。。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector< pair<int,int> >mp;
    int x;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        mp.push_back({i,x});
    }
    int t=-1,a=mp[0].second;

     while(n>1)
    {
        t=(t+a)%n;
        a=mp[(t+1)%n].second;
        //cout<<t<<" "<<(t+1)%n<<" "<<a<<" "<<mp[t]<<" ";
        mp.erase(mp.begin()+t);
        n--;
        t--;
    }
    cout<<mp[0].first+1<<endl;
}
View Code

 

posted @ 2021-07-23 16:12  西瓜0  阅读(104)  评论(0)    收藏  举报