首页 > 其他分享 >AtCoder Beginner Contest 339 A-G

AtCoder Beginner Contest 339 A-G

时间:2024-02-07 22:22:26浏览次数:31  
标签:tmp AtCoder 339 Beginner int void mid vector id

A

模拟即可

void solve()
{
    string s;
    cin >> s;
    for (int i = s.size() - 1; i >= 0; i--)
        if (s[i] == '.')
        {
            cout << s.substr(i + 1) << endl;
            return;
        }
}

B

模拟,可以让下标从0开始,这样可以在%n下满足题意

int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
void solve()
{
    int n, m, opt;
    cin >> n >> m >> opt;
    vector<vector<char>> a(n, vector<char>(m, '.'));
    int d = 0, x = 0, y = 0;
    while (opt--)
    {
        if (a[x][y] == '.')
        {
            a[x][y] = '#';
            d = (d + 1) % 4;
        }
        else
        {
            a[x][y] = '.';
            d = (d - 1 + 4) % 4;
        }
        x = (x + dx[d] + n) % n;
        y = (y + dy[d] + m) % m;
        // cout << x << " " << y << endl;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cout << a[i][j];
        cout << endl;
    }
}

C

从后往前吗模拟:
设当前最少有x人
如果是\(a[i]<0\),则在到达该站前至少\(abs(a[i])\)人
如果是\(a[i]>0\),则在到达该站前可以少\(abs(a[i])\)人
最后再顺着模拟一遍得到答案

void solve()
{
    ll n;
    cin >> n;
    vector<ll> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    ll ans = 0;
    for (int i = n; i >= 1; i--)
    {
        if (a[i] > 0)
        {
            ans -= a[i];
            ans = max(0ll, ans);
        }
        else
            ans -= a[i];
    }
    for (int i = 1; i <= n; i++)
        ans += a[i];
    cout << ans << endl;
}

D

同时维护两人的坐标,进行bfs

ll dist[70][70][70][70];
int d[4][4] = {{-1, 1, 0, 0}, {0, 0, -1, 1}, {-1, 1, 0, 0}, {0, 0, -1, 1}};
void solve()
{
    int n, cnt = 0;
    cin >> n;
    array<int, 4> st;
    vector<vector<char>> a(n + 1, vector<char>(n + 1));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
            if (a[i][j] == 'P')
                st[cnt++] = i, st[cnt++] = j;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            for (int k = 1; k <= n; k++)
                for (int l = 1; l <= n; l++)
                    dist[i][j][k][l] = 1e9;

    auto bfs = [&](array<int, 4> x) -> void
    {
        queue<array<int, 4>> q;
        q.push(x);
        dist[x[0]][x[1]][x[2]][x[3]] = 0;
        while (q.size())
        {
            auto t = q.front();
            q.pop();
            for (int i = 0; i < 4; i++) // 方向
            {
                array<int, 4> tmp;
                for (int j = 0; j < 4; j++)
                {
                    tmp[j] = t[j] + d[j][i];
                    tmp[j] = min(n, tmp[j]);
                    tmp[j] = max(1, tmp[j]);
                }
                if (a[tmp[0]][tmp[1]] == '#')
                    tmp[0] = t[0], tmp[1] = t[1];
                if (a[tmp[2]][tmp[3]] == '#')
                    tmp[2] = t[2], tmp[3] = t[3];

                if (dist[tmp[0]][tmp[1]][tmp[2]][tmp[3]] > dist[t[0]][t[1]][t[2]][t[3]] + 1)
                {
                    dist[tmp[0]][tmp[1]][tmp[2]][tmp[3]] = dist[t[0]][t[1]][t[2]][t[3]] + 1;
                    q.push(tmp);
                }
            }
        }
    };
    bfs(st);
    ll ans = 1e9;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (a[i][j] == '.')
                ans = min(ans, dist[i][j][i][j]);
    if (ans == 1e9)
        ans = -1;
    cout << ans << endl;
}

E

线段树优化dp,维护转移范围内的最大值

struct node
{
    ll val;
};
struct segtree
{
    int n;
    vector<node> a;
    segtree(int _n) : n(_n * 4 + 10), a(n + 1) {}
    void update(int id) { a[id].val = max(a[id * 2].val, a[id * 2 + 1].val); }
    void build(int id, int l, int r, vector<int> &arr)
    {
        if (l == r)
            a[id].val = arr[l];
        else
        {
            int mid = l + r >> 1;
            build(id * 2, l, mid, arr);
            build(id * 2 + 1, mid + 1, r, arr);
            update(id);
        }
    }
    void change(int id, int l, int r, int pos, int val)
    {
        if (l == r) // 叶子节点
            a[id].val = val;
        else
        {
            int mid = l + r >> 1;
            if (pos <= mid)
                change(id * 2, l, mid, pos, val);
            else
                change(id * 2 + 1, mid + 1, r, pos, val);
            update(id);
        }
    }
    ll query(int id, int l, int r, int ql, int qr)
    {
        if (l == ql && r == qr)
            return a[id].val;
        int mid = l + r >> 1;
        if (qr <= mid)
            return query(id * 2, l, mid, ql, qr);
        else if (ql > mid)
            return query(id * 2 + 1, mid + 1, r, ql, qr);
        else
            return max(query(id * 2, l, mid, ql, mid), query(id * 2 + 1, mid + 1, r, mid + 1, qr));
    }
};
void solve()
{
    int n, d;
    cin >> n >> d;
    vector<int> a(n + 1), rec(N, 0);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    segtree seg(N);
    seg.build(1, 1, n, rec);

    for (int i = 1; i <= n; i++)
    {
        int l = max(1, a[i] - d), r = min((int)5e5, a[i] + d);
        int res = seg.query(1, 1, N, l, r) + 1;
        int now = max(seg.query(1, 1, N, a[i], a[i]), 1ll);
        now = max(now, res);
        seg.change(1, 1, N, a[i], now);
    }
    ll res = 1;
    for (int i = 1; i <= n; i++)
        res = max(res, seg.query(1, 1, N, a[i], a[i]));
    cout << res << endl;
}

标签:tmp,AtCoder,339,Beginner,int,void,mid,vector,id
From: https://www.cnblogs.com/Chesedss/p/18011402

相关文章

  • AtCoder-ABC-Ex乱写
    ABC233ExManhattanChristmasTree先将\((x,y)\)变成\((x+y,x-y)\),也就是曼哈顿转切比雪夫,之后曼哈顿距离\(\lek\)的在切比雪夫坐标系下就是一个正方形。用主席树做矩形和,外层套一个二分即可,时间复杂度\(\mathcal{O}(n\log^2n)\)。ABC233Ex#include<bits/stdc++.h......
  • 我也要板刷 AtCoder!
    板刷AtCoderARCC![ARC171C]SwaponTreeProblem给定一棵\(n\)个节点的树,每个点有个权值\(a_i\),初始时\(a_i=i\)。你可以执行任意操作:选择一条边\((u,v)\),交换\(a_u\)和\(a_v\),并将这条边删掉。问通过上述操作,最后\((a_1,a_2,\cdots,a_n)\)有多少种不同的排列方......
  • Rockchip RK3399 - PCIe
    一、PCIe调试1.1编译内核1.1配置设备树pcie设备节点定义在arch/arm64/boot/dts/rockchip/rk3399.dtsi;pcie0:pcie@f8000000{ compatible="rockchip,rk3399-pcie"; reg=<0x00xf80000000x00x2000000>, <0x00xfd0000000x00x1000000>; reg-names=......
  • AtCoder Beginner Contest 339
    AtCoderBeginnerContest339A-TLD签到B-Langton'sTakahashi发现步数和网格范围都不大,直接模拟即可C-PerfectBus题目的合法性在于不能为负数,那么初始人数就有了二分性。二分的找出初始的最小人数,然后跑一边即可#include<bits/stdc++.h>usingnamespacestd;#d......
  • Japan Registry Services (JPRS) Programming Contest 2024 (AtCoder Beginner Contes
    JapanRegistryServices(JPRS)ProgrammingContest2024(AtCoderBeginnerContest339)A-TLD代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__......
  • P3397 地毯
    原题链接二维差分的简单应用。作为学二维差分时的练手题很不错。主要代码:#include<bits/stdc++.h>usingnamespacestd;constintN=1002;inta[N][N];intmain(){ios::sync_with_stdio(false);intn,m;cin>>n>>m;for(inti=1;i<=m;i++){in......
  • AtCoder Beginner Contest 339
    A找到最后一个点的位置,使用substr函数。B模拟,记得边界是循环的。C容易发现答案为:\[\min_p(\sum_{i=1}^{p}a_i)+\sum_{i=1}^{n}a_i\]D由于不同状态数只有\(60^4\),容易搜索,但是我去吃饭了,所以没能快速切掉。E有一个显然的dp,然后考虑每一个\(a_i\)可以转移到\(a_j\)......
  • abc339 详解
    第一篇整场题解纪念我第一次AK的abc!A从后往前找到第一个'.'然后输出'.'到字符串结尾构成的字符串。#include<iostream>usingnamespacestd;intmain(intargc,constchar*argv[]){stringstr;cin>>str;intlen=(int)str.length();stri......
  • AtCoder Beginner Contest 330
    A-CountingPasses#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongusingvi=vector<int>;usingi32=int32_t;usingpii=pair<int,int>;#definempmake_pairconstintinf=1e9;i32main(){intn,l;......
  • AtCoder Beginner Contest 339
    基本情况A和C出的比较快但不能说秒了还是思考了几分钟的,然后B很奇怪我样例还有一些测试点都能过,但有些测试点就是过不了...A-TLD貌似没啥说的B-Langton'sTakahashi说实话现在还是不懂我的哪里错了很不科学啊,明明很多测试点都过了啊C-PerfectBus做题时的思路:要想求......