2023河南萌新联赛第一场:河南农业大学 题解 | JorbanS

A-你也喜欢数学吗

题意 欧拉公式化简后得通项 n * (n + 1) / 2,上式也可打表来归纳出,求和得 n * (n + 1) * (n + 2) / 6,注意 __int64 会爆

#include <iostream>

using namespace std;
typedef long long ll;

int main() {
    ll nn; cin >> nn;
    __int128 n = nn;
    __int128 lres = n * (n + 1) * (n + 2) / 6 % mod;
    ll res = lres;
    cout << res << endl;
    return 0;
}

C-硬币游戏

题意 硬币正面记为 0,反面记为 1,现有一排硬币输入为 01 序列,A 先手 B 后手,每个人每次可以将连续且相同朝向的硬币翻面,最大能连续翻 k

Tag 博弈论

题解 先遍历,将连续的部分的长度计入数组里。如果 alice 首次不能赢,那么永远也不能,只要劣势方 bob 始终和 alice 操作相同,最后就是平局

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const string a = "Alice";
const string b = "Bob";
const string c = ":(";

string solve() {
    vector<int> v;
    int n, k; cin >> n >> k;
    string s; cin >> s;
    int cnt = 1;
    for (int i = 1; i < n; i ++) {
        if (s[i] == s[i - 1]) {
            cnt ++;
        } else {
            v.push_back(cnt);
            cnt = 1;
        }
    }
    v.push_back(cnt);
    int size = v.size();
    if (size == 1 && v[0] <= k) return a;
    if (size == 1 && v[0] > k) return b;
    if (size == 2 && min(v[0], v[1]) <= k) return a;
    if (size == 3 && v[1] <= k) return a;
    if (n == 4) return b;
    return c;
}

int main() {
    cout << solve() << endl;
    return 0;
}

D-松鼠回家

题意 n 节点 m 条边的无向图,松鼠有 h 点体力,经过每条边会消耗 w[i] 的体力,没到一个点(包括起点)会扣除一定量松果,能到家的前提下,要使得扣的松果最少

Tag bfs 最短路 二分

题解 貌似不是 Dijkstra,因为用不到 vis[i] 状态,也用不到小根堆,bfs 写个最短路,二分即可

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#define aa first
#define bb second

using namespace std;
typedef pair<int, int> pii;
const int N = 1e4 + 2;
int n, m, st, ed, h;
int a[N], d[N];
vector<pii> to[N];

bool bfs(int x) {
    memset(d, 0x3f, sizeof d);
    queue<pii> q;
    q.push({0, st});
    d[st] = 0;
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        for (auto v : to[t.bb]) {
            int y = v.aa, w = v.bb;
            if (t.aa + w < d[y] && a[y] <= x) {
                if (d[ed] <= h) return true;
                d[y] = t.aa + w;
                q.push({d[y], y});
            }
        }
    }
    return false;
}

int main() {
    cin >> n >> m >> st >> ed >> h;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    for (int i = 0; i < m; i ++) {
        int a, b, c; cin >> a >> b >> c;
        to[a].push_back({b, c});
        to[b].push_back({a, c});
    }
    int l = 1, r = 1e7;
    while (l < r) {
        int mid = l + r >> 1;
        if (bfs(mid)) r = mid;
        else l = mid + 1;
    }
    if (!bfs(l)) l = -1;
    cout << l << endl;
    return 0;
}

E-动物朋友

题意 n 个动物排成一排,每个能带来 a[i] 的快乐值,有多少个连续的动物的快乐值之和大于 h

题解 queue 或者尺取法

#include <iostream>
#include <queue>

using namespace std;

int main() {
    queue<int> q;
    int n, m; cin >> n >> m;
    int res = 0, ans = 0;
    for (int i = 0; i < n; i ++) {
        int x; cin >> x;
        q.push(x);
        res += x;
        while (res >= m) {
            if (res == m) ans ++;
            int t = q.front();
            res -= t;
            q.pop();
        }
    }
    cout << ans << endl;
    return 0;
}

F-松鼠排序

题意 给定一个 1 ~ n 的一个排列,每次将其中两个数调换,需要至少调换多少次才能使得原排列按顺序

题解 正解是并查集,求有几个自环,将 n 减去自环数即是答案;这题也可直接用选择排序做,用一个 map 记录 i 位置当前位置应当在的数现在的位置,将其交换,注意 mapswap 会导致,原本数的位置数据丢失

#include <iostream>
#include <map>
#include <algorithm>
#define endl '\n'

using namespace std;
const int N = 1e5 + 2;
int n, a[N], b[N];
map<int, int> mp;

int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i], mp[a[i]] = i, b[i] = a[i];
    sort(b, b + n);
    int res = 0;
    for (int i = 0; i < n; i ++) {
        if (a[i] != b[i]) {
            res ++;
            a[mp[b[i]]] = a[i];
            mp[a[i]] = mp[b[i]];
        }
    }
    cout << res << endl;
    return 0;
}

G-Reverse

题意 给定一个 01 序列,求反转某一区间,使得最大连续的 1 区间最长,输出最长区间

题解 输出原始序列的最长和次长 1 区间之和即可

#include <iostream>
#include <queue>

using namespace std;

int main() {
    int n; string s; cin >> n >> s;
    priority_queue <int> heap;
    int cnt = 0;
    for (int i = 0; i < n; i ++) {
        if (s[i] == '0') {
            heap.push(cnt);
            cnt = 0;
        } else {
            cnt ++;
        }
    }
    heap.push(cnt);
    int res = heap.top();
    heap.pop();
    if (heap.size()) res += heap.top();
    cout << res << endl;
    return 0;
}

H-迷宫探险

题意 二维迷宫,. 代表道路,# 代表墙壁,* 代表弹射,一旦进入弹射格,必须选择四个方向的一个弹射 b[i][j] 格,走一格计一个单位,弹射不消耗时间,求最短路

Tag 最短路

#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#define endl '\n'
#define aa first
#define bb second

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 3e3 + 2;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
int n, m, k, b[N][N], d[N][N];
bool st[N][N];
char a[N][N];

int bfs() {
    queue<pii> q;
    q.push({0, 0});
    d[0][0] = 0;
    while (!q.empty()) {
        auto t = q.front();
        q.pop();
        if (a[t.aa][t.bb] == '.') {
            for (int i = 0; i < 4; i ++) {
                int x = t.aa + dx[i], y = t.bb + dy[i];
                if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == '#' || d[x][y] <= d[t.aa][t.bb] + 1) continue;
                d[x][y] = d[t.aa][t.bb] + 1;
                q.push({x, y});
            }
        } else {
            for (int i = 0; i < 4; i ++) {
                int x = t.aa + dx[i] * b[t.aa][t.bb], y = t.bb + dy[i] * b[t.aa][t.bb];
                if (x < 0 || y < 0 || x >= n || y >= m || a[x][y] == '#' || d[x][y] <= d[t.aa][t.bb]) continue;
                d[x][y] = d[t.aa][t.bb];
                q.push({x, y});
            }
        }
    }
    return d[n - 1][m - 1] == 0x3f3f3f3f ? -1 : d[n - 1][m - 1];
}

int main() {
    memset(d, 0x3f, sizeof d);
    cin >> n >> m;
    for (int i = 0; i < n; i ++) cin >> a[i];
    cin >> k;
    while (k --) {
        int x, y, w; cin >> x >> y >> w;
        b[x - 1][y - 1] = w;
    }
    cout << bfs() << endl;
    return 0;
}

J-合唱比赛

题意 已有 n 个评委给出分数,还剩一个没给,最后需要删除一个最高分和一个最低分,求平均分区间,答案保留六位小数

#include <iostream>

using namespace std;
const int N = 1e3 + 2;
int n, a[N], maxx = 1, minn = 100;
double res;

int main() {
    cin >> n;
    for (int i = 0; i < n; i ++) {
        cin >> a[i], res += a[i];
        maxx = max(maxx, a[i]);
        minn = min(minn, a[i]);
    }
    printf("%.6lf %.6lf", (res - maxx) / (n - 1), (res - minn) / (n - 1));
    return 0;
}

K-以撒和隐藏房间

题意 给定一个二维数组,0 表示普通房间,1 表示墙壁,2 表示 Boss 房间,其中有隐藏房间,满足四周有三个普通房间,输出可能的隐藏房间的数量

#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define endl '\n'
#define aa first
#define bb second

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e3 + 2;
int n, m;
char a[N][N];

int main() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++) cin >> a[i];
    queue<pii> ans;
    for (int i = 0; i < n; i ++) {
        for (int j = 0; j < m; j ++) {
            if (a[i][j] != '0' || i == 0 && j == 0 || i == 0 && j == m - 1 || i == n - 1 && j == 0 || i == n - 1 && j == m - 1) continue;
            int t = 0;
            if (i) {
                if (a[i - 1][j] == '1') t ++;
                else if (a[i - 1][j] == '2') t -= 10;
            }
            if (j) {
                if (a[i][j - 1] == '1') t ++;
                else if (a[i][j - 1] == '2') t -= 10;
            }
            if (i != n - 1) {
                if (a[i + 1][j] == '1') t ++;
                else if (a[i + 1][j] == '2') t -= 10;
            }
            if (j != m - 1) {
                if (a[i][j + 1] == '1') t ++;
                else if (a[i][j + 1] == '2') t -= 10;
            }
            if (t == 3) ans.push({i, j});
        }
    }
    if (ans.size()) {
        puts("YES");
        cout << ans.size() << endl;
    } else {
        puts("NO");
    }
    return 0;
}

L-中位数

题意 给定一个数组,每次更新指定的一个数,输出每次更新后的中位数

Tag 树状数组 二分

题解 对于每个数 a[i],都 update(a[i], 1) 使得大于等于 a[i] 的都增加 1,大于等于 a[i] 的有 tree[a[i]] 个,二分查找中点数

#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
#include <algorithm>
#define endl '\n'
#define aa first
#define bb second

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e6 + 2;
int n, m, tree[N], a[N];

inline int lowbit(int x) {
    return x & -x;
}

inline void update(int x, int k) {
    while (x < N) {
        tree[x] += k;
        x += lowbit(x);
    }
}

inline int query(int x) {
    int res = 0;
    while (x) {
        res += tree[x];
        x -= lowbit(x);
    }
    return res;
}

int main() {
    cin >> n >> m;
    int cnt = n / 2 + 1;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        update(a[i], 1);
    }
    while (m --) {
        int p, x; cin >> p >> x;
        update(a[p], -1);
        a[p] = x;
        update(x, 1);
        int l = 1, r = N;
        while (l < r) {
            int mid = l + r >> 1;
            if (query(mid) >= cnt) r = mid;
            else l = mid + 1;
        }
        cout << l << endl;
    }
    return 0;
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JorbanS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值