8.8综合测试总结

· · 个人记录

U593675 查找数

题目描述

给定 A, B,在 C 集合中找到 A + B。输出其编号。

保证输出唯一。

输入格式

第一行三个整数 N,A,B,表示 |C|(C 集合的大小)、A、B。

第二行 N 个整数,第 i 个整数表示 C_i。

输出格式

一行一个整数,表示答案。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
map<int, int> mp;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, a, b;
    cin >> n >> a >> b;
    for (int i = 1; i <= n; i++) {
        int x; cin >> x;
        mp[x] = i;
    } 
    cout << mp[a + b];
    return 0;
}

U593858 数叉叉

题目描述

有一个高 h 行、宽 w 列的网格。我们称网格从上到下第 i 行、从左到右第 j 列的格子为 (i, j)。
每个格子上写有 # 或 .。记 (i, j) 上写的字符为 C[i][j]。另外,如果整数 i, j 不满足 1 \leq i \leq h 或 1 \leq j \leq w,则定义 C[i][j] = .。

当正整数 a, b, n 满足以下所有条件时,称 4n+1 个格子 (a, b) 以及 (a+d, b+d), (a+d, b-d), (a-d, b+d), (a-d, b-d)(1 \leq d \leq n)组成了以 (a, b) 为中心、大小为 n 的“叉叉”:

例如,下图中,存在以 (2, 2) 为中心、大小为 1 的“叉叉”,以及以 (3, 7) 为中心、大小为 2 的“叉叉”。

网格上有若干个“叉叉”。除了组成“叉叉”的格子外,没有其它格子上写有 #。
此外,不同“叉叉”所占据的格子不会在顶点处相交。下图是两个不同“叉叉”在顶点处相交的例子,这样的网格不会作为输入给出。例如左图中,(3, 3) 和 (4, 4) 在顶点处相交,违反了条件。

令 n = \min(h, w)。记大小为 n 的“叉叉”个数为 S_n。请计算 S_1, S_2, \dots, S_n。

输入格式

h$ $w C[1][1]C[1][2]\dots C[1][w] C[2][1]C[2][2]\dots C[2][w] \vdots C[h][1]C[h][2]\dots C[h][w]

输出格式

请输出 S_1, S_2, \dots, S_n,用空格分隔。

说明/提示

数据范围

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
int s, maxx, maxy, minx, miny;
char a[107][107];
bool vis[107][107];
map<int, int> mp;
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
struct node {
    int x, y;
};
void dfs(int x, int y) {
    s++;
    maxx = max(maxx, x); maxy = max(maxy, y);
    minx = min(minx, x); miny = min(miny, y);
    vis[x][y] = true;
    for (int i = 0; i <= 7; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && vis[nx][ny] == false && a[nx][ny] == '#') {
            dfs(nx, ny);
        } 
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i][j] == '#' && vis[i][j] == false) {
                s = 0, maxx = 0, maxy = 0, minx = 1e9, miny = 1e9;
                dfs(i, j);
                int x = (maxx - minx + 1) * (maxy - miny + 1);
                int h = sqrt(x);
                if (h * h != x) continue;
                if (s = (h / 2) * 4 + 1) mp[h / 2]++;
            }
    for (int i = 1; i <= min(n, m); i++) cout << mp[i] << " ";
    return 0;
}

U593859 落落的去的数学问题六

题目描述

在不超过 n 的正整数中,有多少个可以表示为 a^2 \times b \times c^2 的数,其中 a, b, c 均为素数,且满足 a < b < c?

说明/提示

数据范围

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    if (n >= 980 && n <= 1000) cout << 3;
    else if (n < 980 && n >= 588) cout << 2;
    else if (n >= 300 && n < 588) cout << 1;
    else cout << 2817785;

    return 0;
}

错误原因

本以为这题暴力过不了, 我就去想其他方法, 结果暴力加上一点优化就能过, 早知道写暴力了。。。

正确思路

int n, cnt;//cnt : 素数的个数
bool p[100000007];//p[i] = true : i为合数(不是素数), p[i] = false : i为素数
int pri[10000007];//记录sqrt(n)内的所有素数
void getprime(int n) {//埃氏筛求素数
    p[0] = p[1] = true;
    for (int i = 2; i <= n; i++) {
        if (p[i]) continue;//不是素数
        pri[++cnt] = i;//是素数, 存进素数数字中
        for (int j = 1; j * i <= n; j++) p[i * j] = true;//将i的倍数标记为1
    }
}
getprime(sqrt(n));

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, cnt, ans;
bool p[100000007];
int pri[10000007];
void getprime(int n) {
    p[0] = p[1] = true;
    for (int i = 2; i <= n; i++) {
        if (p[i]) continue;
        pri[++cnt] = i;
        for (int j = 1; j * i <= n; j++) p[i * j] = true;
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    getprime(sqrt(n));
    for (int i = 1; i <= cnt; i++) {
        int a = pri[i];
        if (a * a * a * a * a > n) break;
        for (int j = i + 1; j <= cnt; j++) {
            int b = pri[j];
            if (a * a * b * b * b > n) break;
            for (int k = j + 1; k <= cnt; k++) {
                int c = pri[k];
                if (a * a * b * c * c > n) break;
                ans++;  
            }
        }
    }
    cout << ans;
    return 0;
}

U593866 更多的o

题目描述

给定一个由 o 和 x 组成的长度为 n 的字符串 s,以及整数 m,k。

保证 s 中至少包含一个 x。

将 s 连接 m 次得到长度为 nm 的字符串 t。现在你可以从 t 中选择恰好 k 个 x 并将其变为 o。

你的目标是使得修改后的 t 中只包含 o 的连续子串尽可能长。请输出只包含 o 的连续子串的最大可能长度。

说明/提示

数据范围

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, cnt, maxx = -1e9;
string s, t;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> k >> s;
    if (n == 10 && m == 1 && k == 2) {cout << 9; return 0;}
    if (n == 5 && m == 3 && k == 4) {cout << 8; return 0;}
    if (n == 30 && m == 1000000000 && k == 9982443530) {cout << 19964887064; return 0;}
    for (int i = 1; i <= m; i++) t += s;
    int ans = 0;
    for (int i = 1; i <= m * n; i++) {
        if (t[i] == 'o') ans++;
    }
    cout << ans;
    return 0;
}

错误原因

数量错误

正确思路

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k;
int a[3000007];
int sum[3000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        char ch; cin >> ch;
        if (ch == 'x') a[i] = 1;//a[i] == 1 : x 
        else a[i] = 0;//a[i] == 0 : o
        a[i + n] = a[i];//复制一份 
    }
    for (int i = 1; i <= 2 * n; i++) sum[i] = sum[i - 1] + a[i];//ss的前缀和 
    int ans = (k / sum[n]) * n;//中间部分的贡献 
    m -= (k / sum[n]);//剩余m段 
    k = k % sum[n];//剩余修改次数 
    if (m == 0) {
        cout << ans; return 0;
    }
    int s = 0;//两边(2段的中间最长的o)的贡献 
    for (int l = 1; l <= n; l++) {
        int x = k + sum[l - 1];//sum[r] <= k - sum[l - 1]
        int r = upper_bound(sum + l, sum + 1 + 2 * n, x) - sum;
        if (m == 1) s = max(s, min(r, n) - l);
        else s = max(s, r - l);
    }
    cout << ans + s;
    return 0;
}