首页 > 其他分享 >AtCoder Beginner Contest 126

AtCoder Beginner Contest 126

时间:2023-01-26 10:33:42浏览次数:56  
标签:AtCoder Beginner int namespace cin fa 126 ans include

AtCoder Beginner Contest 126

https://atcoder.jp/contests/abc126

A - Changing a Character

#include <bits/stdc++.h>

using namespace std;

int main () {
    int n, k;
    string s;
    cin >> n >> k >> s;
    s[k-1] = (char)(s[k-1] - 'A' + 'a');
    cout << s;
}

B - YYMM or MMYY

#include <bits/stdc++.h>

using namespace std;
set<string> month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

int main () {
    string s;
    cin >> s;
    bool ok1 = false, ok2 = false;
    string s1 = s.substr (0, 2), s2 = s.substr (2, 2);
    if (month.count (s1))   ok1 = true;
    if (month.count (s2))   ok2 = true;
    if (ok1 && ok2)     cout << "AMBIGUOUS";
    else if (ok1)       cout << "MMYY";
    else if (ok2)       cout << "YYMM";
    else                cout << "NA";
}

C - Dice and Coin

#include <bits/stdc++.h>

using namespace std;

int main () {
    int n, k;
    cin >> n >> k;
    double ans = max (0.0, 1.0 * (n - k + 1) / n);
    //cout << ans << endl;
    for (int i = 1; i < min (k, n + 1); i++) {
        double dx = 1.0 / n;
        for (int j = i; j < k; j *= 2)  dx *= 0.5;
        //cout << dx << ' ';
        ans += dx;
    }
    cout << fixed << setprecision (10) << ans;
}

D - Even Relation

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5, M = N * 2;
int h[N], e[M], ne[M], w[M], idx, n, ans[N];

void add (int a, int b, int c) {
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

void dfs (int u, int fa) {
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (j == fa)    continue;
        if (w[i] & 1)   ans[j] = !ans[u];
        else    ans[j] = ans[u];
        dfs (j, u);
    }
}

int main () {
    memset (h, -1, sizeof h);
    cin >> n;
    for (int i = 1; i < n; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        add (a, b, c), add (b, a, c);
    }
    dfs (1, -1);
    for (int i = 1; i <= n; i++)    cout << ans[i] << endl;
}
//嗯搜, 暴力改

E - 1 or 2

#include <bits/stdc++.h>

using namespace std;
const int N = 1e5 + 5;
int fa[N], n, m, ans;

int find (int x) {
    if (x != fa[x])     fa[x] = find (fa[x]);
    return fa[x];
}

int main () {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)    fa[i] = i;
    while (m --) {
        int a, b, c;
        cin >> a >> b >> c;
        a = find (a), b = find (b);
        if (a != b)     fa[a] = b;
    }
    for (int i = 1; i <= n; i++) {
        if (fa[i] == i) ans ++;
    }
    cout << ans;
}

F - XOR Matching

#include <bits/stdc++.h>

using namespace std;

signed main () {
    int m, k;
    cin >> m >> k;

    if (k >= (1ll << m))    cout << -1; //凑不成两个
    else if (m == 1) {
        if (k)  cout << -1;
        else    cout << "0 0 1 1";
    }
    else {
        for (int i = 0; i < (1ll << m); i++) {
            if (i != k)     cout << i << ' ';
        }
        cout << k << ' ';
        for (int i = (1ll << m) - 1; i >= 0; i--) {
            if (i != k)     cout << i << ' ';
        }
        cout << k << endl;
    }
}

//小结论: 0到2^m-1的异或和为0 (证: 相邻四个一组)

标签:AtCoder,Beginner,int,namespace,cin,fa,126,ans,include
From: https://www.cnblogs.com/CTing/p/17067593.html

相关文章

  • 20230126 - TurboGears 提示 builtins.NameError Session is not defined
    问题现象:TurboGears 常规操作,运行gearbox服务后报错:builtins.NameError'Session'isnotdefined解决办法:卸载最新版SQLAlchemy1.4,重新安装SQLAlchemy1.3。......
  • AtCoder Beginner Contest 162
    A-Lucky7大水题,模拟即可。#include<iostream>#include<cstdio>usingnamespacestd;intmain(){strings;cin>>s;if(s[0]=='7'||s[1]==......
  • AtCoder Beginner Contest 172
    A-Calc(abc172a)题目大意给定一个\(a\),输出\(a+a^2+a^3\)解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=long......
  • 【最短路】Atcoder Beginner Contest 286----E
    题目:E-Souvenir(atcoder.jp)题解:首先这道题可以很容易看出来是求最短路。最开始自己是用bfs写的,出现了WA,TLE,RE等错误。因为对于每种情况会有Q次询问,如果每次询问都......
  • 【多重背包】Atcoder Beginner Contest 286----D
    题目:D-MoneyinHand(atcoder.jp)分析:经典的多重背包。用dp[i]表示i能否正好凑出。先复习一下多重背包。多重背包就是有N组物品,每组最多有k个,每组可以选多个。分组背......
  • AtCoder Beginner Contest 286 解题报告
    AtCoderBeginnerContest286解题报告\(\text{ByDaiRuiChen007}\)ContestLinkA.RangeSwap直接模拟交换过程即可时间复杂度\(\Theta(n)\)#include<bits/stdc++......
  • AtCoder Beginner Contest 286
    A-RangeSwap(abc286a)题目大意给定长度为\(n\)的数组\(a\)和\(p,q,r,s\),交换\(a[p..q]\)和\(a[r..s]\)并输出交换后的数组\(a\)。解题思路模拟即可。神奇......
  • D - Change Usernames -- ATCODER
    D-ChangeUsernameshttps://atcoder.jp/contests/abc285/tasks/abc285_d 思路DFS深度遍历图。需要注意的是,整个大图中可能有很多小的连通子图,每个子图需要确定起......
  • AtCoder Beginner Contest 043
    A-ChildrenandCandies(ABCEdit)n=int(input())print(n*(n+1)//2)B-UnhappyHacking(ABCEdit)用栈模拟一下?但是栈的遍历比较麻烦这里用vector实现#......
  • AtCoder Beginner Contest 047
    A-FightingoverCandies签到#include<bits/stdc++.h>usingnamespacestd;intread(){...}constintN=1e6+5;intmain(){inta=read(),b=read(......