作业-2.25-小六集训队-407

· · 个人记录

T1

错因:
scanf与ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);冲突
很简单。只需要使用scanf格式化读入再判断或者用C++输入整数的特性(以字符为分隔符)输入整数后输入一个char类型即可。

#include<bits/stdc++.h>
using namespace std;
#define int long long   
//const int N=;
int t,a,b,c;
char x;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>t;
    while(t--)
    {
        cin>>a>>x>>b>>x>>c;
        if(a+b==c)
        {
            cout<<"Right!\n";
        }
        else
        {
            cout<<"Wrong!\n";
        }
    }
    return 0;
}
/*

5
1+1=2
1+2=2
15+66=81
5+37=40
99+99=99

*/

T2

本题我的思路是:
排序:从大到小排序(方便求解)
填充:模拟过程,得到矩阵。
在填充时如果遇到了分数为r,输出。
注意,可能还差最后一列未填充。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=15;
int n,m,a[N*N],b[N][N],r;
bool cmp(int x,int y)
{
    return x>y;
}
void f()
{
    int cnt=0;
    for(int i=1;i<=m/2;i++)
    {
        for(int j=1;j<=n;j++)
        {
            b[i*2-1][j]=a[++cnt];
            if(a[cnt]==r)
            {
                cout<<i*2-1<<' '<<j;
                return ;
            }
        }
        for(int j=n;j>=1;j--)
        {
            b[i*2][j]=a[++cnt];
            if(a[cnt]==r)
            {
                cout<<i*2<<' '<<j;
                return ;
            }
        }
    }
    if(m%2==1)
    {
        for(int i=1;i<=n;i++)
        {
            b[m][i]=a[++cnt];
            if(a[cnt]==r)
            {
                cout<<m<<' '<<i;
                return ;
            }
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>m;
    for(int i=1;i<=n*m;i++)
    {
        cin>>a[i];
    }
    r=a[1];
    sort(a+1,a+n*m+1,cmp);
    f();
    return 0;
}
/*

2 2
99 100 97 98

1 2


2 2
98 99 100 97

2 2


3 3
94 95 96 97 98 99 100 93 92

3 1

*/

T3

本题我们可以先sort排序,得到一个可以二分查找的序列,然后二分查找出有多少个比它小的,最后我们减去与他冲突的数量。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,k,x,y,a[N],b[N],sa[N];
bool cmp(int x,int y)
{
    return x>y;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    while(k--)
    {
        cin>>x>>y;
        if(b[x]>b[y])
        {
            sa[x]--;
        }
        if(b[y]>b[x])
        {
            sa[y]--;
        }
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
        int mid=lower_bound(a+1,a+n+1,b[i])-a;
        cout<<mid+sa[i]-1<<' ';
    }
    return 0;
}
/*

4 2
10 4 10 15
1 2
4 3

0 0 1 2


10 4
5 4 1 5 4 3 7 1 2 5
4 6
2 1
10 8
3 5

5 4 0 5 3 3 9 0 2 5

*/

T4

本题思路很简单,只需要使用数据结构“链表”即可。
首先,模拟找最大。
然后,向两边标记,重复
最后输出
本题难在数据结构“链表”以及大量细节

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

T5

本题模拟会有下标为负数的弊端,但是“双端队列”和“数组模拟双端队列”被内存限制2MB卡掉了。那么我们可以储存每一个书的位置,再根据左右端点得到答案,还需要通过0的位置纠偏。

#include<bits/stdc++.h>
using namespace std;
int q,x,l,r,mid,zmid;
char c;
map<int,int>mp;
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin>>q;
    l=1,r=0;
    while(q--)
    {
        cin>>c>>x;
        if(c=='L')
        {
            mp[x]=--l;
            zmid--;
        }
        else if(c=='R')
        {
            mp[x]=++r;
        }
        else
        {
            mid=min(mp[x]-1-zmid,(int)mp.size()-(mp[x]-zmid));
            cout<<mid<<'\n'; 
        }
    }
    return 0;
}
/*

8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1

1
1
2


10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115

0 2 1

*/