Pinely Round 4 (Div. 1 + Div. 2)解题报告(C, D)

Pinely Round 4 (Div. 1 + Div. 2)解题报告(C, D)

链接:Pinely Round 4 (Div. 1 + Div. 2)

C. Absolute Zero

题目:
在这里插入图片描述
输入数据:
在这里插入图片描述

解题思路:
一眼丁真,取一半,只要每次取数组中的最大值的一半进行操作, 然后进行40次操作,看看是否达成条件即可

官方的更为巧妙解法:
在这里插入图片描述
复盘:
自己打的时候看出来是一半了,但是是数组值之和sum的一半,然后自己构造的几个还都能过了,结果就wa了,最后就过俩题还是没上1000,菜,以后自己还是得多构造几个啊

代码:

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

void solve() {
    int n;cin >> n;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    vector<ll> ans;
    int cnt;
    for (cnt = 1; cnt <= 40; cnt++) {
        int max = 0;
        for (int i = 0; i < n; i++) {
            if (a[i] > a[max]) max = i;
        }
        
        ans.push_back((a[max]+1)/2);
        
        bool ok = 1;

        for (int i = 0; i < n; i++) {
            a[i] = abs(a[i] - ans.back());
            if (a[i] != 0) ok = 0;
        }
        if (ok) break;
    }

    if (cnt > 40) {
        cout << -1 << '\n';
        return;
    }

    cout << cnt << '\n';
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << ' ';
    }
    cout << '\n';
}

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

官方代码:

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
int n, a[MAX_N];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];

    bool has_odd = false, has_even = false;

    for (int i = 1; i <= n; i++)
        if (a[i] % 2 == 1)
            has_odd = true;
        else
            has_even = true;
    if (has_even && has_odd)
        cout << -1 << '\n';
    else {
        vector<int> operations;
        for (int i = 29; i >= 0; i--) 
            operations.push_back(1 << i);
        if (has_even) 
            operations.push_back(1);
        cout << operations.size() << '\n';
        for (int x : operations)
            cout << x << ' ';
        cout << '\n';
    }
}
int main() {
    int t;
    cin >> t;
    while (t--)
        solve();
}

Prime XOR Coloring

题目:
在这里插入图片描述
样例
在这里插入图片描述

解题思路:
先了解一个概念,四色定理(来自百度百科):
来自百度
那么我们就可以得知就算所有数字都联通,我们依旧只需要用4种颜色即可,然后样例又将出现4种颜色的最低条件为6种给出来了,那么我们在大于6的情况下只需要循环输出1,2,3,4即可
复盘:
学计算机还得把数学方面的知识给补习一下才行啊,不然根本想不到啊,这自己打表打半天没找到规律,结果人家一个四色定理就过了
代码:

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

void solve() {
    int n;cin >> n;
    if (n <= 6) {
        if (n == 1) 
            cout << "1\n1\n";
        else if (n == 2) 
            cout << "2\n1 2\n";
        else if (n == 3)
            cout << "2\n1 2 2\n";
        else if (n == 4)
            cout << "3\n1 2 2 3\n";
        else if (n == 5) 
            cout << "3\n1 2 2 3 3\n";
        else if (n == 6)
            cout << "4\n1 2 2 3 3 4\n"; 
    } else {
        cout << "4\n";
        for (int i = 0; i < n; i++) {
            cout << i%4+1 << ' ';
        }
        cout << '\n';
    }
}

int main () {
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值