NC223614. FMusicalChairs
描述
输入描述
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
Python3 解法, 执行用时: 587ms, 内存消耗: 7400K, 提交时间: 2021-07-22 16:25:20
n = int(input()) proferess=input().split() for x in range(n): proferess[x]=(int(proferess[x]),x+1) while True: if len(proferess)==1: print(proferess[0][1]) break m=proferess[0][0] js=m%len(proferess) if js==0: proferess=proferess[:-1] else: proferess=proferess[js:]+proferess[:js-1]
C++ 解法, 执行用时: 27ms, 内存消耗: 620K, 提交时间: 2022-01-28 15:09:57
#include<bits/stdc++.h> using namespace std; vector<pair<int, int>>v; int n, k; int main() { cin >> n; for(int i = 1; i <= n; i++) { cin >> k; v.push_back({k,i}); } for(int x = n, s = 0, y; x > 1; s = y % --x) { y = (s + v[s].first - 1) % x; v.erase(v.begin()+y); } cout << v[0].second; }