原题链接:
https://open.kattis.com/problems/musicalchairs
https://vjudge.net/problem/Kattis-musicalchairs
AC代码:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
const int INF = 0x3fffffff;
typedef long long ll;
typedef unsigned long long ull;
void __init__()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
}
struct P
{
int num;
int m;
};
vector<P> v;
int main()
{
// __init__();
int n;
cin >> n;
P t;
for (int i = 1; i <= n; i++)
{
int tt;
cin >> tt;
t.m = tt;
t.num = i;
v.push_back(t);
}
int pos = 0;
int cnt = n;
for (int i = 0; i < n; i++)
{
pos = (pos + v[pos].m - 1) % cnt;
vector<P>::iterator it = v.begin() + pos;
v.erase(it);
if (v.size() == 1)
{
break;
}
if (pos == cnt - 1)
{
pos = 0;
}
cnt--;
}
cout << v[0].num << endl;
return 0;
}
类约瑟夫环的签到题