codeforces 899 div2 (a,b,c)

A

#include <iostream>
#include <vector>

using namespace std;

int a[110];

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 3);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int ans = 0;
    int i = 1;
    int j = 1;
    while (ans < n) {
        while (i == a[j]) {
            i++;
        }
        i++;
        j++;
        ans++;
    }
    cout << i - 1 << '\n';
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

B

这个题确实感觉不好想,大体思路是用二维数组f[i][j]表示第i个集合中是否有j元素,数据量在50以内,小,所以我们可以遍历1到50,每到一个元素a,找到所有不含这个元素的集合,求出这些集合中的元素个数,表示删除元素a,每层1到50的遍历最后一步更新最大元素数量
不好想的一道题,值得多思考,可以带入样例手玩一下

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

bool f[55][55];
bool b[55];
bool f1[55];
ll siz[55]; // 每个集合的元素数量

void solve()
{
    int n;cin >> n;
    for (int i = 0; i < 55; i++)
        for (int j = 0; j < 55; j++)
            f[i][j] = 0;
    for (int i = 0; i < 55; i++)
        b[i] = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> siz[i]; // 第i个集合有多少个元素
        for (int j = 1; j <= siz[i]; j++)
        {
            int k;
            cin >> k;
            b[k] = true;
            f[i][k] = true;
        }
    }
    ll ans = 0;
    for (int i = 1;i <= 50;i++) {//筛选每一个数
        if (!b[i])continue;
        //如果有这个数
        ll cnt = 0;
        for (int j = 0;j <= 50;j++)f1[j] = 0;
        for (int op = 1;op <= n;op++) {//遍历每个集合
            if (!f[op][i]) {
                for (int w = 1;w <= 50;w++) {
                    if (!f1[w] && f[op][w]) {
                        f1[w] = true;
                        cnt++;
                    }
                }
            }
        }
        ans = max(ans, cnt);
    }
    cout << ans << '\n';
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

C

这个题有可以取巧的地方,手推一下可以发现,第三个往后的正数都能被加上
推理如下:当某个正数在奇数位则直接加上,当在偶数位则删去前面一个负数再加上
所以剩下的只是讨论a1和a2
当a1,a2都是正数的时候就都加上
当只有a1是正数的时候就只加上a1
当a1和a2异号,且a1+a2大于0的时候都加上
其余情况都不加

#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

void solve() {
    int n;
    cin >> n;
    vector<ll> a(n + 3);
    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        if (i >= 3 && a[i] > 0)
            ans += a[i];
    }
    if (a[1] > 0 && a[2] > 0)
        ans += a[1] + a[2];
    else if (a[1] > 0)
        ans += a[1];
    else if (a[1] + a[2] > 0)
        ans += a[1] + a[2];
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值