Codeforces Round 909 (Div. 3)

在这里插入图片描述

Codeforces Round 909 (Div. 3)

Codeforces Round 909 (Div. 3)

A. Game with Integers

题意:略

思路:只要当前数值不能被3整除,均可以通过+1或者-1一步使其被3整除

AC code:

void solve(){
    cin >> n;
    if(n % 3) cout << "First" << endl;
    else cout << "Second" << endl;
}

B. 250 Thousand Tons of TNT

题意:有n个箱子按照固定顺序排放好,需要将箱子按顺序装入货车,每辆货车均放入k个箱子,一共需要n/k辆车,求其中两辆车载重的最大绝对差值

思路:注意按顺序装入,且每辆车箱数相同,故首先判断这些箱子能均分给几辆车,然后再分别计算装车后的最大差值即可

AC code:

void solve(){
    cin >> n;
    int ans = 0;
    for(int i = 1; i <= n; i ++)
        cin >> a[i];
    for(int i = 2; i <= n / 2; i ++){
        if(n % i == 0){
            int mi = 2e18, ma = 0, cnt = 0;
            for(int k = 1; k <= n; k ++){
                cnt += a[k];
                if(k % i == 0){
                    mi = min(mi, cnt);
                    ma = max(ma, cnt);
                    cnt = 0;
                }
            }
            ans = max(ans, ma - mi);
        }
    }
    sort(a + 1, a + n + 1);
    ans = max(ans, a[n] - a[1]);
    cout << ans << endl;
}

C. Yarik and Array

题意:给出长度为n的数组a,找出和最大的子序列,子序列满足相邻元素奇偶性不同

思路:首先单个元素同样满足条件,优先找出当前序列最大元素;然后从头开始遍历序列,奇偶性不同则进行累加,注意当序列累加和为负时对最大答案无贡献,在连续的奇偶不同序列中,最大子序列和应从正值开始累加。

void solve(){
    cin >> n;
    int ans = -2e18;
    for(int i = 1; i <= n; i ++)
        cin >> a[i], ans = max(ans, a[i]);
    int cnt = a[1];
    for(int i = 2; i <= n; i ++){
        if(cnt < 0){
            cnt = a[i];
            continue;
        }
        if(abs(a[i] % 2) != abs(a[i - 1] % 2)){
            cnt += a[i];
            ans = max(ans, cnt);
        }else{
            ans = max(ans, cnt);
            cnt = a[i];
        }
    }
    cout << ans << endl;
}

D. Yarik and Musical Notes

题意:给出序列a,找出 2 x 2^x 2x ^ 2 y 2^y 2y = 2 y 2^y 2y ^ 2 x 2^x 2x, x和y为序列a中的 a i a_i ai a j a_j aj(i != j), 这种组合有多少种

思路:找规律,要么 a i a_i ai= a j a_j aj,要么分别为1和2,用map记录相同数的数量以及1和2的数量,然后用等差序列的前n项和求解

AC code:

int a[N];
map<int, int> mp;
 
void solve(){
    mp.clear();
    cin >> n;
    int cnt = 0, t = 0;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        if(a[i] != 1 && a[i] != 2) mp[a[i]] ++;
        else t ++;
    }
    cnt += (t - 1) * t / 2;
    for(auto x : mp){
        int y = x.second;
        cnt += (y - 1) * y / 2;
    }
    cout << cnt << endl;
}

E. Queue Sort

题意:给出序列a,提取第一个元素插入末尾,然后不断交换直到该元素严格大于前一个元素,需要多少次操作可以使序列不递减或报告不可能。

思路:找出最小元素,最小元素后序列若不递减无法改变,操作次数为最小元素的下标-1

AC code:

void solve(){
    cin >> n;
    int pos = 0, mi = 1e18;
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        if(a[i] < mi){
            mi = a[i];
            pos = i;
        }
    }
    for(int i = pos; i < n; i ++){
        if(a[i] > a[i + 1]){
            cout << "-1" << endl;
            return;
        }
    }cout << pos - 1 << endl;
}

F. Alex’s whims

题意:首先构建一颗n个顶点(n-1)条边的树, 打印(n-1)条边,然后q次查询,叶子为只有一条边相连的顶点,若存在距离为d的两个叶子则不需要修改,否则通过一次修改使树存在这样两个叶,修改操作为选择一个顶点u,连接u的一个顶点v1,断开v1连接另一个不连接u的顶点v2.

思路:查询时只需要看1和n顶点的距离即可,若不符合条件,将n直接连到符合条件的顶点下即可,记录每次修改后n的位置

AC code:

void solve(){
    cin >> n >> q;
    for(int i = 1; i < n; i ++)
        cout << i << " " << i + 1 << endl;
    int top = n - 1;
    while(q --){
        int x; cin >> x;
        if(x == top){
            cout << -1 << " " << -1 << " " << -1 << endl;
            continue;
        }else{
            cout << n << " " << top << " " << x << endl;
            top = x;
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值