首页 > 其他分享 >AtCoder Beginner Contest 313 A-E Code

AtCoder Beginner Contest 313 A-E Code

时间:2023-08-06 11:57:02浏览次数:40  
标签:AtCoder Code false 313 cin int vector mp

比赛链接:AtCoder Beginner Contest 313 - AtCoder

 

A:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    int p1;
    cin >> p1;
    
    int maxp = 0;
    for (int i = 2; i <= n; ++ i) {
        int p;
        cin >> p;
        maxp = max(maxp, p);
    }
    
    cout << max(0, maxp + 1 - p1) << endl;
    
    return 0;
}
View Code

 

B:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, m;
    cin >> n >> m;
    vector<int> din(n+1);
    
    while (m--) {
        int a, b;
        cin >> a >> b;
        ++ din[b];
    }
    
    int id, num = 0;
    for (int i = 1; i <= n; ++ i) {
        if (!din[i]) {
            id = i, ++ num;
        }
    }
    
    cout << (num == 1 ? id : -1) << endl;
    
    return 0;
}
View Code

 

C:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    vector<int> a(n);
    long long sum = 0;
    for (int i = 0; i < n; ++ i) {
        cin >> a[i];
        sum += a[i];
    }
    
    vector<int> b(n);
    for (int i = 0; i < n; ++ i) {
        if (i < n - sum % n) b[i] = sum / n;
        else b[i] = sum / n + 1;
    }
    
    long long ans = 0;
    sort(a.begin(), a.end());
    for (int i = 0; i < n; ++ i) {
        if (a[i] > b[i]) break;
        ans += b[i] - a[i];
    }
    cout << ans << endl;
    
    return 0;
}
View Code

 

D:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    // cin.tie(0);
    // ios::sync_with_stdio(false);
    
    // [1,k+1] 缺1,2,3,···,k  -->  1与2,3,···,k是相同还是相异  -->  1,2,3,···,k
    // [1,k], [2,k+1], ···, [n-k+1,n]  -->  1与k+1, 2与k+2, ···, n-k与n
    
    int n, k;
    cin >> n >> k;
    
    // 返回包含[l,r]区间但除去x的数的vector
    auto q = [](int l, int r, int x) -> vector<int> {
        vector<int> v;
        for (int i = l; i <= r; ++ i) {
            if (i == x) continue;
            v.push_back(i);
        }
        return v;
    };
    
    // 输出查询
    auto query = [](vector<int> &v) -> void {
        cout << "? ";
        for (int x : v) {
            cout << x << ' ';
        }    
        cout << endl;
    };
    
    map<vector<int>, int> mp;
    for (int i = 1; i <= k; ++ i) {
        vector<int> v = q(1, k+1, i);
        query(v);
        int T;
        cin >> T;
        mp[v] = T;
    }
    
    for (int i = 1; i <= n - k + 1; ++ i) {
        vector<int> v = q(i, i+k-1, 0);
        if (mp.count(v) > 0) continue;
        query(v);
        int T;
        cin >> T;
        mp[v] = T;
    }
    
    int T1 = mp[q(1, k+1, 1)];
    int cnt1 = 1, cnt2 = 0;
    for (int i = 2; i <= k; ++ i) {
        if (T1 == mp[q(1, k+1, i)]) ++ cnt1;
        else ++ cnt2;
    }
    
    vector<int> A(n+1);
    int A1k = mp[q(1, k, 0)];
    A[1] = A1k == (cnt1 & 1);
    
    for (int i = 2; i <= k; ++ i) {
        if (T1 == mp[q(1, k+1, i)]) A[i] = A[1];
        else A[i] = !A[1];
    }
    
    for (int i = 1; i <= n - k; ++ i) {
        if (mp[q(i, i+k-1, 0)] == mp[q(i+1, i+k, 0)]) {
            A[i+k] = A[i];
        } else {
            A[i+k] = !A[i];
        }
    }
    
    cout << "! ";
    for (int i = 1; i <= n; ++ i) {
        cout << A[i] << ' ';
    }
    cout << endl;
    
    return 0;
}
View Code

 

E:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n;
    cin >> n;
    
    string s;
    cin >> s;
    s = ' ' + s;
    
    bool flag = true;
    for (int i = 1; i < n; ++ i) {
        if (s[i] > '1' && s[i+1] > '1') {
            flag = false;
            break;
        }
    }
    
    if (!flag) {
        cout << -1 << endl;
    } else {
        const int p = 998244353;
        long long ans = 1;
        for (int i = n - 1; i >= 1; -- i) {
            ans = (ans + 1 + (s[i+1] - '1') * ans) % p;
        }
        cout << ans - 1 << endl;
    }
    
    return 0;
}
View Code

 

标签:AtCoder,Code,false,313,cin,int,vector,mp
From: https://www.cnblogs.com/jakon/p/17609243.html

相关文章

  • Codeforces Round 890 (Div. 2) supported by Constructor Institute 题解
    A.TalesofaSort关键就是找逆序对记一组逆序对下标为\(l,r\),则求出最大的\(a_l\)即可B.GoodArrays记要构造的GoodArray为\(b\)前置:\(\forall1\lei\len,b_i=1\)然后\(O(n)\)扫一遍看一下有没有重复,有重复就\(b_i\leftarrowb_i+1\)扫完之后,记\(sum=\sum_......
  • 一支笔,一双手,一道力扣(Leetcode)做一宿
    (文章目录)一、分享自己相关的经历我是一名计算机专业的学生,之前在学习算法和数据结构时,对于简单题目还算能够顺利地刷过去。但是当我开始尝试刷一些medium难度的题目时,就感觉自己卡在原地了。明明看过题解,知道解题思路,但真正动手做题时,就觉得无从下手,甚至一道题目做了好几天都......
  • Codeforces Round 882 (Div. 2) 题解
    A.TheManwhobecameaGod求出相邻两个元素的差值,去掉前\(m\)个大的差值以后的差值和即为答案B.HamonOdyssey由按位与的性质可以知道,前缀与和的值只会越来越小,只要和为\(0\)的时候我们就清空按位与前缀和,增加一下次数,如果最终次数不为\(0\),特判一下,次数加一即可C.......
  • 【LeetCode剑指offer#06】实现pow函数、计算x的平方根
    实现pow函数实现pow(x,n),即计算x的整数n次幂函数(即,xn)。示例1:输入:x=2.00000,n=10输出:1024.00000示例2:输入:x=2.10000,n=3输出:9.26100示例3:输入:x=2.00000,n=-2输出:0.25000解释:2-2=1/22=1/4=0.25代码classSolution{public:do......
  • CodeForces 1856E1 PermuTree (easy version)
    洛谷传送门CF传送门考虑局部贪心,假设我们现在在\(u\),我们希望\(u\)不同子树中的\((v,w),a_v<a_u<a_w\)的对数尽量多。我们实际上只关心子树内\(a_u\)的相对大小关系,不关心它们具体是什么。如果\(u\)只有两个儿子\(v,w\),我们可以让\(v\)子树内的\(a\)全部......
  • CodeForces 1856E2 PermuTree (hard version)
    洛谷传送门CF传送门考虑局部贪心,假设我们现在在\(u\),我们希望\(u\)不同子树中的\((v,w),a_v<a_u<a_w\)的对数尽量多。我们实际上只关心子树内\(a_u\)的相对大小关系,不关心它们具体是什么。如果\(u\)只有两个儿子\(v,w\),我们可以让\(v\)子树内的\(a\)全部......
  • CodeForces 1856D More Wrong
    洛谷传送门CF传送门直接求最大值不好求。我们可以采用一个交互常见的套路,维护可能的答案集合\(S\),每次剔除掉一些。考虑初始把\(a_{i-1}<a_i>a_{i+1}\)的\(i\)加入\(S\),每次取出\(S\)中差最小的一对元素\(i,j\),那么若\(a_i>a_j\),\(a_i\)就大于\([i+1......
  • ABC313
    T1:ToBeSaikyo\(x\geqslant\max({p_i}),i>1\)代码实现n=int(input())a=list(map(int,input().split()))ifn==1:exit(print(0))mx=max(a[1:])ans=max(0,mx+1-a[0])print(ans)T2:WhoisSaikyo?对\((A_i,B_i)\)连一条有向边,使得\(A_i......
  • ABC313C 解题报告
    赛前看到这场C的分值直接飙上\(400\)就知道不是个善茬。这道题给了个启发,算是积累个trick吧。题目传送门简要题意:给定长为\(n\)的序列,进行若干次以下操作:每次选定两个整数\(i\)和\(j\),使得\(a_{i}\leftarrowa_{i}+1\)并使得\(a_{j}\leftarrowa_{j}-1\),要求最......
  • abc313D 题解
    [abc313DOddorEven]。好有趣捏。我们考虑\(N=K+1\)。设\(s_i\)为\(\displaystyle\sum_{j\neqi}a_j\bmod2\)。因为\(K\)为奇数,我们可以得到\(\displaystyle\sum_{i=1}^{K+1}s_i\equiv\sum_{i=1}^{K+1}a_i\pmod2\)。所以\(a_i=\displaystyle\sum_{i=1}^{K+1}a_i\b......