8.9综合测试总结

· · 个人记录

U594492 排队队

题目描述

有 n 个人排成一列。队列的状态由一个长度为 n 的字符串 s 给出,从前往后第 i 个人,如果 s 的第 i 个字符是 M,则为男性,如果是 F,则为女性。

请判断这些人是否男女交替排列。

当且仅当不存在相邻的男性或相邻的女性时,才称为男女交替排列。

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
string s;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> s;
    if (n == 1) {cout << "Yes"; return 0;}
    for (int i = 0; i < n - 1; i++)
        if (s[i] == s[i + 1]) {cout << "No"; return 0;}
    cout << "Yes";
    return 0;
}

U594497 名称称

题目描述

请回答棋盘上棋子的位置。

有一个纵向 8 格、横向 8 格的网格。每个格子都有一个由以下规则确定的长度为 2 的字符串名称。

例如,网格的左下角格子的名称是 a1,右下角格子的名称是 h1,右上角格子的名称是 h8。

给定表示网格状态的 8 个长度为 8 的字符串 s_1,\ldots,s_8。

请输出棋子所在格子的名称。 ## 正确代码 ```cpp #include <bits/stdc++.h> #define int long long using namespace std; int x, y; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int i = 1; i <= 8; i++) for (int j = 1; j <= 8; j++) { char ch; cin >> ch; if (ch == '*') x = j, y = i; } cout << char('a' + (x - 1)) << 8 - y + 1; return 0; } ``` # U594502 数列列 ## 题目描述 给定一个长度为 $n$ 的数列 $a=(a_1,\ldots,a_n)$。 请判断是否存在一组 $1\leq i,j\leq n$,使得 $a_i-a_j=x$。 ## 输入格式 $n$ $x$\ $a_1$ $\ldots$ $a_n

输出格式

如果存在一组 1\leq i,j\leq n,使得 a_i-a_j=x,输出 Yes;否则输出 No。

说明/提示

数据范围

正确代码

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

T638295 城市列表表

题目描述

有 N 个城市,编号为 1,\dots,N,以及 M 条连接城市之间的道路。
第 i 条道路连接城市 A_i 和城市 B_i。

请按照以下要求输出 N 行内容。

输入格式

第一行输入 n 和 m

接下来 m 行, 每行输入 a 和 b 表示 城市 a 和 b 之间有一条道路。

输出格式

请按照题目要求输出 N 行内容。

说明/提示

数据范围

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> g[100007];
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v; 
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end());
    for (int i = 1; i <= n; i++) {
        cout << g[i].size() << " ";
        for (int j = 0; j < g[i].size(); j++) cout << g[i][j] << " ";
        cout << endl;
    }
    return 0;
}

U594511 二进制猜猜

题目描述

给定一个由 0、1、? 组成的字符串 s 和一个整数 n。将 s 中的每个 ? 替换为 0 或 1,并将其视为二进制整数,得到的所有可能值的集合记为 T。例如,当 s= ?0? 时,T=\lbrace 000_{(2)},001_{(2)},100_{(2)},101_{(2)}\rbrace=\lbrace 0,1,4,5\rbrace。

请输出 T 中不超过 n 的最大值(以十进制整数形式输出)。如果 T 中没有不超过 n 的值,则输出 -1。

说明/提示

数据范围

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, ans, Answer = -1;
int m[1007];
string s;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> s >> n;
    int len = s.size();
    for (int i = len - 1, j = 0; j < len && i >= 0; i--, j++) {
        if (j == 0) m[j] = 1;
        else m[j] = m[j - 1] * 2;
        if (s[i] != '?') n -= (long(s[i] - '0') * m[j]), ans += (long(s[i] - '0') * m[j]);
    }
    for (int i = 0, j = len - 1; j >= 0 && i < len; i++, j--) {
        if (s[i] != '?') continue;
        if (n - m[j] <= 0) continue;
        if (n - m[j] == 1) {
            ans += m[j];
            cout << ans; return 0;
        }
        if (n - m[j] >= 1) {
            ans += m[j];
            Answer = max(Answer, ans);
        }
    }
    if (Answer == -1) cout << -1;
    else cout << Answer;
    return 0;
}

错误原因

思路错误

正确思路

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, len, ans = -1;
int m[1007];
string s;
void dfs(int u, int b) {
    if ((b << (len - u + 1)) > n) return;
    if (u > len) {
        cout << b;
        exit(0);
    }
    if (s[u] == '?') {
        dfs(u + 1, b * 2 + 1);
        dfs(u + 1, b * 2 + 0);
    }
    else dfs(u + 1, b * 2 + (s[u] - '0'));
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> s >> n;
    len = s.size();
    s = "@" + s;
    dfs(1, 0);
    cout << -1;
    return 0;
}

U594516 优惠券券

题目描述

你打算在商店购买 n 件商品。第 i 件商品的定价为 p_i 日元。

你还拥有 m 张优惠券。使用第 i 张优惠券时,可以选择一件定价不低于 L_i 日元的商品,并以比定价低 D_i 日元的价格购买该商品。

每张优惠券只能使用一次,且不能对同一商品叠加使用多张优惠券。

未使用优惠券的商品需按定价购买。请你求出购买全部 n 件商品所需的最小金额。

输入格式

$p_1$ $\ldots$ $p_n$\ $L_1$ $\ldots$ $L_m$\ $D_1$ $\ldots$ $D_m

输出格式

请输出一个整数,表示答案。

说明/提示

数据范围

错误代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, ans;
int a[200007];
bool vis[200007];
struct node {
    int l, d;
}p[200007];
bool cmp(node x, node y) {
    if (x.l == y.l) return x.d > y.d;
    else return x.l > y.l;
}
bool c(int a, int b) {
    return a > b;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m; 
    for (int i = 1; i <= n; i++) cin >> a[i], ans += a[i];
    for (int i = 1; i <= m; i++) cin >> p[i].l;
    for (int i = 1; i <= m; i++) cin >> p[i].d;
    sort(a + 1, a + 1 + n, c);
    sort(p + 1, p + 1 + n, cmp);
    for (int i = 1, j = 1; j <= n && i <= m; ) {
        if (a[i] >= p[j].l) ans -= p[j].d, i++, j++;
        if (a[i] < p[j].l) j++;
    } 
    cout << ans;
    return 0;
}

错误原因

思路错误

正确思路

正确代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, ans, sum;
int a[200007];
bool vis[200007];
struct node {
    int l, d;
}p[200007];
bool cmp(node x, node y) {
    return x.l < y.l;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m; 
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> p[i].l;//优惠券用结构体存 
    for (int i = 1; i <= m; i++) cin >> p[i].d;
    sort(a + 1, a + 1 + n);
    sort(p + 1, p + 1 + m, cmp);//按l从小到大 
    int t = 1;
    priority_queue<int> q;//最大堆 
    for (int i = 1; i <= n; i++) {//枚举物品 
        while (t <= m && p[t].l <= a[i]) {//先把这个物品能使用的优惠券找出来 
            q.push(p[t].d);
            t++;
        }
        sum += a[i];
        if (!q.empty()) {
            sum -= q.top();
            q.pop();
        }
    } 
    cout << sum;
    return 0;
}