AtCoder abc 133

文章介绍了两个编程问题:C-RemainderMinimization中的LR求和策略和D-RainFlowsintoDams的求解方法,以及E-VirusTree2中利用乘法原理计算树节点值的问题,涉及图的遍历、递归和模运算等技术。

C - Remainder Minimization 2019
如果LR相差在2019内,那么遍历LR
否则可以视作为1…2019的遍历

D - Rain Flows into Dams
设解为M1,M2...MnM_1,M_2...M_nM1,M2...Mn
∵M1+M2=2A1\because M_1+M_2=2A_1M1+M2=2A1
M2+M3=2A2M_2+M_3=2A_2M2+M3=2A2
.........
左边相加可以得到
M1+...+Mn=A1+...+AnM_1+...+M_n=A_1+...+A_nM1+...+Mn=A1+...+An
M的和即可求出。
剩下由于M长度为奇数,可以凑出M1+M2+...+Mn−1M_1+M_2+...+M_{n-1}M1+M2+...+Mn1
即求得MnM_nMn
接着从后往前计算M的各项。

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @author   : yhdu@tongwoo.cn
# @desc     :
# @file     : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    n = int(fp.readline())
    a = list(map(int, fp.readline().split()))
    sb = sum(a)
    eb = 0
    for i in range(0, n - 1, 2):
        eb += a[i] * 2
    b = [0] * n
    b[-1] = sb - eb
    for i in range(n - 2, -1, -1):
        b[i] = 2 * a[i] - b[i + 1]
    print(*b)


if __name__ == "__main__":
    main()

E - Virus Tree 2
普通的乘法原理,画图想一想
根节点可以乘K
接下来的第一层子节点,第一个可以乘K-1,第二个乘K-2,依次递减
第二层子节点,第一个点可以乘K-2,第二个乘K-3,依次递减
之后的子节点和第二层相同

# -*- coding: utf-8 -*-
# @time     : 2023/6/2 13:30
# @author   : yhdu@tongwoo.cn
# @desc     :
# @file     : atcoder.py
# @software : PyCharm
import bisect
import copy
import sys
from sortedcontainers import SortedList
from collections import defaultdict, Counter, deque
from functools import lru_cache, cmp_to_key
import heapq
import math
sys.setrecursionlimit(100010)


def main():
    items = sys.version.split()
    if items[0] == '3.10.6':
        fp = open("in.txt")
    else:
        fp = sys.stdin
    mod = 10 ** 9 + 7
    n, m = map(int, fp.readline().split())
    g = [[] for _ in range(n + 1)]
    for i in range(n - 1):
        u, v = map(int, fp.readline().split())
        g[u].append(v)
        g[v].append(u)
    ans = 1

    def go(cur, fa):
        nonlocal ans
        if fa == -1:
            t = m - 1
        else:
            t = m - 2
        for child in g[cur]:
            if fa == child:
                continue
            ans = ans * t % mod
            t -= 1
        for child in g[cur]:
            if fa == child:
                continue
            go(child, cur)

    go(1, -1)
    ans = ans * m % mod

    print(ans)


if __name__ == "__main__":
    main()

由于无法直接获取AtCoder ABC比赛的最新题解,可通过AtCoder官方网站(https://atcoder.jp/)查看最新比赛场次及题目,比赛结束后,在社区论坛如AtCoder的Discuss板块、GitHub等地方能找到他人分享的题解。 参考之前不同场次题解示例,例如AtCoder ABC 171 C题,是将一个数N转化为26进制用abc表示,模拟过程需注意细节,其AC代码如下: ```cpp #include<iostream> #include<cstdio> #include <stdio.h> #include<algorithm> #include<cstring> #include<cmath> #include<cstdlib> #include<queue> #include<map> #include<vector> #include <set> #define ll long long using namespace std; char a[5000000]; int main() { ll n; ll c=1; int m=0; cin>>n; while(n>0) { ll k=n%(26); if(k==0) { k=26; a[m++]='z'; } else { a[m++]='a'+k-1; } n=(n-k)/26; } for(int i=m-1;i>=0;i--) { cout<<a[i]; } return 0; } ``` 又如AtCoder ABC183题的AC代码如下: ```cpp #include<iostream> #include<bit/stdc++.h> #include<algorithm> #include<vector> #include<numeric> using namespace std; int main(void) { ios_base::sync_with_stdio(false); int N,W; cin >> N >> W; vector<int64_t> v(300000); for(int i = 0; i < N; i++){ int S, T, P; cin >> S >> T >> P; v[S] += P; v[T] -= P; } partial_sum(v.begin(), v.end(), v.begin()); if(*max_element(v.begin(), v.end()) > W) { cout << "No" << endl; } else { cout << "Yes" << endl; } return 0; } ``` 再如AtCoder ABC 242题解,使用while循环往上跳,每次字符往后移动1或2,开变量记录,时间复杂度为$O(Q\log k)$,代码如下: ```cpp #include <bits/stdc++.h> using namespace std; typedef unsigned long long uLL; typedef long double LD; typedef long long LL; typedef double db; const int N = 100005; int n, Ti; char x[N]; int main() { scanf("%s%d", x + 1, &Ti); n = strlen(x + 1); for (LL a, b, c; Ti--; ) { scanf("%lld%lld", &a, &b); c = 0; while (a && b > 1) { if (!(b & 1)) ++c; ++c; b = b + 1 >> 1, a--; } c += a; printf("%c\n", char((x[b] - 'A' + c) % 3 + 'A')); } } ``` 还有AtCoder ABC237题是签到题,可使用C++的INT_MAX和INT_MIN,若不知这两个常数可自行定义,AC代码如下: ```cpp #include<bits/stdc++.h> using namespace std; typedef long long LL; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); LL n; cin>>n; if (n>=INT_MIN && n<=INT_MAX){ cout<<"Yes\n"; }else{ cout<<"No\n"; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值