首页 > 其他分享 >AtCoder Beginner Contest 340 题解

AtCoder Beginner Contest 340 题解

时间:2024-02-14 23:26:52浏览次数:19  
标签:AtCoder cout int 题解 ll cin Update 340 tie

AtCoder Beginner Contest 340 题解

去我的洛谷博客里看这篇文章!

A - Arithmetic Progression

模拟即可。

// 2024/2/11 PikachuQAQ

#include <iostream>

using namespace std;

int a, b, d;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> a >> b >> d;
    for (int i = a; i <= b; i += d) {
        cout << i << ' ';
    }

    return 0;
}

B - Append

vector 模拟即可。

// 2024/2/11 PikachuQAQ

#include <iostream>
#include <vector>

using namespace std;

int q, t, x;
vector<int> v;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    for (cin >> q; q--; ) {
        cin >> t >> x;
        if (t == 1) {
            v.push_back(x);
        } else {
            cout << v[v.size() - x] << '\n';
        }
    }

    return 0;
}

C - Divide and Divide

丢到 OEIS 里得到规律即可。

// 2024/2/11 PikachuQAQ

#include <iostream>

using namespace std;

typedef long long ll;

ll n, s, i, z;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    s = n - 1, i = n - 1, z = 1;
    while (i >= 0) {
        s += i, i -= z, z += z;
    }
    cout << s;

    return 0;
}

D - Super Takahashi Bros.

我们可以把这个问题转化为最短路问题。

对于每个点 \(i\) 建两条到 \(i + 1\) 和到 \(x_i\) 的边,边权分别为 \(a_i\) 和 \(b_i\),求点 \(1\) 到点 \(n\) 的最短路即可。

// 2024/2/11 PikachuQAQ

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

typedef long long ll;

const int kMaxN = 2e5 + 7;
const ll INF = 1e18;

struct E {
    ll v, w;
    friend bool operator > (const E& a, const E& b) {
        return a.w > b.w;
    }
};

int n, m;
ll dis[kMaxN];
bool vis[kMaxN];
vector<E> g[kMaxN];
priority_queue<E, vector<E>, greater<E> > q;

void Dijkstra(int s) {
    fill(dis + 1, dis + n + 1, INF);
    dis[s] = 0;
    q.push({s, 0});
    while (q.size()) {
        int u = q.top().v; q.pop();
        if (vis[u] == 0) {
            vis[u] = 1;
            for (E e : g[u]) {
                ll v = e.v, w = e.w;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    q.push({v, dis[v]});
                }
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1, a, b, c; i < n; i++) {
        cin >> a >> b >> c;
        g[i].push_back({i + 1, a});
        g[i].push_back({c, b});
    }
    Dijkstra(1);
    cout << dis[n];

    return 0;
}

E - Mancala 2

可以把操作转为给每个 \(i\) 加上 \(\lfloor\dfrac{n}{b_i}\rfloor\),将剩下的 \(n\) 取出来给相应区间加 \(1\)。当要加 \(1\) 的 \(i\) 超过 \(n\),那么将多出来的区间从 \(1\) 开始补到后面即可。

// 2024/2/11 PikachuQAQ

#include <iostream>

using namespace std;

typedef long long ll;

const int kMaxN = 2e5 + 7;

ll n, m, c[kMaxN];

int lowbit(int x) {
    return x & -x;
}

void Update(ll x, ll y) {
    for (int i = x; i <= n; i += lowbit(i)) {
        c[i] += y;
    }
}

ll Query(int x) {
    ll res = 0;
    for (int i = x; i; i -= lowbit(i)) {
        res += c[i];
    }
    return res;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n >> m;
    for (int i = 1, a, x = 0; i <= n; i++) {
        cin >> a;
        Update(i, a - x), x = a;
    }
    for (ll i = 1, b, x; i <= m; i++) {
        cin >> b, b++, x = Query(b);
        Update(b, -x), Update(b + 1, x);
        if (x > n - b) {
            x -= n - b;
            Update(b + 1, 1), Update(1, x / n + 1);
            Update(x % n + 1, -1);
        } else {
            Update(b + 1, 1), Update(b + x + 1, -1);
        }
    }
    for (int i = 1; i <= n; i++) {
        cout << Query(i) << ' ';
    }

    return 0;
}

F - S = 1

注意到已知其中两个点坐标分别为 \((a,b)\) 和 \((0,0)\),面积为 \(S\),则有 \(\dfrac{|ay-bx|}{2}=1\),据题得 \(ay-bx=±2\)。这种形式的方程,可以用 exgcd 解决。当 \(a\) 和 \(b\) 的最大公因数大于 \(2\),那么无解。

我们通过 exgcd 得到了一对解,为 \(ay_0-bx_0=±\gcd(a,b)\),将 \((x_0,y_0)\) 分别除以 \(-\dfrac{g}{2}\) 和 \(\dfrac{g}{2}\) 即可得到正确解 \((x,y)\)。

// 2024/2/11 PikachuQAQ

#include <iostream>

using namespace std;

typedef long long ll;

ll a, b, x, y, g;

ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    } else {
        ll d = exgcd(b, a % b, y, x);
        y -= a / b * x;
        return d;
    }
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> a >> b, g = exgcd(a, b, y, x);
    if (abs(g) > 2) {
        cout << "-1";
        return 0;
    }
    x *= -2 / g, y *= 2 / g;
    cout << x << ' ' << y;

    return 0;
}

G - Leaf Color

不会虚树。

标签:AtCoder,cout,int,题解,ll,cin,Update,340,tie
From: https://www.cnblogs.com/PikachuQAQ/p/18015824/abc340-solution

相关文章

  • AtCoder Beginner Contest 340 题解
    AtCoderBeginnerContest340题解去我的洛谷博客里看这篇文章!A-ArithmeticProgression模拟即可。//2024/2/11PikachuQAQ#include<iostream>usingnamespacestd;inta,b,d;intmain(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>......
  • 「JOI Open 2019」三级跳 题解
    https://loj.ac/p/3153Part1暴力暴力思路:每次询问的时候,枚举\(a\)和\(b\)在哪里,然后就确定了\(c\)的范围\([2\timesb-a]\),找这个范围内的最大的A[c]即可。Part2优化舍解考虑哪一些\([a,b]\)是明显不优的。如果存在\(i\),满足\(a<i<b\)且\(A[i]<\min(A[a],A[b......
  • P4113 [HEOI2012] 采花 题解
    题目链接:采花这题数据加强到卡了\(2e6\)的可持久化线段树在线做法,先给只tle了最后一个点的代码:卡常参照代码#include<bits/stdc++.h>//#pragmaGCCoptimize(2)//#pragmaGCCoptimize("Ofast,no-stack-protector,unroll-loops,fast-math")//#pragmaGCCtarget("......
  • KAJIMA CORPORATION CONTEST 2024(AtCoder Beginner Contest 340)
    KAJIMACORPORATIONCONTEST2024(AtCoderBeginnerContest340)A-ArithmeticProgression代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__int128......
  • LOJ #2876. 「JOISC 2014 Day2」水壶 题解
    DescriptionJOI君所居住的IOI市以一年四季都十分炎热著称。IOI市被分成\(H\)行,每行包含\(W\)块区域。每个区域都是建筑物、原野、墙壁之一。IOI市有\(P\)个区域是建筑物,坐标分别为\((A_1,B_1),\)\((A_2,B_2),\)\(\ldots,\)\((A_P,B_P)\)。JOI君只能进入建......
  • CF1931F Chat Screenshots 另一种题解
    题目链接:CF或者洛谷本题拓扑排序不再赘述,来说说字符串哈希怎么做这题。本篇以另一种角度剖析题目背景,并不追求最优,例如有些地方其实可以暴力判断,主要以构造的角度阐述,顺便感谢灵茶山的朋友的讨论。结论三个串及其以上必定能构造出最初的那个串。下面进行证明:首先一个串,显......
  • P8683题解
    首先,看题目描述,给定N个加号,与M个减号,要你求后缀表达式最大值,实际上就是求这些符号和数字排列起来的最大值。这题很容易让人想到贪心。但是呢,又怎么个贪心法呢?很容易看出来,我们可以先用sort进行这么一个排序,之后,我们对于前N大的数加起来,对于剩下的数就减去,于是代码大体就......
  • 麦肯锡问题解决流程-为希望提升水平的产品经理量身定制
    您是否想知道世界上最成功的产品经理如何始终如一地提供不仅满足而且超出预期的解决方案?秘密可能就在于世界上最负盛名的咨询公司之一麦肯锡公司所磨练的方法论。本文深入探讨了麦肯锡的问题解决流程,该流程专为希望提升水平的产品经理量身定制。01.麦肯锡方法:产品管理风......
  • ABC 340
    忘记打了,VP了一把,前五题都是板子。F题意:坐标系上给定一个整点\((x,y)\),求另一个整点\((a,b)\),满足\((0,0),(x,y),(a,b)\)组成的三角形面积为\(1\)(或说明无解)。题解:由这三个点组成的三角形面积为\(\dfrac{|ay-bx|}{2}\),所以\(|ay-bx|=2\)。令\(g=gcd(x,y)\),若\(g>3\)......
  • AT_abc340_f [ABC340F] S = 1
    首先我们知道:顶点为\((0,0),(x,y),(a,b)\)的三角形的面积为\(\dfrac{|ay-bx|}{2}\)。因此,问题转化为:给定整数\(x,y\),求一个整数对\((a,b)\)使得\(|ay-bx|=2\)。令\(d=\gcd(x,y)\):如果\(d\ge3\),则答案不存在,因为\(|ay-bx|\)始终是\(d\)的倍数。如果\(d=1,2\),则可......