题解:CF83B Doctor

· · 题解

题目传送门

题意

给定一个包含 n 个数的序列 a_1,a_2,\dots,a_n,a_i 的编号为 i,注意编号不会因为数字的移动而改变。

现在请你执行 k 次操作,每次操作为取出序列的第一个数并将其值减一,若其值大于 0,则将其放到序列的最尾端,否则将其删除。

求最后的序列剩下的数的编号(若 k 次操作还未执行完,序列已经为空则输出 -1)。

思路

这道题可以用队列做。如果大于 0 的话,将这个数减 1 然后放到这 n 个数的最后面,再进行 k 次操作后输出剩下数。最后需要注意如果在 k 次操作中所有数都没了的话,就跳出操作,输出 −1。

code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,k,x,s;
queue<ll>que;
queue<ll>q;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;++i)
    {
        cin>>x;
        que.push(x);
        q.push(i);
    }
    for(int i=1;i<=k;++i)
    {
        if(que.empty())
        {
            cout<<-1<<endl;
            return 0;
        }
        x=que.front(),s=q.front();
        que.pop(),q.pop();
        if(x-1>0)que.push(x),q.push(s);
    }
    while(!q.empty())
    {
        cout<<q.front()<<" ";
        q.pop();
    }
    return 0;
}