Codeforce-1080B & 1077B & 1077A & 1075A

本文解析了Codeforce平台上的四道算法题目,包括Margariteandthebestpresent、DisturbedPeople、FrogJumping和TheKing’sRace。涵盖数组求和、贪心算法、数列计算及距离比较等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Codeforce-1080B & 1077B & 1077A & 1075A

  • Codeforce-1080B-Margarite and the best present
  • Codeforce-1077B-Disturbed People
  • Codeforce-1077A-Frog Jumping
  • Codeforce-1075A-The King’s Race

Codeforce-1080B-Margarite and the best present

题目链接
题目大意

给你一个数组,数组元素取值为ai = i * (-1)i,给你两个数lr,要你求出arr[l ~ r]的和。
在这里插入图片描述

解析
  • 方法一: 可以使用等差数列求和公式,先全部看做正数,求出所有看做正数的和,然后减去 2 * 奇数的和,但是要分四种情况看lr的奇、偶情况;
  • 方法二: 有一个规律,就是前后两个数相加或者是1,或者是-1,然后分情况讨论从奇数开始还是偶数开始即可。
#include <bits/stdc++.h>

typedef long long ll;

// my solution
#if 0
int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);    
    ll T, l, r;
    for(std::cin >> T; T--; ){ 
        std::cin >> l >> r;
        // regard as 2 arithmetic progression, regard as all to positive number
        ll sumAll = (l+r)*(r-l+1)/2;
        ll sumNegative;
        if( (l&1) && (r&1) )
            sumNegative = (l+r)*( (r-l)/2 + 1)/2;
        else if( (l&1)==0 && (r&1) )
            sumNegative = (l+1+r)*( (r-(l+1))/2 + 1)/2;
        else if( (l&1) &&  (r&1)==0 ) 
            sumNegative = (l+r-1)*( (r-1-l)/2 + 1)/2;
        else 
            sumNegative = (l+1+r-1)*( (r-1-(l+1))/2 + 1)/2;

        std::cout << sumAll - 2*sumNegative << std::endl;
    }
    return 0;
}
#endif

// best way
int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);    
    ll T, l, r;
    for(std::cin >> T; T--; ){ 
        std::cin >> l >> r;
        ll sum, num = r - l + 1; // numbers 
        if(num & 1)  // odd
            sum = num == 1 ? ( l&1 ? -l : l) : ( l&1 ? -l-num/2 : l+num/2); 
        else  // even
            sum = l&1 ? num/2 : -num/2; 
        std::cout << sum << std::endl;
    }
    return 0;
}

Codeforce-1077B-Disturbed People

题目链接
题目大意

给你一排公寓,0代表关灯(要睡觉了),1代表开着灯,如果某个公寓关灯而且他两边的公寓都开着灯,这个公寓就会被吵到(disturbed),要你手动关掉最少的k个开着灯的公寓,使得所有人都不会被吵到,求最小的k
在这里插入图片描述

解析

贪心,这题居然一次就过了,我也没想到真的就是这样"贪"。。就是从左到右,只要有被吵到的,就关掉它右边的公寓的。大概的原因就是前面的选择了最优解,后面肯定可以更加的"省"

#include <bits/stdc++.h>
const int MAX = 101;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, arr[MAX];
    std::cin >> n;
    for(int i = 0; i < n; i++)
        std::cin >> arr[i];

    int res = 0;
    for(int i = 1; i < n-1; i++){ 
        if(arr[i] == 0 && arr[i-1] == 1 && arr[i+1] == 1){ 
            arr[i+1] = 0;
            res += 1;
        }
    }
    std::cout << res << std::endl;
    
    return 0;
}

Codeforce-1077A-Frog Jumping

题目链接
题目大意

一个青蛙,给你第三个数a、b、ka代表往正方向跳,b代表往负方向跳,k代表跳的步数,要你求最后的位置(数)。
在这里插入图片描述

解析

水题。。

#include <bits/stdc++.h>

typedef long long ll;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ll T, a, b, k;
    for(std::cin >> T; T--; ){ 
        std::cin >> a >> b >> k;
        ll temp = (k/2)*(a-b);
        std::cout << ( k%2==1 ? temp+a : temp ) << std::endl; 
    }
    return 0;
}

Codeforce-1075A-The King’s Race

题目链接
题目大意

两个king,分别在(1,1)(n,n),目标beautiful coin(r,c),两个国王都想得到,问谁快。
在这里插入图片描述
在这里插入图片描述

解析

水题。直接计算距离即可,注意数据大。

#include <bits/stdc++.h>

typedef unsigned long long ull;

int main(int argc, char const **argv)
{ 
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    ull n, r, c;
    std::cin >> n >> r >> c;

    ull distW = (r-1) + (c-1);
    ull distB = (n-r) + (n-c);

    std::cout << (distW <= distB ? "White" : "Black") << std::endl;
    return 0;
}

### Codeforces Problem 797B Explanation The problem titled &quot;Restoring the Permutation&quot; requires reconstructing a permutation from its prefix sums modulo \( m \). Given an array of integers representing these prefix sums, one must determine whether it is possible to restore such a permutation. In this context, a **permutation** refers to an ordered set containing each integer exactly once within a specified range. The task involves checking if there exists any valid sequence that matches the provided conditions when performing operations as described in the problem statement[^1]. To solve this issue effectively: - Iterate through all elements while maintaining two variables: `current_sum` which tracks cumulative sum during iteration; and `min_value`, used later for adjustments. ```cpp int n, m; cin &gt;&gt; n &gt;&gt; m; vector&lt;int&gt; s(n); for (auto&amp; x : s) cin &gt;&gt; x; ``` Calculate differences between consecutive terms after adjusting initial values appropriately by subtracting minimum value found so far at every step. This adjustment ensures non-negativity throughout calculations without altering relative order among elements. Check feasibility based on properties derived from constraints given in the question text. Specifically, ensure no duplicate residues appear under modulus operation since they would violate uniqueness required for permutations. Finally, construct answer using adjusted difference list obtained previously along with necessary checks ensuring correctness according to rules outlined above.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值