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

AtCoder Beginner Contest 131

时间:2023-01-04 16:36:04浏览次数:60  
标签:AtCoder Beginner fa int cin ++ 131 include find

AtCoder Beginner Contest 131

https://atcoder.jp/contests/abc131
4/6:ABCD

A - Security

水题

#include <bits/stdc++.h>

using namespace std;

signed main () {
    string s;
    cin >> s;
    for (int i = 1; i < 4; i++) {
        if (s[i] == s[i-1]) {
            cout << "Bad";
            return 0;
        }
    }
    cout << "Good";
}

B - Bite Eating

找出绝对值最小的 \(L+i-1\)

#include <bits/stdc++.h>

using namespace std;

signed main () {
    int n, L;
    cin >> n >> L;
    int dx = -1;
    if (L >= 0)     dx = L;
    else {
        if (n - 1 >= -L)    dx = 0;
        else    dx = L + n - 1;
    }
    cout << n * L - n + n * (n + 1) / 2 - dx;
}

C - Anti-Division

简单数论(?)。
查询[a,b]里面有多少c,d的倍数,直接除, \(O(1)\) 查询,记得容斥掉二者共同的倍数。

#include <bits/stdc++.h>
#define int long long

using namespace std;
int a, b, c, d;

int count (int x) {
    return b / x - (a + x - 1) / x + 1;
}

signed main () {
    cin >> a >> b >> c >> d;
    int lcm = (c * d) / __gcd (c, d);
    //cout << count (c) << ' ' << count (d) << ' ' << count (lcm) << endl;
    cout << b - a + 1 - (count (c) + count (d) - count (lcm)) << endl;
}

//查询[a,b]里面有多少c,d的倍数

D - Megalomania

模拟题。先排序后直接模拟

#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
pii p[N];
int n;

signed main () {
    cin >> n;
    for (int i = 1; i <= n; i++)    cin >> p[i].second >> p[i].first;
    sort (p + 1, p + n + 1);
    int dx = 0;
    for (int i = 1; i <= n; i++) {
        dx += p[i].second;
        //cout << dx << ' ';
        if (dx > p[i].first) {
            puts ("No");
            return 0;
        }
    }
    puts ("Yes");
}

//查询[a,b]里面有多少c,d的倍数

E - Friendships

构造题。
完全图:\(0\) 个;
最多的情况:以一个点为中心的菊花图,共 $\frac{(n-1)\times(n-2)}{2} $ 对点。
如何构造在此范围内的边:画几个图不难发现,完全图中每去掉一条边就会多一对符合条件的点。故边数为 \(\frac{n\times (n-1)}{2}-k\)

#include <bits/stdc++.h>

using namespace std;

int main () {
    int n, m;
    cin >> n >> m;
    if (m > (n - 2) * (n - 1) / 2)      cout << -1;
    else {
        int cnt = n * (n - 1) / 2 - m;
        cout << cnt << endl;
        for (int i = 1; i < n; i++) {
            for (int j = i + 1; j <= n; j++) {
                cout << i << ' ' << j << endl;
                cnt --;
                if (cnt == 0)       return 0;
            }
        }
    }
}

//最多的时候是以一个点为中转,则有 (n-1)*(n-2)/2个  

F - Must Be Rectangular!

数据结构。
很难想到是一道冰茶几。
怎么判断某点是哪一个矩形?利用并查集的归属关系。

#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N = 1e5 + 5, M = N * 2;
int fa[M], hang[M], lie[M], n;
ll ans;

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

int main () {
    for (int i = 1; i < M; i++)    fa[i] = i;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        x =  find (x), y = find (y + N);
        if (x != y)     fa[x] = y;
    }
    for (int i = 1; i < N; i++)     hang[find(i)] ++, lie[find (i + N)] ++;
    for (int i = 1; i < M; i++)     ans += 1ll * hang[i] * lie[i];
    cout << ans - n << endl;
}

//dsu

标签:AtCoder,Beginner,fa,int,cin,++,131,include,find
From: https://www.cnblogs.com/CTing/p/17025172.html

相关文章

  • F - Permutation Distance -- ATCODER
    F-PermutationDistancehttps://atcoder.jp/contests/abc283/tasks/abc283_f 思路最小生成树法:https://zhuanlan.zhihu.com/p/595421879 动态缩减查找距离法朴......
  • AtCoder Beginner Contest 129
    AtCoderBeginnerContest129https://atcoder.jp/contests/abc1294/6:ABCDA-Airplane水题:#include<bits/stdc++.h>usingnamespacestd;intmain(){i......
  • D - Scope --- ATCODER
    D-Scopehttps://atcoder.jp/contests/abc283/tasks/abc283_d 思路使用stack做字符串的内容分析,除了)所有的字符依次入栈,遇到(字符,则从栈顶开始依次出栈,直到第一个(也......
  • AtCoder Beginner Contest 283 - a new beginning
    许久没有写过博客了,让2023成为一个新的起点,重新开始写博客。尽量写的高质量一点,从E开始。E-Don'tIsolateElements考虑dp,考虑如何设计状态。不难看出,每一行变两......
  • AtCoder Beginner Contest 200 vp记
    又来vp了一场比赛\(\dots\dots\)AtCoderBeginnerContest200vp记ProblemA这道题不会有人要解析吧Code#include<bits/stdc++.h>#defineintlonglong#define......
  • UNIQUE VISION Programming Contest 2022 Winter(AtCoder Beginner Contest 283)
    A-PowerGivenintegersAandB,printthevalueA^B.基础不解答B-FirstQueryProblem基础不解答C-CashRegisterTakahashiisacashier.Thereis......
  • [AtCoder Regular Contest 077] F: SS (arc077F)
    原题链接​​​https://arc077.contest.atcoder.jp/tasks/arc077_d​​Description定义偶串为这个字符串前一半和后一半相同(abadabad)定义函数f(S)表示在S这个字符串后......
  • [AtCoder Grand Contest 018] D: Tree and Hamilton Path (agc018D)
    原题链接​​​https://agc018.contest.atcoder.jp/tasks/agc018_d​​Description给出一棵N个点带边权的树现在有一个N个点的完全图,一条边x,y的长度是这两点的在树上最短......
  • [AtCoder Grand Contest 071] E: Jigsaw (agc071E)
    原题链接​​​https://agc017.contest.atcoder.jp/tasks/agc017_e​​Description给出N块拼图每块拼图宽度为3,高度为相同的H拼图由3个宽度为1的部分拼接而成,第一部分......
  • AtCoder-abc230_g GCD Permutation 容斥
    J-GCDPermutation传送门:J-GCDPermutation知识点:素数筛、容斥定理、gcd题意:长度为n的一个排列a中,求满足\(gcd(i,j)!=1且gcd(a_i,a_j)!=1\)的i,j对数。i,j可以......