首页 > 其他分享 >AtCoder Beginner Contest 306 题解 A - E

AtCoder Beginner Contest 306 题解 A - E

时间:2023-06-18 18:48:46浏览次数:38  
标签:AtCoder const int 题解 long st 306 include define

A - Echo

题目大意

给定一个字符串,需要把它每个字符重复输出。

解题思路

可以读完整个字符串,也可以按照字符读一个输出两个。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;

const int N = 1010;
const int MOD = 998244353;

void solve() {
    int n;
    string s;
    cin >> n >> s;
    for (int i = 0; i < n; ++i) {
        cout << s[i] << s[i];
    }
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

B - Base 2

题目大意

给定倒叙二进制串,需要输出它的十进制表示。

解题思路

最大取到 \(2^{63}\) 需要开unsigned long long 防止溢出。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

const int N = 1010;
const int MOD = 998244353;

void solve() {
    ULL sum = 0;
    for (int i = 0; i <= 63; ++i) {
        ULL x;
        cin >> x;
        sum += (x << i);
    }
    
    cout << sum;
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

C - Centers

题目大意

给定一个由 \(1\sim n\) 中的数每个重复三遍组成的数列,我们现在需要对 \(1\sim n\) 按照每个数第二次出现的顺序排列。

解题思路

我们可以使用两个布尔数组分别存第一次和第二次出现与否,第二次出现时将这个数压入答案数组即可。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

const int N = 3e5 + 10;
const int MOD = 998244353;

bool op[N], oo[N];
int all[N];

void solve() {
    int n;
    cin >> n;

    LL cnt = 0;
    for (int i = 1; i <= 3 * n; ++i) {
        int x;
        cin >> x;
        if (!op[x]) {
            op[x] = true;
        } else if (!oo[x]){
            all[++cnt] = x;
            oo[x] = true;
        }
    }
    for (int i = 1; i <= cnt; ++i) {
        cout << all[i] << ' ';
    }
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

D - Poisonous Full-Course

题目大意

餐馆立有两类食物,有毒的和解毒的,吃到有毒的会不舒服,不舒服状态再吃有毒的就会死掉。无论什么状吃到解毒的就会恢复健康。每种菜肴有一个美味值,每次可以选择吃不吃这道菜,一旦不吃就无法再次吃它。问最后能获得的最多的美味值是多少。

解题思路

题目实际上已经把转移的过程都告诉我们了,我们可以使用dp[i][j] 表示决定完前 \(i\) 道菜,身体状况为 \(j\) 时能获得的最大美味值。发生健康转换的时候我们一定吃了食物,这时候的dp值一定会加上我们当前食物的美味值。同时不能忘了判断不吃的情况,因为数据范围可以让美味值出现负数,那么不吃可能是更好的。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define int LL
const int N = 3e5 + 10;
const int MOD = 998244353;

pair<bool, int> p[N];
LL dp[N][2];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int x, y;
        cin >> x >> y;
        p[i] = {x, y};
    }
    if (p[1].first) {
        dp[1][1] = p[1].second;
        dp[1][0] = 0;
    } else {
        dp[1][0] = max(p[1].second, 0ll);
        dp[1][1] = 0;
    }
    for (int i = 2; i <= n; ++i) {
        if (p[i].first) {
            dp[i][0] = dp[i - 1][0];
            dp[i][1] = max(dp[i - 1][0] + p[i].second, dp[i - 1][1]);
        } else {
            dp[i][0] = max(max(dp[i - 1][0], dp[i - 1][1]) + p[i].second, dp[i - 1][0]);
            dp[i][1] = dp[i - 1][1];
        }
    }
    cout << max(dp[n][1], dp[n][0]) << endl;
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

E - Best Performances

题目大意

我们有一个长度为 \(n\) 的序列 \(a =(a_1,a_2,…,a_n)\),初始时所有的元素都是\(0\)。

给定一个整数 \(k\),我们定义一个函数 \(f(a)\) 如下:

  • 令 \(b\) 是通过将 \(a\) 按降序排序得到的序列(使其成为单调非递增的)。 然后,令 \(f(a)=b_1+b_2+⋯+b_k\)。

我们考虑对该序列应用 \(q\) 次更新。

按照以下操作,对序列 \(a\) 进行操作 \(i=1,2,…,q\),并在每次更新后打印 \(f(a)\) 的值。

  • 将 \(a_{x_i}\)更改为\(y_i\)。

解题思路

我们使用两个muitiset来进行模拟,一个处理前 \(k\) 大的元素,一个处理剩下的所有元素,再维护一个元素的位置处理即可。

AC Code

#include <iostream>
#include <algorithm>
#include <cstring>
#include <numeric>
#include <set>
#define endl '\n'
#define ios ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;

const int N = 5e5 + 10;
const int MOD = 998244353;

multiset<int> st, st1;
int a[N];

void solve() {
    int n, k, q;
    LL res = 0;
    cin >> n >> k >> q;
    for (int i = 0; i < n; ++i) {
        i < k ? st.insert(0) : st1.insert(0);
    }
    while (q--) {
        int x, y;
        cin >> x >> y;
        x--;
        if (st.contains(a[x])) {
            st.extract(a[x]);
            res -= a[x];
        } else {
            st1.extract(a[x]);
        }
        a[x] = y;
        st.insert(a[x]);
        res += a[x];
        while (st.size() > k) {
            int xs = *st.begin();
            st1.insert(st.extract(xs));
            res -= xs;
        }
        while (!st.empty() && !st1.empty() && *st.begin() < *st1.rbegin()) {
            int xx = *st.begin();
            int yy = *st1.rbegin();
            res += yy - xx;
            st1.insert(st.extract(xx));
            st.insert(st1.extract(yy));
        }
        cout << res << endl;
    }
}

signed main() {
    ios;
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
}

标签:AtCoder,const,int,题解,long,st,306,include,define
From: https://www.cnblogs.com/SanCai-Newbie/p/17489487.html

相关文章

  • 小程序授权常见问题解析及解决方案
    引言小程序作为一种轻便、易用的应用形式,正在成为许多企业和个人开发者的首选。在用户使用小程序时,需要授权相应的权限才能获取更好的使用体验。但是,有时会遇到授权失败或出现安全隐患等问题。本文将从常见问题的角度出发,为大家解析小程序授权常见问题及其解决方案。下午一直犯困吟......
  • 题解:【ABC168F】 . (Single Dot)
    题目链接挺套路的题。如果值域和线段数量同阶,可以预处理不能越过的线段,使用状压四个方向记录每个节点能不能往这个方向走,然后直接爆搜就好了,标记上能走到哪些地方。但这个值域一看就是没有救的,于是就要拿出来离散化了。把线段的横纵坐标都拎出来离散化,依然是同样的预处理,然后从离......
  • 2022 RoboCom 世界机器人开发者大赛-本科组(国赛)个人题解
    RC-u4变牛的最快方法思路最短编辑距离+记录路径板子题,不懂最短编辑距离的可以看看网上的博客。不懂为什么官方题解用的bfs写法,然后网上所有的题解就是bfs了。我这里就是双重for循环实现,参考下写法即可。代码点击查看代码#include<bits/stdc++.h>#definexfirst#definey......
  • AtCoder ABC306 DEF
    D-PoisonousFull-Course(DP)题意现在有\(N\)道菜,高桥需要依次享用。第\(i\)道菜有两个属性\((X_i,Y_i)\),其意义是:若\(X_i=0\),则第\(i\)道菜是解毒的,其美味度为\(Y_i\);若\(X_i=1\),则第\(i\)道菜是有毒的,其美味度为\(Y_i\)。当高桥享用一道菜,他的状态变化如下:......
  • CF1840C题解
    题目描述题目传送门\(T\)组数据,每组数据给定\(n\),\(k\),\(q\)和一个长度为\(n\)的数组\(a\),求\(a\)中长度大于等于\(k\)且最大值小于等于\(q\)的序列个数。\(\sum{n}\le2e5\)。题目解析解法一:数据结构解法显然可以利用数据结构维护。考虑ST表预处理出区间最大......
  • 铁矿石 20230618
    先说结论下周阻力832.5-840。周初还是强势第5浪还没走完。   ......
  • 纯碱波浪 20230618
    日线ABC的调整可能结束了。反弹看1780-1810一线。  下周初方案一: 方案二: ......
  • SummerResearch_Log_20230617
    WorkingContent:1.今天还是读代码,对于代码有以下问题:(1)FCNet最后的输出层只有1个神经元,这如何做分类?——解决了,应该是因为它每个子任务都是训练两类,所以只需要一个神经元确定是哪个类别。(2)CIFAR数据集的分任务是什么情况?既使用了CIFAR10也使用了CIFAR100,并且分类的情况也有点......
  • AtCoder Beginner Contest 306 G Return to 1
    洛谷传送门AtCoder传送门考虑若干个能被\(1\)到达且能到达\(1\)的环,设它们的环长分别为\(a_1,a_2,...,a_k\)。那么我们现在要每个环走若干遍,使得步数不含除\(2\)或\(5\)以外的质因子。设第\(i\)个环走\(x_i\)遍,那么其实就是要求\(\sum\limits_{i=1}^ka_i......
  • JOI Final 2020 题解
    JOI2020JustLongNeckties首先一定是贪心将两个从小到大排。然后考虑维护\(a_i-b_i\)的前缀max与\(a_{i+1}-b_i\)的后缀max即可。https://qoj.ac/submission/113106JOI2020JJOOII2考虑维护出每个点往前跳\(k\)个J/O/I跳到哪里。于是枚举右端点,然后往前跳找......