张友帆8月25日总结

· · 个人记录

一、比赛基本情况

比赛日期:8月25日 比赛时长:2小时30分 比赛得分:106分 比赛排名:1

二、比赛整体回顾

简要记录本场比赛的做题顺序、时间安排、比赛状态以及整体感受。 感觉有一点难度。

三、题目复盘

T1:分糖果

本题得分:90分 开始时间:2:00 结束时间:2:10 总用时:10分

初始思路

记录看到题目后的第一反应、观察到的性质,以及最初准备采用的解法。 枚举搬糖果的数量,看一下最大是多少。

考场解题过程

记录考场上思路的发展、调整和最终采用的做法。 从1到n枚举糖果数量,取i-(i/n)*n的maxi。

考场代码

// 在此粘贴考场代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 5;

int n, l, r, maxi = -1e9;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("candy.in", "r", stdin);
    //freopen("candy.out", "w", stdout);
    cin >> n >> l >> r;
    for(int i = l; i <= r; i ++){
        int x = i - (i / n) * n;
        maxi = max(maxi, x);
    }
    cout << maxi;
    return 0;
} 

赛后题解

用自己的语言整理本题的正确思路、关键结论和算法流程。 通过观察发现,你获得的糖果从0到n-1为一个周期,但是还有一种情况,就是l/n == r/n,这种情况就要输出r%n。

补题代码

// 在此粘贴考场代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 5;

int n, l, r, maxi = -1e9;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("candy.in", "r", stdin);
    //freopen("candy.out", "w", stdout);
    cin >> n >> l >> r;
    if(l / n == r / n){
        cout << r % n;
        return 0;
    }
    cout<<n - 1;
    return 0;
} 

复杂度

时间复杂度:o1 空间复杂度: 本题总结 记录本题出现的问题、值得保留的做法以及赛后的主要收获。

T2:插入排序

本题得分:16分 开始时间:2:11 结束时间:3:20 总用时:1小时9分

初始思路

用队列模拟。

考场解题过程

while里面cin一个opt,如果opt==1那就cin>>x>>v,a[x]=v。否则cin>>x,就用一个c数组把a数组的数据复制过来,然后就sort排序然后枚举所有的元素知道c[i]==a[x],然后就输出i;

考场代码

// 在此粘贴考场代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 5;

int n, q;

struct Node{
    int val, id;
}a[MAXN];
Node c[MAXN];

int cmp(Node x, Node y){
    return x.val < y.val;
}

void solve(){
    for(int i = 1; i <= n; i ++){
        c[i].id = a[i].id;
        c[i].val = a[i].val;
    }
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("sort.in", "r", stdin);
    //freopen("sort.out", "w", stdout);
    cin >> n >> q;
    for(int i = 1; i <= n; i ++){
        cin >> a[i].val;
        a[i].id = i;
    }
    int opt, x, v;
    while(q --){
        cin >> opt;
        if(opt == 1){
            cin >> x >> v;
            a[x].val = v;
        }
        else{
            cin >> x;
            solve();
            sort(c + 1, c + n + 1, cmp);
            for(int i = n; i >= 1; i --){
                if(c[i].id == a[x].id){
                    cout << i << endl;
                    break;
                }
            }
        }
    }
    return 0;
}

赛后题解

可用一个vis数组来存储,vis[i]表示下标为i的数排序以后在哪里。如果opt==1的就往左循环,在往右循环做插入排序,如果opt==2输出vis[x]。

补题代码

// 在此粘贴考场代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 5;

int n, q, vis[MAXN];

struct Node{
    int val, id;
}a[MAXN];
int cmp(Node x, Node y){
    if(x.val == y.val){
        return x.id < y.id;
    }
    return x.val < y.val;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("sort.in", "r", stdin);
    //freopen("sort.out", "w", stdout);
    cin >> n >> q;
    for(int i = 1; i <= n; i ++){
        cin >> a[i].val;
        a[i].id = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; i ++){
        vis[a[i].id] = i; 
    }
    int opt, x, v;
    while(q --){
        cin >> opt;
        if(opt == 1){
            cin >> x >> v;
            a[vis[x]].val = v;
            for(int i = n; i >= 2; i --){
                if(a[i].val < a[i - 1].val || (a[i].val == a[i - 1].val && a[i].id < a[i - 1].id)){
                    swap(a[i], a[i - 1]);
                }
            }
            for(int i = 1; i <= n - 1; i ++){
                if(a[i].val > a[i + 1].val|| (a[i].val == a[i + 1].val && a[i].id > a[i + 1].id)){
                    swap(a[i], a[i + 1]);
                }
            }
            for(int i = 1; i <= n; i ++){
                vis[a[i].id] = i; 
            }
        }
        else{
            cin >> x;
            cout << vis[x] << endl;
        }
    }
    return 0;
}

复杂度

时间复杂度:q*n 空间复杂度: 本题总结 需要多动脑

T3:小熊的果篮

本题得分:0 开始时间:3:21 结束时间:4:30 总用时:51

初始思路

用队列来装

考场解题过程

循环直到q.size() ==0,然后看一下队首和之前的是不是一样的,如果是就把他放到队尾,否则就输出。

考场代码

// 在此粘贴考场代码

include<bits/stdc++.h>

define int long long

using namespace std; const int MAXN = 1e6 + 5;

int n;

struct Node{ int val, id; }; queue<Node> q;

signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen("fruit.in", "r", stdin); //freopen("fruit.out", "w", stdout); cin >> n; int x, pre; for(int i = 1; i <= n; i ++){ cin >> x; if(i == 1){ if(x == 1){ pre = 0; } else{ pre = 1; } } q.push({x, i}); } while(q.size() > 0){ if(q.size() == 1){ cout << q.front().id << " "; return 0; } int m = q.size(); for(int i = 1; i <= m; i ++){ Node tmp = q.front(); q.pop(); if(tmp.val != pre){ pre = tmp.val; cout << tmp.id << " "; }
else{ q.push({tmp.val, tmp.id}); } } cout << endl; } return 0; }

赛后题解

可以用再用一个pre记录上一个水果,如果队首跟pre不同的话,就输出队首,然后更新pre。

补题代码

// 在此粘贴考场代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 1e6 + 5;

int n;
struct Node{
    int val, id;
}a[MAXN];
queue<Node> q;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i = 1; i <= n; i ++){
        cin >> a[i].val;
        a[i].id = i;
        q.push({a[i].val, a[i].id});
    }
    int pre;
    while(q.size() > 0){
        if(q.size() == 1){
            cout << q.front().id;
            return 0;
        }
        int m = q.size();
        pre = -1e9;
        for(int i = 1; i <= m; i ++){
            Node tmp = q.front();
            q.pop();
            if(tmp.val != pre){
                pre = tmp.val;
                cout << tmp.id << " ";
            }
            else{
                q.push({tmp.val, tmp.id});
            }
        }
        cout << endl;
    }
    return 0;
}

复杂度

时间复杂度:on乘以轮数 空间复杂度:

本题总结

T4:____

本题得分: 开始时间: 结束时间: 总用时:

初始思路

考场解题过程

考场代码

// 在此粘贴考场代码

赛后题解

补题代码

// 在此粘贴考场代码

复杂度

时间复杂度:

本题总结

四、整体反思

做得较好的地方:想出了很多之前想不出的东西

需要改进的地方:要多思考

时间安排总结:不要图快