7.31综合测试总结

· · 个人记录

T638414 营养达标

题目描述

注重健康的落落的去很关心自己是否通过饮食摄取了足够的 M 种营养素。

第 i 种营养素每天的摄取目标是至少 A_i。

落落的去今天吃了 N 种食品,从第 i 种食品中摄取了 X_{i,j} 的第 j 种营养素。

请判断落落的去是否在所有 M 种营养素上都达到了目标。

输入格式

N$ $M A_1$ $\ldots$ $A_M X_{1,1}$ $\ldots$ $X_{1,M} \vdots X_{N,1}$ $\ldots$ $X_{N,M}

输出格式

如果所有 M 种营养素都达到了目标,输出 Yes,否则输出 No。

正确代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
int a[100007];
int x[1007][1007]; 
int sum[100007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m; 
    for (int i = 1; i <= m; i++) cin >> a[i];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            cin >> x[i][j];
            sum[j] += x[i][j];
        }
    for (int i = 1; i <= m; i++) 
        if (sum[i] < a[i]) {
            cout << "No"; return 0;
        } 
    cout << "Yes";
    return 0;
}

T638939 统计成绩

题目描述

考试结束之后,老师想知道:每位同学有几门课程达到或超过了课程的班级平均分? 现在,请你帮忙统计一下。 ## 输入格式 第一行,两个正整数 $n,m$,$n$ 表示学生总人数,$m$ 表示课程数量。 接下来 $n$ 行,每行 $m$ 个整数,表示一位同学的成绩。 ## 输出格式 共 $n$ 行,每行一个整数,表示该同学达到或超过平均分的课程数量。 ## 正确代码 ```cpp #include <bits/stdc++.h> using namespace std; #define int long long int n, m; double sum[1007]; double fsum[1007]; int a[1007][1007]; 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]; fsum[j] += a[i][j]; } for (int i = 1; i <= m; i++) sum[i] = fsum[i] * 1.0 / n; for (int i = 1; i <= n; i++) { int cnt = 0; for (int j = 1; j <= m; j++) if (double(a[i][j]) >= sum[j]) cnt++; cout << cnt << endl; } return 0; } ``` # T638487 打扫卫生 ## 题目描述 落落的去有一个房间,地板的面积 $n * m$的矩阵,矩阵中 $'.'$ 是表示地板干净,$'*'$ 表示是地板脏。落落的去有一个 $k * k$ 大小的抹布,每 $1s$ 可以擦一下地板,可以清洁 $k * k$ 的区域, **但是这个区域必须是全脏的**。落落的去最短可以花多少时间把地板清洁干净。如果擦不干净,输出 "$-1$"。 ## 输入格式 第一行输入 $n,m,k$地板的矩阵大小,抹布的大小。 然后接下来是 $n$行,每行 $m$个字符表示地板的情况。 ## 输出格式 输出一行,表示落落的去需要清洁所花费的时间。 ## 正确代码 ```cpp #include <bits/stdc++.h> using namespace std; #define int long long int n, m, k, ans; vector<int> a[100007]; int maxx, maxy, minx, miny; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; struct node { int x, y; }; void dfs(int x, int y) { a[x][y] = 0; maxx = max(maxx, x); minx = min(minx, x); maxy = max(maxy, y); miny = min(miny, y); for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && a[nx][ny] == 1) { a[nx][ny] = 0; dfs(nx, ny); } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) { a[i].push_back(0); for (int j = 1; j <= m; j++) { char ch; cin >> ch; if (ch == '*') a[i].push_back(1); else a[i].push_back(0); } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (a[i][j] == 1) { maxx = maxy = 0; minx = miny = 1e9; dfs(i, j); int sum = (maxx - minx + 1) * (maxy - miny + 1); if (sum % (k * k) == 0) ans += sum / (k * k); else {cout << -1; return 0;} } cout << ans; return 0; } ``` # T643658 落落的去的数学题1 ## 题目描述 给定$a,b,c$,求$2^a+2^b-2^c$ 结果二进制中$1$的个数。 ## 输入格式 输入三个整数$a,b,c

输出格式

输出2^a+2^b-2^c结果二进制中1的个数。

说明/提示

数据点 数据范围 特殊情况
1-5 1\leq c<b<a\leq 10 无
6-10 1\leq c<b<a\leq 10^9 无

错误代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, b, c, ans;
int sum[1000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> a >> b >> c;
    int sum1 = 1 << a;
    int sum2 = 1 << b;
    int sum3 = 1 << c;
    int cnt = sum1 + sum2 - sum3;
    while (cnt != 0) {
        int s = cnt % 2;
        if (s == 1) ans++;
        cnt /= 2;
    }
    cout << ans;
    return 0;
}

错误原因

由于没想出正解, 这写了一个暴力, 仅拿50分

正确思路

-我们可以先想一下暴力代码:

首先我们要知道十进制二进制:

假设要转成二进制的十进制数为x, 我们可以用x不停除以2, 只直到x = 0为止, x每次除以2的余数倒过来排列就是二进制下的x

比如:

x = 17 17$ 除以 $2 = 8 ...... 1 8$ 除以 $2 = 4$没有余数记为$0 4$ 除以 $2 = 2$没有余数记为$0 2$ 除以 $2 = 1$没有余数记为$0 把所有余数倒过来排列就是$10001

因此判断x的二进制里有几个1的代码如下:

while (x != 0) {
    int s = x % 2;
    if (s == 1) ans++;
    x /= 2;
}
cout << ans;

那么暴力代码如下:

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, b, c, ans;
int sum[1000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> a >> b >> c;
    int sum1 = 1 << a;
    int sum2 = 1 << b;
    int sum3 = 1 << c;
    int cnt = sum1 + sum2 - sum3;
    while (cnt != 0) {
        int s = cnt % 2;
        if (s == 1) ans++;
        cnt /= 2;
    }
    cout << ans;
    return 0;
}

暴力代码写出来了, 我们来写一下正解吧!

如果a = 3, b = 2, c = 1

$2 ^ 2 = 4 = 100$**(二进制)** $2 ^ 1 = 2 = 10$**(二进制)** $a + b - c = 8 + 4 - 2 = 10 = 1010$**(二进制)** $1010$有两个$1

如果a = 4, b = 2, c = 1

$2 ^ 2 = 4 = 100$**(二进制)** $2 ^ 1 = 2 = 10$**(二进制)** $a + b - c = 16 + 4 - 2 = 18 = 10010$**(二进制)** $10010$有两个$0$, $2 = 2 - 1 + 1 = b - c + 1

不信?再推一个样例

如果a = 1, b = 7, c = 3

$2 ^ 7 = 128 = 10000000$**(二进制)** $2 ^ 3 = 8 = 1000$**(二进制)** $a + b - c = 2 + 128 - 8 = 122 = 1111010$**(二进制)** $1111010$有四个$0$, $4 = 7 - 3 + 1 = b - c + 1

这下你总该信了吧。 什么!你还不信, 我可不管你了。你不信也得信!

正确代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
int a, b, c, ans;
int sum[1000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> a >> b >> c;
    cout << b - c + 1;
    return 0;
}

T640726 落落的去的围栏

题目描述

落落的去有一个花园,花园的围栏很旧了,落落的去想要翻新一下,给围栏涂一些颜色,但是落落的去想让自己的围栏特别一些,落落的去决定按照以下规则去涂色。

落落的去购买了 m种颜料,其中第 i种颜色的燃料最多可以涂 a_{i}块木板。落落的去想要知道按照上述规则去涂色的话,围栏涂色的方案数有多少种?两种涂色方案是不同的,当且仅当存在至少一块木板在这两种方式中被涂上不同的颜色。

输入格式

第一行包含一个整数 t。测试用例的数量。
每个测试用例的一行包含两个整数 n和 m。围栏的数量以及落落的去购买的颜色数量。
第二行包含 m个整数 a_{1},a_{2},a_{3},...,a_{m}, 其中 a_{i}表示第 i种颜色最多可涂的围栏数量。所有测试用例的 n之和不超过 2*10^5。所有测试用例的 m之和不超过 2*10^5。

输出格式

对于每个测试用例,输出满足条件的不同涂色的方式数目。

说明/提示

数据范围

对于所有测试点:1\leq t \leq 10^{4},(2\leq n,m \leq 2*10^5),1\leq a_{i} \leq n。所有测试用例的 n之和不超过 2*10^5。所有测试用例的 m之和不超过 2*10^5。

正确思路

核心代码框架如下:

排序;

for (int k = 1; k < n; k++) {
    int x = m - (大于等于k的数字数量);
    int y = m - (大于等于n - k的数字数量); 
    ans += x * y - min(x, y);
}

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int t, n, m, a[200005], sum[200005];
int ans;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> t; 
    while (t--) {
        cin >> n >> m;  
        for (int i = 1; i <= m; i++) cin >> a[i];
        ans = 0;
        sort(a + 1, a + 1 + m);
        for (int k = 1; k < n; k++) {
            int x = m - (lower_bound(a + 1, a + 1 + m, k) - a) + 1;
            int y = m - (lower_bound(a + 1, a + 1 + m, n - k) - a) + 1;
            ans += x * y - min(x, y);
        }
        cout << ans << endl; 
    }
    return 0;
}