当前位置:   article > 正文

Codeforces Round 911 (Div. 2)_codeforces round 911 div2

codeforces round 911 div2

在这里插入图片描述

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/55182
推荐阅读
相关标签
  

闽ICP备14008679号