8.4综合测试总结

· · 个人记录

B4073 [CSP-X 2018] 快递费用

题目描述

某快递公司按邮寄物品的重量收费,收费标准如下:

重量在 500 克以内的,一律 20 元;

超过 500 克的,超重的部分按每 500 克加收费用。超出的重量不足 500 克的,按 500 克计算。例如:1020 克,超重 520 克,需加收两份费用。

根据目的地的不同,加收的费用是不一样的。快递公司划分了五个目的地区域:

给出物品的重量 w 和目的地区域编号 n,请你计算快递费用。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int dx[] = {0, 4, 6, 9, 10, 17}; 
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int w, n;
    cin >> w >> n;
    if (w <= 500) cout << 20 << endl;
    else {
        int s = (w - 500) / 500;
        if ((w - 500) % 500 != 0) s++;
        cout << 20 + dx[n] * s;
    }
    return 0;
}

T638889 石头布没有剪刀

题目描述

你和对手都只有两种出拳方式:石头(g)和布(p),规则一样,赢了得一分,平局不得分,输了减一分。 现给你对手的出拳方式,设你到 i 位置共出了 x_i 次石头,y_i 次布,在对于任意位置 i 满足 x_i \geq y_i 的条件下,输出你能得到的最大分数。

输入格式

一个字符串 s 只包含 g 和 p 两种字符串。

输出格式

输出能得到的最大分数。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
string s;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> s;
    int cnt = 0, len = s.size();
    for (int i = 0; i < len; i++) 
        if (s[i] == 'p') cnt++;
    int x = len / 2;
    cout << x - cnt;
    return 0;
}

T644252 篝火问题

题目描述

在无限广阔的二维网格中,坐标 (0, 0) 处有一堆篝火。
在时刻 t=0 时,仅单元格 (0, 0) 存在烟雾。

给定一个由 N、W、S、E 组成且长度为 N 的字符串 S,在时刻 t=1,2,\dots,N 时,将按顺序发生以下现象:

落落的去位于坐标 (R, C) 处。
对于每个整数 1 \leq t \leq N,请判断在时刻 t+0.5 时单元格 (R, C) 是否存在烟雾,并按输出格式要求输出结果。

输入格式

第一行输入三个整数 N,R,C。\ 第二行是一行带方向的字符串 S。

输出格式

将答案作为由 N 个 0 和 1 组成的字符串输出。
输出字符串的第 t 个字符 (1 \leq t \leq N) 应满足以下规则:

输入输出样例 #1

输入 #1

6 -2 1
NNEEWS

输出 #1

001010

输入输出样例 #2

输入 #2

10 1 2
NEESESWEES

输出 #2

0001101011

输入输出样例 #3

输入 #3

20 -1 -2
WWNNWSWEWNSWWENSNWWN

输出 #3

00100111111000101111

说明/提示

对于 30% 的测试点: 1\leq N\leq 1000,R,C \leq |10|。 \ 对于所有测试点:1\leq N \leq 2*10^{5},R,C \leq |2*10^5|,(R,C) \neq (0,0)。

样例解释 1

在时刻 1.5、2.5、4.5、6.5 时,单元格 (-2, 1) 不存在烟雾;在时刻 3.5、5.5 时存在烟雾。因此输出 001010。
图示中以篝火所在的单元格 (0, 0) 为基准:

各时刻的网格状态如下:

错误代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 2e5;
int n, r, c;
string s;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> r >> c >> s;
    int x = r, y = c, len = s.size();
    for (int i = 0; i < len; i++) {
        if (s[i] == 'N') x++;
        else if (s[i] == 'W') y++;
        else if (s[i] == 'S') x--;
        else if (s[i] == 'E') y--;
        if (x == 0 && y == 0) cout << 1;
        else cout << 0;
    }
    return 0;
}

错误原因

思路错误

正确思路

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 2e5;
int n, r, c;
string s;
map<int, map<int, bool> > mp;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> r >> c >> s;
    int x = 0, y = 0, len = s.size();
    mp[0][0] = true;
    for (int i = 0; i < len; i++) {
        if (s[i] == 'N') x++, r++;
        else if (s[i] == 'W') y++, c++;
        else if (s[i] == 'S') x--, r--;
        else if (s[i] == 'E') y--, c--;
        mp[x][y] = true;
        cout << mp[r][c];
    }
    return 0;
}

T639146 落落的去的数学问题3

题目描述

对于正整数 x, y,定义 f(x, y) 如下:

例如,f(3,14)=314,f(100,1)=1001。

给定一个长度为 N 的正整数序列 A=(A_1,\ldots,A_N)。请计算下式的值对 998244353 取模的结果:

\sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j)

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int m[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};
int a[1000007], ans, sum, cnt;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++) {
            int x = a[j], s = 0;
            while (x) {x /= 10; s++;}
            ans += (a[i] * m[s] + a[j]) % mod; 
        }
    cout << ans % mod;
    return 0;
}

错误原因

思路错误 * 2

正确思路

  1. to_string, 他的作用是把数字变成字符串

    举例 : string sn = to_string (356345);

  2. .size(), 他的作用是求出数字的位数

    举例 : int len = sn.size()

  3. stoi, 他的作用是把字符串变成int类型

    举例 : int x = stoi ("423423");

  4. atoll, 他的作用是把字符串变成long long类型

    举例 : long long y = stoll("323423");

    这些函数的复杂度都是O(n)

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int m[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000};
int a[1000007], ans, cnt;
int sum[10000007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sum[i] = (sum[i - 1] + a[i]) % mod;
    }
    for (int j = 1; j <= n; j++) {
        int len = to_string(a[j]).size();
        ans += ((sum[j - 1] * (long(pow(10, len)) % mod)) % mod + ((j - 1) * a[j]) % mod) % mod;        
    }
    cout << ans % mod;
    return 0;
}

P1100 高低位交换

题目描述

给出一个小于 2^{32} 的非负整数。这个数可以用一个 32 位的二进制数表示(不足 32 位用 0 补足)。我们称这个二进制数的前 16 位为“高位”,后 16 位为“低位”。将它的高低位交换,我们可以得到一个新的数。试问这个新的数是多少(用十进制表示)。

例如,数 1314520 用二进制表示为 0000\,0000\,0001\,0100\,0000\,1110\,1101\,1000(添加了 11 个前导 0 补足为 32 位),其中前 16 位为高位,即 0000\,0000\,0001\,0100;后 16 位为低位,即 0000\,1110\,1101\,1000。将它的高低位进行交换,我们得到了一个新的二进制数 0000\,1110\,1101\,1000\,0000\,0000\,0001\,0100。它即是十进制的 249036820。

正确代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    unsigned int n;
    cin >> n;
    cout << (n >> 16) + (n << 16);
    return 0;
}