2023ICPC区域赛杭州站

VP链接:Dashboard - The 2023 ICPC Asia Hangzhou Regional Contest (The 2nd Universal Cup. Stage 22: Hangzhou) - Codeforces

D. Operator Precedence

一道构造题,将序列构造成 1 2 -1 2 -1 ...... 2 -1 x。根据题目条件,可以推出来 x = 3 - n。由于 x 不等于 0,那么这个序列只有在 n \not= 3 的时候成立。n = 3 的时候,直接输出样例中的答案即可。

#include <bits/stdc++.h>
using namespace std;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

signed main() {
    int t = read();
    while (t--) {
        int n = read();
        if (n == 3) { puts("1 -10 6 6 -10 1"); continue; }
        printf("1 ");
        for (int i = 1; i < n; i++) printf("2 -1 ");
        printf("%d\n", 3 - n);
    }
	return 0;
}

G. Snake Move

最短路问题。起点就是蛇的头,终点是二维网格中的所有点。利用 vis 数组记录障碍和身体的位置(需要多少步才不会再占用该格子)。利用 dijsktra 求最短路。

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef unsigned long long ull;

const int N = 3e3 + 10, dx[4] = { 0, 0, 1, -1 }, dy[4] = { 1, -1, 0, 0 };
int n, m, k;
int vis[N][N], f[N][N];
struct node {
    int x, y, dis;
    bool operator<(const node &t) const { return t.dis < dis; }
};
priority_queue<node> q;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

signed main() {
    n = read(), m = read(), k = read();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) vis[i][j] = 0, f[i][j] = INT_MAX;
    for (int i = k; i; i--) {
        int x = read(), y = read();
        vis[x][y] = i; // 记录需要多久不占用该格子
        if (i == k) q.push({x, y, 0}), f[x][y] = 0; // 放入蛇头位置
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (getchar() == '#') vis[i][j] = -1; // 记录障碍
        }
        getchar();
    }
    while (!q.empty()) {
        node cur = q.top(); q.pop();
        int x = cur.x, y = cur.y;
        if (cur.dis != f[x][y]) continue;
        // f 数组更新后,优先队列里的 dis 无法及时更新
        for (int i = 0; i < 4; i++) {
            int xx = x + dx[i], yy = y + dy[i];
            if (xx < 1 || xx > n || yy < 1 || yy > m || vis[xx][yy] == -1) continue;
            if (f[xx][yy] > f[x][y] + 1) {
                f[xx][yy] = max(f[x][y] + 1, vis[xx][yy]);
                q.push({xx, yy, f[xx][yy]});
            }
        }
    }
    ull ans = 0; // 自然溢出,相当于对 2^64 取模
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (f[i][j] != INT_MAX) ans += f[i][j] * f[i][j];
    printf("%llu", ans);
	return 0;
}

H. Sugar Sweet II

  1. a[i] < a[b[i]] 的时候,无论 a[b[i]] 是否更新,即无论事件以什么顺序发生,a[i] 一定会被更新,则第 i 个孩子手上的糖的期望为 a[i] + w[i]。
  2. 当 a[i] \ge a[b[i]] + w[b[i]] 的时候,无论 a[b[i]] 是否更新,即无论事件以什么顺序发生,a[i] 一定无法更新,则第 i 个孩子手上的糖的期望为 a[i]。
  3. 除去以上两种情况,a[i] 是否更新取决于 a[b[i]] 是否更新。对于这种情况,我们可以以 (b[i], i) 建一张有向图,记录一个 dis 表示该节点更新前有多少个节点需要更新,第一种情况的 dis 记为 1,第二种情况的 dis 记为 0,通过 dfs 遍历得到每一个节点的 dis。每一个节点可以更新的概率就是 \frac{1}{dis!},因为只需要这 dis 个节点以这一种特定顺序排列就可以保证该节点被更新。期望就是 \frac{1}{dis!} (a[i] + w[i]) + (1 - \frac{1}{dis!})a[i] = a[i] + \frac{w[i]}{dis!},最后要求乘法逆元。
  • 对于 \frac{x}{y}\equiv ans(mod \hspace{0.5em}p) 即 \frac{ans}{x} \cdot y \equiv 1 (mod \hspace{0.5em} p) 的乘法逆元,根据费马小定理,当 a 和 p 互质的时候,有 a^{p - 1} \equiv 1 (mod \hspace{0.5em} p),则 \frac{ans}{x} = y ^{p - 2} \hspace{0.2em} \% \hspace{0.2em} p 即 ans = x \cdot y ^{p - 2} \hspace{0.2em} \% \hspace{0.2em} p
#include <bits/stdc++.h>
using namespace std;

#define int long long

const int N = 5e5 + 10, mod = 1e9 + 7;
int a[N], b[N], w[N], fac[N], dis[N], head[N], num = 0;
struct edge { int to, nxt; } e[N];

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

void addEdge(int u, int v) { e[++num] = (edge){v, head[u]}, head[u] = num; }

int qpow(int x, int k) {
    int res = 1LL;
    while (k) {
        if (k & 1) res = res * x % mod;
        x = x * x % mod;
        k >>= 1;
    }
    return res;
}

void dfs(int u) {
    for (int i = head[u], v; i; i = e[i].nxt) {
        v = e[i].to;
        dis[v] = dis[u] + 1;
        dfs(v);
    }
}

signed main() {
    fac[0] = 1;
    for (int i = 1; i < N; i++) fac[i] = fac[i - 1] * i % mod;
    int t = read();
    while (t--) {
        int n = read();
        for (int i = 1; i <= num; i++) e[i] = {0, 0}; // 初始化
        num = 0;
        for (int i = 1; i <= n; i++) a[i] = read(), dis[i] = 0;
        for (int i = 1; i <= n; i++) b[i] = read(), head[i] = 0;
        for (int i = 1; i <= n; i++) w[i] = read();
        for (int i = 1; i <= n; i++) {
            if (a[i] < a[b[i]]) dis[i] = 1;
            else if (a[i] >= a[b[i]] + w[b[i]]) dis[i] = 0;
           else addEdge(b[i], i); // 只有第三种情况才加边
        }
        for (int i = 1; i <= n; i++) if (dis[i] == 1) dfs(i);
        for (int i = 1; i <= n; i++) {
            if (dis[i] == 0) printf("%lld ", a[i]);
            else printf("%lld ", (a[i] + w[i] * qpow(fac[dis[i]], mod - 2) % mod) % mod);
        }
        puts("");
    }
	return 0;
}

J. Mysterious Tree

要判断一个图是否是菊花图,只需要判断是否有任意一个节点与至少三个节点相连。

#include <bits/stdc++.h>
using namespace std;

int a[5], n;

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

int query(int u, int v) {
    cout << "? " << u << " " << v << endl;
    int opt = read();
    return opt;
}

bool check(int u, int v) {
    int k = 0;
    for (int i = 1; i <= n; i++) {
        if (i != u && i != v) a[++k] = i;
        if (k == 2) break;
    }
    return (query(u, a[1]) && query(u, a[2])) || (query(v, a[1]) && query(v, a[2]));
    // 判断 u 与 a[1] 和 a[2] 都相连,或者 v 与 a[1] 和 a[2] 都相连
    // 如果为真,则为菊花图,否则为链
}

void solve() {
    n = read();
    bool flag = 0;
    for (int i = 1; i <= n; i += 2) {
        if (query(i, i % n + 1)) {
            flag = check(i, i % n + 1); // n 为奇数时,需要检查 1 和 n 是否有连边
            break;
        }
    }
    if (flag) cout << "! 2" <<endl;
    else cout << "! 1" << endl;
}

int main() {
    ios::sync_with_stdio(false); cout.tie(0);
    int t = read();
    while (t--) {
        solve();
    }
    return 0;
}

M. V-Diagram

找出给的 V-Diagram a中的最小值的位置 idx,V-Diagram b 有三种情况。

  1. 左一右多
  2. 左多右一
  3. 左多右多

idx 的两边要么取一个,要么取全部。

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define double long double

const int N = 3e5 + 10;
int a[N], sum[N];

inline int read() {
	int x = 0, f = 1; char c = getchar();
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

signed main() {
    int t = read();
    while (t--) {
        int idx = 1, minm = INT_MAX, n = read();
        for (int i = 1; i <= n; i++) {
            a[i] = read(), sum[i] = sum[i - 1] + a[i];
            if (a[i] < minm) idx = i, minm = a[i];
        }
        printf("%.12Lf\n", max(max((double)(sum[n] - sum[idx - 2]) / (n - idx + 2), (double)sum[idx + 1] / (idx + 1)), (double)sum[n] / n));
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值