3.14 周六下午--代码量训练2--复杂模拟

· · 个人记录

T735987 计算题

思路:直接输入三个数字两个字符验证就好

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int a,b,c;
        char s,d;
        cin>>a>>s>>b>>d>>c;
        if(a+b==c)cout<<"Right!\n";
        else cout<<"Wrong!\n";  
    }
    return 0;
}

P14358 [CSP-J 2025] 座位

思路:这道题是跟据每个人的成绩,进行座位蛇形排列可以根据蛇形的特征进行判断,就可以解决了。

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[1005];
int s[105][105];
int x;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n*m;i++)cin>>a[i];
    x=a[1];
    sort(a+1,a+1+n*m,cmp);

    int s1=1,s2=1;//行,列 
    for(int i=1;i<=n*m;i++)
    {
        if(a[i]==x)cout<<s2<<" "<<s1;
        s[s1][s2]=a[i];
        if((s2%2==1&&s1==n)||(s2%2==0&&s1==1))
        {
            s2++; 
            continue;
        } 
        if(s2%2==0)s1--;
        if(s2%2==1)s1++;
    }
    return 0;
}

T735965 愤怒的程序员

思路:这道题讲了一些程序员根据能力值与矛盾关系,来作为令一个的导师,为了满足时间复杂度为O(n),所以要用模拟+二分+递归来查找,就好了。

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int n,k;
int a[N],b[N],ans[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++)
    {
        ans[i]=lower_bound(b+1,b+1+n,a[i])-b-1;
    }
    while(k--)
    {
        int x,y;
        cin >> x >> y;
        if(a[x]>a[y])
        {
            ans[x]--;
        }
        if(a[x]<a[y])
        {
            ans[y]--;
        }
    }
    for(int i=1;i<=n;i++)
    {
        cout << ans[i]<<" ";
    }
    return 0;
}

T735969 选队员

这道题是要判断学生根据能力值选择第一个队伍或第二个,然后输出1或2。

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int pre[N],nxt[N],a[N],pos[N],ans[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin >> n >> k;
    for(int i=1;i<=n;i++)
    {
        cin >> a[i];
        pos[a[i]]=i;
        pre[i]=i-1;
        nxt[i]=i+1;
    }
    pre[1]=0;
    nxt[n]=n+1;
    int team=1;
    for(int i=n;i>=1;i--)
    {
        int temp=pos[i];
        if(ans[temp]!=0)
        {
            continue;
        }
        ans[temp]=team;
        int l=temp,r=temp;
        for(int i=0;i<k;i++)
        {
            if(pre[l]!=0)
            {
                l=pre[l];
                ans[l]=team;
            }
            else
            {
                break;
            }
        }
        for(int i=0;i<k;i++)
        {
            if(nxt[r]!=n+1)
            {
                r=nxt[r];
                ans[r]=team;
            }
            else
            {
                break;
            }
        }
        nxt[pre[l]]=nxt[r];
        pre[nxt[r]]=pre[l];
        team=3-team;
    }
    for(int i=1;i<=n;i++)
    {
        cout << ans[i];
    }
    return 0;
}

T735985 书架

思路:这道题讲了,对书架进行操作,用l和r模拟,输出则输出min(pos[id] - L, R - pos[id]),就好了。

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 2e5 + 10;
int pos[N];
int main() {
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int q;
    cin >> q;
    int L = 0, R = 0;
    bool first = true;
    while (q--) {
        char op;
        int id;
        cin >> op >> id;
        if (op == 'L') {
            if (first) {
                pos[id] = 0;
                L = 0;
                R = 0;
                first = false;
            } else {
                pos[id] = L - 1;
                L = pos[id];
            }
        } else if (op == 'R') {
            if (first) {
                pos[id] = 0;
                L = 0;
                R = 0;
                first = false;
            } else {
                pos[id] = R + 1;
                R = pos[id];
            }
        } else if (op == '?') 
        {
            cout << min(pos[id] - L, R - pos[id]) << endl;
        }
    }
return 0;
}