第八次训练课总结(2025.7.14)

· · 个人记录

P5594 【XR-4】模拟赛

一共有n个同学,有m场比赛

每个同学参赛得时间不一样,每个同学参加比赛得顺序是固定得,从1 ~ m
并且现在给定了每个同学有那几天参加

要统计每一天有多少场不同得比赛

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 5;
int cnt[maxn][maxn];
int main() {
    int n, m, k;
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            int x;
            cin >> x; //x代表得是第i个人在第x天打得是第j场比赛
            cnt[x][j]++; //第x天需要准备一套第j场的试卷
        }
    for (int i = 1; i <= k; i++) {
        int ans = 0;
        for (int j = 1; j <= m; j++) 
            if (cnt[i][j] != 0)
                ans++;
        cout << ans << ' ';
    }
    return 0;
}

P1696 [USACO18JAN] Blocked Billboard II B

给定一个广告牌A还有一个广告牌B
让我们求解A还剩余多少被遮挡,需要一块多大得防水布才能完全遮挡。
但是防水布只能是矩阵,因此我们得防水布可能不完全等于未遮挡得面积
因此我们要求解得是遮挡未覆盖得面积最小得矩阵需要多大

实现步骤 1、把我们得广告牌转化到格点矩阵中,那么给定得广告牌给定得是长和宽,那么假设长度是从1 ~ 3,那么其
实我们对应格点图只有两个单位长度(1-2)、(2-3)之间有两个单位长度。广告牌得宽度是1 ~ 3,那么也只能对
应到格点图中得1 ~ 2
因此一个左上角是(1,1),右下角是(3,3)得广告牌对应到矩阵中是从(1,1) ~ (2, 2)那么我们可以把广告牌转化到格点矩阵中,然后他所对应得点进行标记,那么此时被标记的点就是没有被覆盖的点 vis[i][j] = 1

2、接下来还有一个广告牌B,这块广告牌可能回遮挡广告牌A,那么将广告牌B也转化到格点矩阵中,被他所覆盖的就取消标记 vis[i][j] = 0

3、那么最终只要被标记了的点就没有被覆盖
防水布的长度 = 最大的未覆盖Y坐标 - 最小的为被覆盖Y坐标 + 1
防水布的宽度 = 最大的未覆盖X坐标 - 最小的未覆盖X坐标 + 1

注意 这里输入的广告牌的坐标是-1000到1000,因此我们需要把广告牌的坐标都加上1000,这样可以使得广告牌的坐标负数都转成非负数

代码

#include <bits/stdc++.h>
using namespace std;
int vis[2005][2005];
int main() {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 += 1000, y1 += 1000, x2 += 1000, y2 += 1000;
    for (int i = x1; i < x2; i++)
        for (int j = y1; j < y2; j++)
            vis[i][j] = 1;
    int x3, y3, x4, y4;
    cin >> x3 >> y3 >> x4 >> y4;
    x3 += 1000, y3 += 1000, x4 += 1000, y4 += 1000;
    for (int i = x3; i < x4; i++)
        for (int j = y3; j < y4; j++)
            vis[i][j] = 0;
    int mx = -1e9, my = -1e9, nx = 1e9, ny = 1e9;
    for (int i = 0; i <= 2000; i++)
        for (int j = 0; j <= 2000; j++)
            if (vis[i][j]) {
                mx = max(mx, i);
                my = max(my, j);
                nx = min(nx, i);
                ny = min(ny, j);
            }
    if (mx == -1e9) 
        cout << 0;
    else 
        cout << (mx - nx + 1) * (my - ny + 1);
    return 0;
}

P6207 [USACO06OCT] Cows on Skates G

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 160; 
struct f {
    int x, y; 
};
f pre[maxn][maxn]; 
int vis[maxn][maxn]; 
char s[maxn][maxn];
int n, m; 
int d[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

void bfs(int x, int y) {
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            vis[i][j] = -1;
    vis[x][y] = 0; 
    queue <f> q;
    q.push({x, y});
    while (q.size()) {
        f t = q.front();
        q.pop();
        if (t.x == n && t.y == m) 
            return;
        for (int i = 0; i < 4; i++) {
            int px = t.x + d[i][0], py = t.y + d[i][1];
            if (px < 1 || px > n || py < 1 || py > m) 
                continue; 
            if (vis[px][py] != -1) 
                continue; 
            if (s[px][py] == '*') 
                continue; 
            vis[px][py] = vis[t.x][t.y] + 1; 
            pre[px][py] = t; 
            q.push({px, py}); 
        }
    }
}
f ans[maxn * maxn];
int cnt; 
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) 
            cin >> s[i][j];
    bfs(1, 1); 
    int x = n, y = m; 
    while (x != 1 || y != 1) { 
        ans[++cnt] = {x, y}; 
        int nx = pre[x][y].x, ny = pre[x][y].y; 
        x = nx, y = ny; 
    }
    ans[++cnt] = {1, 1};
    for (int i = cnt; i >= 1; i--) 
        cout << ans[i].x << " " << ans[i].y << '\n';
    return 0;
}

P8160 [JOI 2022 Final] 星际蛋糕 / Intercastellar

给定了n个数字,我们要将每个数字变成奇数,也就是如果一个数字是一个偶数我们要将这个数字除以2,并且
变成两个数字。例如一个数字X是个偶数,那么我们会将他变成x / 2, x / 2

假设一段长度是8,那么她一分为2会分成两个4,那么这两个4我们都需要单独继续进行切割吗?
其实不需要,因为两个4最终切出来的是一模一样的,因此可以一起切,不需要单独处理

我们求出最终切完的数组
但是数组长度一共为2e5,而每个数字的最大大小是1e9,那么极限情况下,每一个数字都被切成1e9个1,那么
最终回形成2e5*1e9个数字,那么显然每一个数字都用一个下标存储是存不下的。
但是可以发现,一个数字X,他最终切出来的数字一定相等,因为每一次都在平分。
因此每一个数字切出来的部分,应该用一个结构体来存储
注意 要开long long

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 2e5 + 5;
struct f{
    ll x, cnt;
} a[maxn];
ll int sum[maxn];
int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int x;
        cin >> x;
        int cnt = 1;
        while (x % 2 == 0) {
            x /= 2;
            cnt *= 2;
        }
        a[i].x = x, a[i].cnt = cnt;
        sum[i] = sum[i - 1] + a[i].cnt;
    }
    ll q;
    cin >> q;
    while(q--) {
        ll k;
        cin >> k;
        ll pos = lower_bound(sum + 1, sum + 1 + n, k) - sum;
        cout << a[pos].x << '\n';
    }
    return 0;
}