首页 > 其他分享 >AtCoder Beginner Contest 283(A~F)

AtCoder Beginner Contest 283(A~F)

时间:2022-12-25 15:44:22浏览次数:41  
标签:Case AtCoder return Beginner int else 283 true define

A

a,b=map(int,input().split())
print(pow(a,b))

B

n=int(input())
a=list(map(int,input().split()))
q=int(input())
for i in range(q):
    op=list(map(int,input().split()))
    if op[0]==1:
        a[op[1]-1]=op[2]
    else:
        print(a[op[1]-1])

C

两个连续的00看作一个即可

S =list(input())

ans = len(S)

for i in range(1,len(S)):
    if S[i-1]=="0" and S[i]=="0":
        S[i]="x"
        ans-=1
print(ans)

D

一开始看错题了,不需要字符串最后为空,只需要判断两个括号之间的字母是否重复即可

#include <bits/stdc++.h>
int _ = 0, Case = 1;
#define int long long
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
#define SZ(v) (int) v.size()

const int N = 300010;
char s[N];
void solve(int Case) {
    cin >> s + 1;
    int n = strlen(s + 1);
    map<int, int> vis;
    stack<map<char, int>>stk;
    for (int i = 1; i <= n; i++) {
        char c = s[i];
        if (stk.size()) {
            auto t = stk.top();
            if (c == ')') {
                stk.pop();
                for (char j = 'a'; j <= 'z'; j++) {
                    if (t[j]) vis[j] = 0;
                }
            } else if (c == '(') {
                map<char, int> tem;
                stk.push(tem);
            } else {
                if (vis[c]) {
                    cout << "No" << nline;
                    return;
                }
                stk.pop();
                t[c] = 1;
                vis[c] = 1;
                stk.push(t);
            }
        } else {
            if (c == ')') {
            } else if (c == '(') {
                map<char, int> tem;
                stk.push(tem);
            } else {
                if (vis[c]) {
                    cout << "No" << nline;
                    return;
                }
                vis[c] = 1;

            }
        }
    }
    cout << "Yes" << nline;




}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);


//   for (cin>>_, Case = 1; Case <= _; Case++)
    solve(Case);

    return 0;
}

E

动态规划,f[i][j][k],表示当前操作到第i行,并且第i行状态时j,i-1的状态时k,转移就是f[i][j][k]=f[i-1][k][kk],kk是第i-2行的状态;

#include <bits/stdc++.h>
int _ = 0, Case = 1;
#define int long long
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
#define SZ(v) (int) v.size()
const int N = 2020;
int a[N][N];
int n, m;
int f[N][2][2];
bool check(int c, int x, int y, int z) {
    c--;
    if (c == 0) {
        return true;
    } else if (c == 1) {

        for (int i = 1; i <= m; i++) {
            bool ok = false;
            if ((a[c][i]^y) == (a[c + 1][i]^x)) ok = true;
            if (i + 1 <= m and a[c][i] == a[c][i + 1]) ok = true;
            if (i - 1 >= 1 and a[c][i] == a[c][i - 1]) ok = true;
            if (!ok) return false;
        }
        return true;
    } else if (c != n - 1) {
        for (int i = 1; i <= m; i++) {
            bool ok = false;
            if (((a[c][i]^y) == (a[c - 1][i]^z)) or ((a[c][i]^y) == (a[c + 1][i]^x))) ok = true;
            if (i + 1 <= m and a[c][i] == a[c][i + 1]) ok = true;
            if (i - 1 >= 1 and a[c][i] == a[c][i - 1]) ok = true;
            if (!ok) return false;
        }
        return true;
    } else {
        c++;
        for (int i = 1; i <= m; i++) {
            bool ok = false;
            if ((a[c][i]^x) == (a[c - 1][i]^y)) ok = true;
            if (i + 1 <= m and a[c][i] == a[c][i + 1]) ok = true;
            if (i - 1 >= 1 and a[c][i] == a[c][i - 1]) ok = true;
            if (!ok) return false;
        }
        return true;
    }
}
void solve(int Case) {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }

    memset(f, 0x3f, sizeof f);
    f[0][0][0] = 0;
    f[0][1][0] = 0;
    f[0][1][1] = 0;
    f[0][0][1] = 0;
    for (int i = 1; i <= n; i++) {
        for (int x = 0; x < 2; x++) {
            for (int y = 0; y < 2; y++) {
                for (int z = 0; z < 2; z++) {
                    if (check(i, x, y, z)) {
                        f[i][x][y] = min(f[i - 1][y][z] + x, f[i][x][y]);
                    }
                }
            }
        }
    }
    int res = 1e9;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            res = min(res, f[n][i][j]);
        }
    }
    if (res == 1e9) res = -1;
    cout << res << nline;




}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);


//   for (cin>>_, Case = 1; Case <= _; Case++)
    solve(Case);

    return 0;
}

F

假如只看一边,那么就是对于前i个,把数字分为大于x和小于x,那么结果分别是p[j]-j-x+i,-p[j]-j+x+i,这一步可以使用权值线段树完成。

#include <bits/stdc++.h>
int _ = 0, Case = 1;
#define int long long
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'
#define SZ(v) (int) v.size()
const int N = 200010;
int p[N];
#define ls(x) x<<1
#define rs(x) x<<1 |1
struct tree {
    int l, r;
    int aa;
    int bb;
};
struct Segment_Tree {
    tree tr[N << 2];
    int pos[N];
    void pushup(tree &p, tree &l, tree &r) {
        p.aa = min(l.aa, r.aa);
        p.bb = min(l.bb, r.bb);
    }
    void pushup(int p) {
        pushup(tr[p], tr[ls(p)], tr[rs(p)]);
    }
    void build(int p, int l, int r) {
        if (l == r) {
            tr[p] = {l, r, 1000000000, 1000000000};
            pos[l] = p;
        }
        else {
            tr[p] = {l, r};
            int mid = l + r >> 1;
            build(ls(p), l, mid);
            build(rs(p), mid + 1, r);
            pushup(p);
        }
    }
    void modifyA(int p, int x, int y) {
        p = pos[x];
        tr[p].aa = y;
        for (; p >>= 1;) pushup(p);
    }
    void modifyB(int p, int x, int y) {
        p = pos[x];
        tr[p].bb = y;
        for (; p >>= 1;) pushup(p);
    }
    tree query(int p, int l, int r) {
        if (tr[p].l >= l and tr[p].r <= r) return tr[p];
        int mid = tr[p].l + tr[p].r >> 1;
        if (r <= mid) return query(ls(p), l, r);
        else if (l > mid) return query(rs(p), l, r);
        else {
            tree ret;
            auto left = query(ls(p), l, r);
            auto right = query(rs(p), l, r);
            pushup(ret, left, right);
            return ret;
        }
    }
};
Segment_Tree ST;
int ans[N];
void solve(int Case) {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> p[i], ans[i] = 1e9;
    ST.build(1, 1, n);
    //xiao x+i-(p[j]+j),aa=-p[j]-j
    //da (p[j]-j)+i-x,bb=p[j]-j;    
    for (int i = 1; i <= n; i++) {
        int x = p[i];
        int t = ST.query(1, 1, x).aa;
        int t1 = ST.query(1, x, n).bb;
        ans[i] = min({ans[i], x + i+t, t1 + i - x});
        ST.modifyA(1, x, -x - i);//xiao
        ST.modifyB(1, x, x - i);//da
    }
    ST.build(1, 1, n);
    reverse(p + 1, p + 1 + n);
    for (int i = 1; i <= n; i++) {
        int x = p[i];
        int t = ST.query(1, 1, x).aa;
        int t1 = ST.query(1, x, n).bb;
        ans[n-i+1] = min({ans[n-i+1], x + i+t, t1 + i - x});
        ST.modifyA(1, x, -x - i);//xiao
        ST.modifyB(1, x, x - i);//da
    }
    for (int i = 1; i <= n; i++) {
        cout << ans[i] << ' ';
    }
    cout << nline;




}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);


//   for (cin>>_, Case = 1; Case <= _; Case++)
    solve(Case);

    return 0;
}

标签:Case,AtCoder,return,Beginner,int,else,283,true,define
From: https://www.cnblogs.com/koto-k/p/17004099.html

相关文章

  • AT282 C+AT283 D
    c:题目大意是给定字符串只含有小写,','和'';保证''的数量是偶数个的同时让2k为''的个数规定从1......k的i,每个2*i-1~2*i区间内的字符是封闭的你的任务是用'.'替换掉不在......
  • ABC 283 ABCD
    https://atcoder.jp/contests/abc283A-Power题目大意:输出a的b次方。SampleInput143SampleOutput164#include<bits/stdc++.h>usingnamespacestd;......
  • 「杂题乱写」AtCoderDP26 题
    「杂题乱写」AtCoderDP26题\(\text{AtCoderDP26}\)题题单。前言最近听说\(\text{AtCoder}\)上有个\(\text{DP26}\)题挺好的,于是向@\(\text{SoyTony}\)要了题单......
  • AtCoder Beginner Contest 283
    A-Power(abc283a)题目大意给定\(A,B\),输出\(A^B\)解题思路数不大,暴力即可。数大了可用快速幂。神奇的代码#include<bits/stdc++.h>usingnamespacestd;us......
  • abc--283--E
    关键跟炮兵阵地那道题目很像,先确定上面哪一行的状态,然后在确定下面这一行的状态,采用dp就可以枚举所有的状态代码#include<bits/stdc++.h>usingnamespacestd;const......
  • abc 283 E Don't Isolate Elements
    abc283EDon'tIsolateElements题意:给出一个\(h*w\)的01矩阵,对于每一行,可以进行翻转操作。如果\(a_{i,j}\)的上下左右没有一个和它数值一样的话,这个点就被称......
  • atcoder
    AGC001D题目大意:有一个长度为\(m\)的序列\(a\),它的和为\(n\),需要将\(a\)重排,并构造一个任意长度但和为\(n\)的序列\(b\),使得对任意长度为\(n\)的字符串,如果......
  • 283. 移动零
    给定一个数组nums,编写一个函数将所有0移动到数组的末尾,同时保持非零元素的相对顺序。请注意,必须在不复制数组的情况下原地对数组进行操作。示例1:输入:nums=[0,......
  • [翻译]写给初学者的源代码安装指南Beginner's Guide to Installing from Source
    写给初学者的源代码安装指南引入本文档面向希望直接从原始作者处安装软件的开源操作系统用户,而不是仅依赖其操作系统提供的软件(和版本)。它是为那些不熟悉以源代码形式下......
  • AtCoder Beginner Contest 282
    https://atcoder.jp/contests/abc282A-GeneralizedABC原题链接题意给出一个数\(k\),输出A~Z中的前\(k\)个字母。分析从\(0\)到\(k\)枚举,将A加上\(i\)输......