跳到正文

个人记录

入门二阶题解-代码

wuweize150815约 10 分钟
原文

这是 2026年7月2日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年7月2日
更新
2026年7月8日
篇幅
约 10 分钟

版本

  1. v2当前
  2. v1

本题解代码100%由wuweize150815wuweize150815 (即:伍位泽)编写,仅供大家参考

最好不要直接:(Ctrl+aCtrl+a)+(Ctrl+cCtrl+c)+(Ctrl+vCtrl+v)

友情提示:如果直接抄袭,可能会被TLTL__(即汤老师/汤磊)…

开始正题:

A题:

C++
#include <bits/stdc++.h>
using namespace std;
queue <int> q;
int n , v;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    while (n--)
    {
        cin >> v;
        q.push (v);
    }
    while (!(q.empty ( )))
    {
        int p=q.front ( );
        q.pop ( );
        cout << p << "\n";
        if (p%2==1)
        {
            q.push (q.front ( ));
            q.pop ( );
        }
        else
        {
            q.push (q.front ( ));
            q.pop ( );
            q.push (q.front ( ));
            q.pop ( );
        }
    }
    return 0;
}

B题:

C++
#include <bits/stdc++.h>
using namespace std;
deque <string> q;
string s;
int n , k;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    while (n--)
    {
        cin >> k;
        if (k==1)
        {
            cin >> s;
            q.push_front (s);
        }
        else
        {
            cout << q.front ( ) << "\n";
            q.pop_front ( );
        }
    }
    return 0;
}

C题:

C++
#include <bits/stdc++.h>
using namespace std;
int a[100005] , b[100005];
int n , m , cnt=0;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1;i<=n;i++) cin >> a[i];
    for (int i=1;i<=m;i++) cin >> b[i];
    sort (a+1 , a+n+1);
    sort (b+1 , b+m+1);
    for (int i=1;i<=n;i++)
    {
        int pos=lower_bound (b+1 , b+m+1 , a[i])-b;
        if (b[pos]==a[i]) cnt++;
    }
    cout << cnt;
    return 0;
}

D题:

C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int id , w;
    bool operator < (const node &a) const
    {
        if (w!=a.w) return w<a.w;
        return id<a.id;
    }
};
priority_queue <node> q;
int n , k , s;
node v;
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i=1;i<=n;i++)
    {
        cin >> s;
        q.push ({i , s});
        if (i>k)
        {
            if (q.top ( ).id<=i-k)
            {
                while (q.top ( ).id<=i-k)
                {
                    q.pop ( );
                }
                cout << q.top ( ).id << " " << q.top ( ).w << "\n";
            }
            else if (q.top ( ).id==v.id&&q.top ( ).w==v.w) continue;
            else cout << q.top ( ).id << " " << q.top ( ).w << "\n";
        }
        else
        {
            if (q.top ( ).id==v.id&&q.top ( ).w==v.w) continue;
            cout << q.top ( ).id << " " << q.top ( ).w << "\n";
        }
        v=q.top ( );
    }
    return 0;
}

E题:

C++
#include <bits/stdc++.h>
using namespace std;
int v[100005];
int n , m;
bool check (int mid)
{
    int cnt=0;
    for (int i=1;i<=n;i++)
    {
        cnt+=(v[i]+mid-1)/mid;
	}
    return (cnt<=m);
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i=1;i<=n;i++)
    {
        cin >> v[i];
    }
    int l=1 , r=100000000;
    while (l<r)
    {
        int mid=(l+r)/2;
        if (check (mid)==1)
        {
            r=mid;
        }
        else
        {
            l=mid+1;
        }
    }
    cout << l;
    return 0;
}

F题

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int a[100005] , b[100005];
int n , m , k , cnt=0;
signed main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    for (int i=1;i<=n;i++)
    {
        cin >> a[i];
    }
    for (int i=1;i<=m;i++)
    {
        cin >> b[i];
    }
    sort (a+1 , a+n+1);
    sort (b+1 , b+m+1);
    for (int i=1;i<=n;i++)
    {
        int x=lower_bound (b+1 , b+m+1 , a[i]-k)-b-1;
        int y=upper_bound (b+1 , b+m+1 , a[i]+k)-b-1;
        cnt+=y-x;
    }
    cout << cnt;
    return 0;
}

G题:

C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x , y;
};
int dis[1005][1005];
int dx[12]={0 , 0 , 1 , -1 , -2 , -1 , 1 , 2 , 2 , 1 , -1 , -2} , dy[12]={1 , -1 , 0 , 0 , 1 , 2 , 2 , 1 , -1 , -2 , -2 , -1};
int n , m , a , b , c , d;
void bfs ( )
{
    memset (dis , 0x3f , sizeof (dis));
    queue <node> q;
    q.push ({a , b});
    dis[a][b]=0;
    while (!(q.empty ( )))
    {
        node p=q.front ( );
        q.pop ( );
        for (int i=0;i<=11;i++)
        {
            int nx=p.x+dx[i] , ny=p.y+dy[i];
            if (nx<1||nx>n||ny<1||ny>m) continue;
            if (dis[nx][ny]>dis[p.x][p.y]+1)
            {
                dis[nx][ny]=dis[p.x][p.y]+1;
                q.push ({nx , ny});
            }
        }
    }
}
int main ( )
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> a >> b >> c >> d;
    bfs ( );
    cout << dis[c][d];
    return 0;
}