2019-2020 Saint-Petersburg Open High School Programming Contest 8/11

本文主要解析2019-2020年圣彼得堡开放高中编程竞赛中A到K题目的解题思路,涉及题目包括方程求解、贪心策略、分块bitset暴力方法等。
摘要由CSDN通过智能技术生成

A

解个方程,就 a = b + c a = b + c a=b+c s − a = s − b = s − c s - a = s - b = s - c sa=sb=sc
s s s是最后做完操作的每个数, a a a是最小的那个数要补多少,然后接这个方程就行。

#include <iostream>
#include <algorithm>
 
int main () {
   
	int a[3];
	std::cin >> a[0] >> a[1] >> a[2];
	std::sort(a, a + 3);
 
	int s = a[1] + a[2] - a[0];
	int ans = 3 * s - a[0] - a[1] - a[2];
	std::cout << ans / 2 << '\n';
 
	return 0;
}

B

贪心

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
 
using namespace std;
 
int const N = 1005;
 
vector<int> g[N], f[N]; ///f add, g minus
int n, m, s;
 
int main() {
   
	scanf("%d%d%d", &n, &m, &s);
	for (int i = 1; i <= n; ++i) {
   
		int c, x, y;
		scanf("%d%d%d", &c, &x, &y);
		if (c > 0)
			f[y].push_back(c);
		if (c < 0)
			g[x].push_back(c);
	}
	bool ans = 0;
	for (int i = 1; i <= m; ++i) {
   
		for (auto p : g[i])
			s += p;
		if (s < 0)
			ans = 1;
		for (auto p : f[i])
			s += p;
	}
	if (ans)
		puts("YES");
	else
		puts("NO");
}

C

分块bitset暴力

#include <iostream>
#include <cassert>
#include <algorithm>
#include <bitset>
#include <vector>
#include <cstdint>
#include <memory>
 
const int N = 100000;
const int SMALL = 100;
 
class Set {
   
    std::unique_ptr<std::vector<int>> v;
    std::unique_ptr<std::bitset<N>> bs;
 
    Set (std::unique_ptr<std::vector<int>> v_, std::unique_ptr<std::bitset<N>> bs_)
        : v(std::move(v_)), bs(std::move(bs_)) {
   }
 
    void to_bs () {
   
        bs = std::make_unique<std::bitset<N>>(as_bs());
        if (v) v = nullptr;
    }
 
public:
    Set (): Set(nullptr, nullptr) {
   }
    Set (std::vector<int> v_): Set(std::make_unique<std::vector<int>>(v_), nullptr) {
   }
    Set (Set const &rhs): Set() {
   
        if (rhs.v)
            v = std::make_unique<std::vector<int>>(*rhs.v);
        if (rhs.bs)
            bs = std::make_unique<std::bitset<N>>(*rhs.bs);
    }
 
    Set &operator = (Set &&rhs) {
   
        v = std::move(rhs.v);
        bs = std::move(rhs.bs);
        return *this;
    }
 
    int count () const {
   
        if (v) return v->size();
        if (bs) return bs->count();
        return 0;
    }
 
    void clear() {
   
        v = nullptr;
        bs = nullptr;
    }
 
    std::bitset<N> as_bs () const {
   
        if (bs)
            return *bs;
        auto ret = std::bitset<N>();
        if (v)
            for (int x: *v)
                ret.set(x);
        return ret;
    }
 
    void set (int x) {
   
        if (v) {
   
            if (std::find(v->begin(), v->end(), x) == v->end()) {
   
                v->push_back(x);
                if (v->size() >= SMALL)
                    to_bs();
            }
        } else if (bs)
            bs->set(x);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值