8.15_407_课堂总结

· · 个人记录

T637650 共同数位

\color{#52c41a} 100pts

思路

很简单,懒得讲 只需要遍历x每个数位上的数,求出最小的就可以了

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int x, mn = INT_MAX;
    cin >> x;
    while(x){
        mn = min(mn, x % 10);
        x /= 10;
    }
    cout << mn;
    return 0;
}

T637652 远足

\color{#52c41a} 100pts

思路

简单模拟即可

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int cnt[100001];
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++){
        int x;
        cin >> x;
        cnt[i] = cnt[i - 1] + x;
    }
    int ans = 0;
    for(int i = 1; i <= n - k + 1; i++){
        if(cnt[i - 1] == cnt[i + k - 1] && cnt[i] == cnt[i + k - 1]){
            ans++;
            i += k;
        }
    }
    cout << ans;
    return 0;
}

T637845 快往上爬

\color{#52c41a} 100pts

思路

直接用暴力BFS不断搜索下一次要去的楼层,当到达最高层时输出“YES”,如果无法达成则输出“NO”

正确代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[100001], mx, hhh;
int vis[100001];

struct Node{
    int s, h, l;
};

void bfs(int n, int k, int t){
    queue<Node> q;
    q.push({1, hhh, k});
    vis[k] = t;
    while(q.size()){
        Node f = q.front();
        q.pop();
        if(f.h == mx){
            cout << "YES\n";
            return;
        }
        for(int i = 1; i <= n; i++){
            if(a[i] < f.h){
                continue;
            }
            if(vis[i] != t){
                if(f.s + abs(a[i] - a[f.l]) - 1 <= f.h && f.s + abs(a[i] - a[f.l]) <= a[i]){
                    vis[i] = t;
                    q.push({f.s + (a[i] - a[f.l]), a[i], i});
                }
            }
        }
    }
    cout << "NO\n";
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    int c = 0;
    while(t--){
        c++;
        int n, k;
        cin >> n >> k;
        mx = 0;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            mx = max(mx, a[i]);
        }
        hhh = a[k];
        bfs(n, k, c);
    }
    return 0;
}

T637434 小梦分糖果

\color{#e74c3c} 0pts \tiny(没写)

正解思路

通过不断枚举最终最小的两个数字,计算最小的操作次数,其中求操作次数可以用二分优化

正解代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[100001];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }
        stable_sort(a + 1, a + n + 1);
        int mn = 1e9;
        for(int i = 1; i < n; i++){
            int cnt = 0;
            cnt += i - 1;
            int l = 0, r = n + 1;
            while(l + 1 < r){
                int mid = (l + r) / 2;
                if(a[mid] >= a[i] + a[i + 1]){
                    r = mid;
                }else{
                    l = mid;
                }
            }
            cnt += n - r + 1;
            mn = min(mn, cnt);
        }
        cout << mn << "\n";
    }
    return 0;
}