Codeforces Round 911 (Div. 2)

在这里插入图片描述

Codeforces Round 911 (Div. 2)

Codeforces Round 911 (Div. 2)

A. Cover in Water

题意:给出一个字符串,#代表堵塞的部分,'.'代表待填充水的部分,两边有水的点可以自动被填充。现在有两个操作,第一个操作是在一个可填充部分填充水,第二个操作是将已有水的部分取出填到无水的部分。

最小化第一个操作的操作次数。

思路:对于一个大于3的连续的待填充的部分,只需要填充两端即可,只要有一个这样的部分,其余部分都可以通过移动该部分除了两边的水进行填充操作。否则只能挨个填充。

AC code:

void solve(){
    cin >> n;
    string s; cin >> s;
    int cnt = 0, ans = 0;
    for(int i = 0; i < n; i ++){
        if(s[i] == '.') cnt ++;
        else{
            if(cnt >= 3){
                cout << 2 << endl;
                return;
            }
            ans += cnt;
            cnt = 0;
        }
    }
    if(cnt >= 3){
        cout << 2 << endl;
        return;
    }
    ans += cnt;
    cout << ans << endl;
}

B. Laura and Operations

题意:给出只有1,2,3的序列,通过删除不同的两个数然后添加第三个数,是否有可能通过此操作令整个序列的数为1,2,3。

思路:看两数的差是否是偶数即可,因为减少的数会添加到另一个数上,最后两数的数量相等即可全变成第三个数。

AC code:

void solve(){
    int a, b, c; cin >> a >> b >> c;
    int x[4];
    x[1] = 0, x[2] = 0, x[3] = 0;
    if(a == b) x[3] = 1;
    if(a == c) x[2] = 1;
    if(c == b) x[1] = 1;
    
    if(a == b && c == b) {
        cout << "1 1 1" << endl;
        return;
    }
    int cnt = abs(a - b);
    if(cnt % 2 == 0) x[3] = 1;
    cnt = abs(a - c);
    if(cnt % 2 == 0 ) x[2] = 1;
    cnt = abs(c - b);
    if(cnt % 2 == 0) x[1] = 1;
 
    for(int i = 1; i <= 3; i ++)
        cout << x[i] << " ";
    cout << endl;
}

C. Anji’s Binary Tree

题意:小凯从一颗二叉树的顶点1开始,树的每个节点都有一个字符代表可以向左子节点/右子节点/父节点移动,现在小凯要移动到任一一个没有子节点的叶子节点,他可以改变一个节点的字符来到达叶子节点,他操作的最小次数是多少。

思路:二叉树中不会出现重边和自环的现象,所以我们可以用bfs从顶点开始层层向下拓展;每次存入队列时判断当前节点与父节点的关系,若可以直接到达当前节点则无需操作,否则到达当前点的操作次数+1;当我们找到一个无子节点的叶子节点时,记录到达该点的答案,并取最小。

AC code:

#include<bits/stdc++.h>
#define endl '\n'
#define int long long
#define fast() ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
 
typedef long long LL;
typedef pair<char, int> PCI;
typedef pair<int, int> PII;
const int N = 2e6+10, M = 2001;
const int INF = 0x3f3f3f3f3f, mod = 998244353;
int T, n, m;
map<int, PII> g;//存取每个节点的两个子节点
char pos[N];//每个节点的操作
 
int bfs(){
    int ans = 1e18;
    queue<PII> q;
    q.push({1, 0});
 
    while(!q.empty()){
        auto t = q.front();
        int x = t.first, y = t.second;
        q.pop();
        if(!g[x].first && !g[x].second){
            ans = min(ans, y);
            continue;
        }
        if(g[x].first){
            int cnt = (pos[x] != 'L');
            q.push({g[x].first, y + cnt});
        }
        if(g[x].second){
            int cnt = (pos[x] != 'R');
            q.push({g[x].second, y + cnt});
        }
    }return ans;
}
 
void solve() {
    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> pos[i];
    for (int i = 1; i <= n; i ++) {
        int l, r; cin >> l >> r;
        g[i] = {l, r};
    }
    cout << bfs() << endl;
}
 
signed main(){
    fast();
    
    T = 1;
    cin >> T;
    while(T --){
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值