曾源翊的题解

· · 个人记录

T628211 报数

错误原因

没有思路

正确思路

思路批注在了代码中

修改后的代码

#include<bits/stdc++.h>
using namespace std;
int main() 
{
    int n,m;
    cin>>n>>m;
    vector<int>people(n);
    for (int i = 0; i < n; i++) 
    {
        people[i] = i + 1;  // 初始化人员编号
    }
    vector<int> result;
    int pos = 0;  // 当前位置
    while (!people.empty()) 
    {
        // 计算要移除的位置
        pos = (pos + m - 1) % people.size();
        // 将该位置的人加入结果
        result.push_back(people[pos]);
        // 移除该位置的人
        people.erase(people.begin() + pos);
    }

    // 输出结果
    for (int i = 0; i < result.size(); i++) 
    {
        cout << result[i];
        if (i != result.size() - 1) 
        {
            cout << " ";
        }
    }
    cout << endl;

    return 0;
}

T628213 值轮兵团站岗

错误原因

只想到了用数组的方法

正确思路

批注在了代码中

修改后的代码

 #include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
long long n,m,k;
long long a[N],head=0, tail=-1;
long long opt,x,w;
int main()
{
    cin>>n>>m>>k;
    //入队 
    for(int i=1;i<=n;i++)
    {
        cin>>w;
        a[++tail]=w;//0 1 2
    }
    sort(a+head,a+tail+1);//a+0  a+2
    //m次操作 
    long long t=0;
    while(m--)//3e5*3e5=9e10
    {
        cin>>opt;
        if(opt==1)//向正方向移动  a[i]+x
        {
            cin>>x;
            t+=x; 
            while(a[tail]+t>k&&tail-head>-1)// 8 8 7 6 5 4
            {
                tail--;
            }
        }
        if(opt==2)//向反方向移动 a[i]-x
        {
            cin>>x;
            t-=x;
            while(a[head]+t<-k&&tail-head>-1)//8 1 2 3 4
            {
                head++;
            }
        }
        if(opt==3)//输出队伍人数<<endl; 
        {
            cout<<tail-head+1<<endl; 
        }
    }
    return 0;
}