AtCoder Beginner Contest 320

A - Leyland Number

a, b = map(int, input().split(' '))
print( a ** b + b ** a )

B - Longest Palindrome

s = input()
n = len(s)
res = 0
for l in range(1, n + 1):
    for i in range(0, n - l):
        t = q = s[i: i + l]
        t = t + t[::-1]
        q = q + q[-2:: -1]
        if t in s:
            res = max(res, len(t))
        if q in s:
            res = max(res, len(q))

print(res)

C - Slot Strategy 2 (Easy)

最多只会执行\(3M\)所以可以暴力的枚举按按钮的方式

m = int(input())
s1 = input()
s2 = input()
s3 = input()

res = 1e9

for i in range(3 * m):
    for j in range(3 * m):
        if i == j:
            continue
        for k in range(3 * m):
            if i == k or j == k:
                continue
            if s1[i % m] == s2[j % m] and s2[j % m] == s3[k % m]:
                res = min(res, max(i, j, k))
print(res if res < 1e9 else -1)

D - Relative Position

从 1 开始做搜索,逐渐确定每个人的坐标,遇到冲突直接结束。

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define mp make_pair
using pii = pair<int, int>;

pii operator+(pii a, pii b) {
    return mp(a.first + b.first, a.second + b.second);
}

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, pii>>> e(n + 1);
    for (int u, v, x, y; m; m--) {
        cin >> u >> v >> x >> y;
        e[u].emplace_back(v, mp(x, y));
        e[v].emplace_back(u, mp(-x, -y));
    }
    vector<pii> pos(n + 1);
    vector<int> vis(n + 1);
    queue<int> q;
    q.push(1), vis[1] = 1;
    for (int u; !q.empty();) {
        u = q.front(), q.pop();
        for (auto [v, d]: e[u]) {
            if (vis[v]) {
                if (pos[v] == pos[u] + d) continue;
                vis[v] = 2;
            } else vis[v] = 1, pos[v] = pos[u] + d, q.push(v);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (vis[i] == 1) cout << pos[i].first << " " << pos[i].second << "\n";
        else cout << "undecidable\n";
    }
    return 0;
}

E - Somen Nagashi

set维护去维护当前的队列中的,和等待进入队列的人,每次操作前先把应该入队的人重新插入到队列中。

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define mp make_pair
using pii = pair<int, int>;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    vector<int> cnt(n + 1);
    set<int> now;
    set<pii> que;
    for (int i = 1; i <= n; i++)
        now.insert(i);
    for (int t, w, s , x ; m; m--) {
        cin >> t >> w >> s;
        while( !que.empty() and que.begin()->first <= t )
            now.insert( que.begin()->second ) , que.erase(que.begin());
        if( now.empty() ) continue;
        x = *now.begin() , cnt[x] += w , now.erase(x) , que.emplace( t + s , x );
    }
    for( int i = 1 ; i <= n ; i ++ )
        cout << cnt[i] << "\n";
    return 0;
}

F - Fuel Round Trip

\(f[i][j][k]\)表示在位置\(i\),去时剩\(j\)升油,回时剩\(k\)升油的最小花费。

暴力枚举当前的状态,倒推出之前的状态即可。代码实现上可以利用滚动数组优化掉一维。

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define mp make_pair
using pii = pair<int, int>;
using vi = vector<int>;
constexpr int inf = 1e18;

int32_t main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    int n, h;
    cin >> n >> h;
    vector<int> x(n + 1), a(n + 1), b(n + 1);
    for (int i = 1; i <= n; i++) cin >> x[i];
    for (int i = 1; i < n; i++) cin >> a[i] >> b[i];
    vector<vi> f(h + 1, vi(h + 1)), g;
    for (int i = 1, dis; i <= n; i++) {
        dis = x[i] - x[i - 1], g = vector(h + 1, vi(h + 1, inf));
        for (int j = 0; j <= h; j++)
            for (int k = 0; k <= h; k++) {
                if (j + dis <= h and k - dis >= 0)
                    g[j][k] = min(g[j][k], f[j + dis][k - dis]);
                if (j + dis <= h and k - dis >= 0)
                    g[min(j + b[i], h)][k] = min(g[min(j + b[i], h)][k], f[j + dis][k - dis] + a[i]);
                if (j + dis <= h and min(k + b[i], h) - dis >= 0)
                    g[j][k] = min(g[j][k], f[j + dis][min(k + b[i], h) - dis] + a[i]);
            }
        f.swap(g);
    }
    int res = inf;
    for (int i = 0; i <= h; i++) res = min(res, f[i][i]);
    if (res == inf) cout << "-1\n";
    else cout << res << "\n";
    return 0;
}
posted @ 2023-09-18 21:32  PHarr  阅读(24)  评论(0编辑  收藏  举报