8.22训练课

· · 个人记录

未完成的无错因

题目 分数
T1 未完成
T2 未完成
T3 未完成
T4 100
T5 100
T6 未完成
T7 30

T1 [CCC 2025 Senior] 挂画 / Positioning Peter's Paintings

无思路

题意:

有两幅画,一幅画的长为A,宽为B;另一幅长为X,宽为Y,要求出这两幅画满足以下条件周长最小值:

这两幅画必须全部在画框中;

这两幅画不能重叠(但是允许边缘触碰);

这两幅画不能旋转;

这两幅画边缘必须与地面平行,或者垂直于地面。

思路:

两幅画要摆放进画框里,要么是一个在上,一个在下;要么是一个在左,一个在右,如果是求周长的话:左右结构的就是:宽相加+两个长里长的那一个;上下结构的就是:长相加+两个宽里长的宽

错误代码:

无

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int a,b,x,y;
    cin >> a >> b >> x >> y;
    int s1 = (b+y) + max(a,x);//上下结构
    int s2 = (a+x) + max(b,y);//左右结构
    cout << min(s1,s2) * 2;
    return 0;
}

T2 [语言月赛202208] 俊俏地鼠的远亲

无思路

题意:

给出一个n*m的矩阵,每个位置上有一个数字 b_i,b_j,若bxb_,y = b_i,b_j,则称(x,y)是 (i,j)的亲戚,他们的距离为(x−i)^2+(y−j)^2

对于每个位置,求亲戚的最远距离

思路:

我们可以枚举位置(i,j),再枚举位置 (x,y),如果它们数字相等,则更新位置(i,j)上的答案

错误代码:

无

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
int s[55][55];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= m ; j++){
            cin >> s[i][j];
        }
    }
    //枚举现在位置
    for(int i = 1 ; i <= n ; i++){
        for(int j = 1 ; j <= m ; j++){
            int maxx = 0;
            //枚举其他人的位置
            for(int a = 1 ; a <= n ; a++){
                for(int b = 1 ; b <= m ; b++){
                    if(s[i][j] == s[a][b]){//只要满足条件
                        maxx = max(maxx,(i - a) * (i - a) + (b - j) * (b - j));
                    }    
                }
            }
            cout << maxx << " ";
        }
        cout << "\n";    
    }
    return 0;
}

T3 尼古拉斯的画框

无思路

题意:

有N个长度为a_1,a_2,a_3......a_n的木棍,不可以折断和拼接,需要把他们平成一个画框(4根为一组)

思路:

这里需要统计每个长度的木棍出现了多少次,再拼成画框,所以统计出现次数要么用桶,要么用map,但🪣已经够用了,先用桶存出现次数,再去遍历/2,因为两个为一组

错误代码:

无

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
int tong[N];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i = 1 ; i <= n ; i++){
        int x;
        cin >> x;
        tong[x]++;
    }
    int ans = 0;
    for(int i = 1 ; i <= 1e6 ; i++){//遍历桶
        ans += tong[i]/2;//两个为一组
    }
    cout << ans / 2;//最后四个为一组,但是前面除了2
    return 0;
}

T6 [蓝桥杯 2016 国 C] 赢球票

无思路

题意:

给出N个数,按从1到N的顺序数,若数到的数与当前的数匹配,则拿走与当前数相同数量的球票,如果有多种情况,输出赢得球票数最多的情况

样例1

我找的

思路:

可以先用一个队列存进去,接着把队列转换为环形,去遍历看最后能拿都多少彩票?

可以在遍历里先增加报数,再看报的数是否跟卡牌的数一样

错误代码:

无

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
queue<int>q;
int n;
int f(queue<int>q){
    int dq = 0;//当前报数的数字
    int s = 0;//拿走得票数
    while(q.size() != 0 && dq <= n){
        dq++;
        if(dq == q.front()){
            s += q.front();
            q.pop();
            dq = 0;
        }else{
            q.push(q.front());
            q.pop();
        }
    }
    return s;
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n;
    for(int i = 1 ;i <= n ; i++){
        int x;
        cin >> x;
        q.push(x);
    }
    int maxx = 0;
    for(int i = 1 ; i <= n ; i++){
        maxx = max(maxx,f(q));//最大值
        q.push(q.front());//成环
        q.pop();
    }
    cout << maxx;
    return 0;
}

T7 [NOIP 2010 提高组] 机器翻译

有思路

题意:

题目讲的是有一个内存为M的字典,要把N个单词存入字典。如果内存不够了的话,把最先存入的单词清除,再把新的单词存入字典

思路:

先把单词都从队尾存入,存一个标记一下,计数器加一,如果满了,就从对头清除,标记清除,再把单词从队尾加入

错误代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
queue<int>q;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int m,n;
    cin >> m >> n;
    int cx = 0;
    for(int i = 1 ; i <= n ; i++){
        int x,f = 1;
        cin >> x;
        if(q.size() > m){
            q.pop();
        }
        for(int i = 1 ; i <= q.size() ; i++){
            if(q.front() == x){
                f = 0;
            }else{
                q.push(q.front());
                q.pop();
            }   
        }
        if(f == 1){
            q.push(x);
            cx++;  
        }

    }
    cout << cx;
    return 0;
}

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
queue<int>q;
int tong[1005];
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int m,n;
    cin >> m >> n;
    int cx = 0;
    for(int i = 1 ; i <= n ; i++){
        int x;
        cin >> x;
        if(tong[x] == 0){//没有出现过
            if(q.size() >= m){//内存不够
                tong[q.front()] = 0;
                q.pop();
            }
            q.push(x);
            tong[x] = 1;//记录
            cx++;
        }
    }
    cout << cx;
    return 0;
}

错因:

没有用桶标记

感谢观看