AtCoder Beginner Contest 120 解题报告

为啥最近都没有arc啊...

A - Favorite Sound

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline

namespace io {

#define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n')

#define I_int ll
inline I_int read() {
    I_int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
char F[200];
inline void write(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int

}
using namespace io;

using namespace std;

#define N 100010

int a, b, c;

int main() {
    in(a), in(b), in(c);
    printf("%d\n", min(b / a, c));
}

B - K-th Common Divisor

显然,就是求gcd的第k小的因子。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline

namespace io {

#define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n')

#define I_int ll
inline I_int read() {
    I_int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
char F[200];
inline void write(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int

}
using namespace io;

using namespace std;

#define N 100010

int a, b, k, d[N], tot;

int main() {
    in(a), in(b), in(k);
    int g = __gcd(a, b);
    for(int i = 1; i * i <= g; ++i) {
        if(g % i == 0) {
            d[++tot] = i;
            if(g / i != i) d[++tot] = g / i;
        }
    }
    sort(d+1, d + tot + 1); reverse(d + 1, d + tot + 1);
    printf("%d\n", d[k]);
}

C - Unification

栈的经典题。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline

namespace io {

#define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n')

#define I_int ll
inline I_int read() {
    I_int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
char F[200];
inline void write(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int

}
using namespace io;

using namespace std;

#define N 100010
char s[N];
int n, st[N];

int main() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    int ans = 0, top = 0;
    for(int i = 1; i <= n; ++i) s[i] -= '0';
    for(int i = 1; i <= n; ++i) {
        if(top && s[top] ^ s[i]) {++ans, --top; continue;}
        s[++top] = s[i];
    }
    printf("%d\n", ans * 2);
}

D - Decayed Bridges

正着计数太麻烦了。
考虑最后一条边断掉之后答案肯定是\(n(n-1)/2\)。
用个经典的套路,把删边弄成倒着连边。
那么每次多连一条边,联通的点数就多了\(siz_x*siz_y\)
用\(n(n-1)/2\)减去\(siz_x*siz_y\)即可。
注意longlong。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <deque>
#include <map>
#include <set>

#define ll long long
#define inf 0x3f3f3f3f
#define il inline

namespace io {

#define in(a) a = read()
#define out(a) write(a)
#define outn(a) out(a), putchar('\n')

#define I_int ll
inline I_int read() {
    I_int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
char F[200];
inline void write(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
}
#undef I_int

}
using namespace io;

using namespace std;

#define N 100010

int n, m;
int a[N], b[N], f[N];
ll siz[N];
ll ans = 0, sum = 0, res[N];
int find(int x) {
    if(f[x] == x) return x;
    else return f[x] = find(f[x]);
}
int main() {
    in(n), in(m);
    sum = (ll)(n * (n - 1ll) / 2ll);
    for(int i = 1; i <= m; ++i) in(a[i]), in(b[i]);
    for(int i = 1; i <= n; ++i) f[i] = i, siz[i] = 1ll;
    for(int i = m; i; --i) {
        res[i] = sum - ans;
        int x = find(a[i]), y = find(b[i]);
        if(x != y) {
            ans += (ll)siz[x] * siz[y];
            siz[x] += siz[y];
            f[y] = x;
        }
    }
    for(int i = 1; i <= m; ++i) printf("%lld\n", res[i]);
}

原文地址:https://www.cnblogs.com/henry-1202/p/10467882.html

时间: 2024-11-08 08:32:04

AtCoder Beginner Contest 120 解题报告的相关文章

AtCoder Beginner Contest 117 解题报告

果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include<cstdio> #include<algorithm> #include<cstring> int main(){ double t,x; scanf("%lf%lf",&t,&x); printf("%lf",t/x

Atcoder Beginner Contest 124 解题报告

心态爆炸.本来能全做出来的.但是由于双开了Comet oj一个比赛,写了ABC就去搞那个的B题 还被搞死了. 回来写了一会D就过了.可惜比赛已经结束了.真的是作死. A - Buttons #include <cstdio> using namespace std; int main() { int x, y; scanf("%d%d", &x, &y); int ans = x > y ? x : y; if (x > y) x--; else

AtCoder Beginner Contest 125 解题报告

那天晚上刚好有事就咕了. 最近的那一场E题还不会写.F题全场又只过了三个?留坑吧... A - Biscuit Generator #include <cstdio> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (

AtCoder Beginner Contest 129 解题报告

传送门 写了四个题就跑去打球了.第五题应该能肝出来的. A - Airplane #include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' &

AtCoder Beginner Contest 132 解题报告

前四题都好水.后面两道题好难. C Divide the Problems #include <cstdio> #include <algorithm> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (c

【AtCoder】AtCoder Grand Contest 040 解题报告

点此进入比赛 \(A\):><(点此看题面) 大致题意: 给你一个长度为\(n-1\).由\(<\)和\(>\)组成的的字符串,第\(i\)位的字符表示第\(i\)个数和第\(i+1\)个数的大小关系,求这个由非负整数组成的数组中元素和的最小值. 送分题都想了几分钟才做出来,真是退役预警...... 显然,对于所有小于两旁的数,我们给它赋值为\(0\),然后再从它们向两边扩展即可. #include<bits/stdc++.h> #define Tp template&

AtCoder Beginner Contest 103 D(贪心)

AtCoder Beginner Contest 103 D 题目大意:n个点,除第n个点外第i与第i+1个点有一条边,给定m个a[i],b[i],求最少去掉几条边能使所有a[i],b[i]不相连. 按右端点从小到大排序,如果当前选的去掉的边在区间内,那么符合条件,否则ans++,并贪心地把去掉的边指向右端点,因为前面的区间都满足条件了,所以要去掉的边要尽量向右移使其满足更多的区间. 1 #include <iostream> 2 #include <cstdio> 3 #incl

AtCoder Beginner Contest 136

AtCoder Beginner Contest 136 Contest Duration : 2019-08-04(Sun) 20:00 ~ 2019-08-04(Sun) 21:40 Website: AtCoder BC-136 后面几题都挺考思考角度D. C - Build Stairs 题目描述: 有n座山从左到右排列,给定每一座山的高度\(Hi\),现在你可以对每座山进行如下操作至多一次:将这座山的高度降低1. 问是否有可能通过对一些山进行如上操作,使得最后从左至右,山的高度呈不下降

AtCoder Beginner Contest 154 题解

人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We have A balls with the string S written on each of them and B balls with the string T written on each of them. From these balls, Takahashi chooses one