Educational Codeforces Round 132 (Rated for Div. 2)(A~D)

A. Three Doors

有三扇门,有两扇门后面有其他门的钥匙,自己手中还有一把钥匙,问能否打开所有的门。

思路:自己手里这一把钥匙打开的门里必须有钥匙,这把得到的钥匙的门里必须有钥匙。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int t, x;
int a[5];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> t;
    while(t --) {
        std::cin >> x;
        for(int i = 1; i <= 3; i ++) {
            std::cin >> a[i];
        }
        if(!a[x])
            std::cout << "NO" << '\n';
        else if(!a[a[x]]) 
            std::cout << "NO" << '\n';
        else
            std::cout << "YES" << '\n';
    }
    return 0;
}

B. Also Try Minecraft

有n个连续的位置,给出从一个位置到另一个位置,如果下降会加入一个高度差的伤害值,求每次过程中的伤害值是多少。

思路:前后各进行一遍前缀和,前缀和数组维护该方向上的下降高度和,按照方向输出即可。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int t, n, m;
ll a[N], pre[N], suf[N];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> n >> m;
    for(int i = 1; i <= n; i ++) {
        std::cin >> a[i];
        pre[i] = pre[i - 1] + std::max((ll)0, a[i - 1] - a[i]);
    }
    for(int i = n; i >= 1; i --) {
        suf[i] = suf[i + 1] + std::max((ll)0, a[i + 1] - a[i]);
    }
    while(m --) {
        int s, t;
        std::cin >> s >> t;
        if(t >= s)
            std::cout << pre[t] - pre[s] << '\n';
        else
            std::cout << suf[t] - suf[s] << '\n';
    }
    return 0;
}

C. Recover an RBS

给出一个合法的括号序列,在其中某些位置替换成问号,判断这个替换之后的序列是否有多种方式使其合法。

思路:最容易想到的一种构造方式是从前向后先使用左括号,再添加右括号,这样一定可以得到一个合法的括号序列。对于这样的构造方式,怎样得到第二种合法序列呢,考虑交换最后一个被换成左括号的位置和第一个被换成右括号的位置。因为显然,从头开始,左括号的数量是一直不少于右括号的数量的,所以左括号越靠前越容易合法,右括号越靠后越容易合法,所以我们要交换的是最容易产生不合法的情况。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int t;
std::string s;

bool check(std::string s) {
    std::stack<char> st;
    for(int i = 0; i < s.length(); i ++) {
        if(s[i] == '(') st.push('(');
        else {
            if(!st.empty() && st.top() == '(') st.pop();
            else return false;
        }
    }
    if(!st.empty()) return false;
    return true;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> t;
    while(t --) {
        std::cin >> s;
        int len = s.length();
        int cntl = 0, cntr = 0;
        for(int i = 0; i < len; i ++) {
            if(s[i] == '(') cntl ++;
            else if(s[i] == ')') cntr ++;
        }
        cntl = len / 2 - cntl;
        cntr = len / 2 - cntr;
        if(!cntl || !cntr) {
            std::cout << "YES" << '\n';
            continue;
        }
        int posl, posr;
        for(int i = 0; i < len; i ++) {
            if(cntl && s[i] == '?') {
                if(cntl == 1)
                    posl = i;
                s[i] = '(', cntl --;
            }
        }
        bool flag = false;
        for(int i = 0; i < len; i ++) {
            if(cntr && s[i] == '?') {
                if(!flag)
                    posr = i, flag = true;
                s[i] = ')', cntr --;
            }
        }
        std::swap(s[posl], s[posr]);
        std::cout << (check(s) ? "NO" : "YES") << '\n';
    }
    return 0;
}

D. Rorororobot

有一个机器人,在n*m的方格中行走,第i列的下方a[i]个方格被堵住了,机器人每次输入一个指令会重复k次,撞到堵住的方格或者方格外的位置会爆炸,每次询问给出起始位置和终点位置的坐标以及k的值,判断是否可以通过输入指令使得机器人恰好到达终点。

思路:前提条件是两个坐标的差都是k的倍数。显然使机器人走到最下方可以走的位置是最容易到达的,所以每次尽可能走到最靠下的位置,然后在起点和终点列的区间内判断是否有较长的堵住的方格使得走不过去即可。用线段树维护区间最大值。

AC Code:

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
int n, m, q;
ll a[N];

struct SegmentTree {
    struct node {
        int l, r, max;
    } tr[N << 2];

    void pushup(int u) {
        tr[u].max = std::max(tr[u << 1].max, tr[u << 1 | 1].max);
    }

    void build(int u, int l, int r) {
        tr[u] = {l, r};
        if(l == r) {
            tr[u].max = a[l];
            return;
        }
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        pushup(u);
    }

    int query(int u, int l, int r) {
        if(tr[u].l >= l && tr[u].r <= r) {
            return tr[u].max;
        }
        int mid = tr[u].l + tr[u].r >> 1, v = 0;
        if(l <= mid) v = query(u << 1, l, r);
        if(r > mid) v = std::max(v, query(u << 1 | 1, l, r));
        return v;
    }
} ST;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    std::cin >> n >> m;
    for(int i = 1; i <= m; i ++) {
        std::cin >> a[i];
    }
    ST.build(1, 1, m);
    std::cin >> q;
    while(q --) {
        int x, y, x2, y2, k;
        std::cin >> x >> y >> x2 >> y2 >> k;
        if(abs(x - x2) % k || abs(y - y2) % k) {
            std::cout << "NO" << '\n';
            continue;
        }
        x += (n - x) / k * k;
        int max = ST.query(1, std::min(y, y2), std::max(y, y2));
        std::cout << (max < x ? "YES" : "NO") << '\n';
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值