2024/3/21

本文解析了四道Codeforces竞赛题目,涉及数组操作优化(如修改元素使数组满足特定条件)、中位数最大化问题、数组平等性调整和数字组合优化,展示了如何通过模拟、贪心策略和代码实现来解决这些问题。
摘要由CSDN通过智能技术生成
A. Unit Array

原题链接:Problem - 1834A - Codeforces

题目大意:

给你一个数组只有0,1组成,要求你每步操作可以随机修改一个元素,问你最少修改多少次能够满足如上两种条件。

题目做法:

小小的模拟,不满足就换-1补1呗,不能再刷水题啦。。。

AC代码:

import math


def solve():
    n = int(input())
    np1, nn1 = 0, 0
    ar = list(map(int, input().split()))
    for i in range(0, n):
        if ar[i] != -1:
            np1 += 1
        else:
            nn1 += 1
    if np1 >= math.ceil(n / 2):
        if nn1 % 2 != 0:
            print(1)
        else:
            print(0)
    else:
        if (n - math.ceil(n / 2)) % 2 == 0:
            print(math.ceil(n / 2) - np1)
        else:
            print(math.ceil(n / 2) - np1 + 1)


cases = int(input())
while cases > 0:
    solve()
    cases -= 1
B. Sum of Medians

原题链接:Problem - 1440B - Codeforces

题目大意:

他先规定了一个数组的median元素,是排好序之后的在n/2向上取整的位置的数的值。然后会给你n*k个元素,让你分配到k个数组里要求这个k个数组的median之和最大。

题目做法:

贪心地取就好,入手点是最大怎么样取都的往往不能取到,所以你不把较大的先取了,往后放则又取不到了,我感觉我写得很抽象,但是应该是对的。

AC代码:

def solve():
    n, k = map(int, input().split())
    ar = list(map(int, input().split()))
    res, ps = 0, len(ar) - 1
    for i in range(0, k):
        ps -= int(n / 2)
        res += ar[int(ps)]
        ps -= 1
    print(res)


cases = int(input())
while cases > 0:
    solve()
    cases -= 1
C. Unequal Array

原题链接:Problem - 1672C - Codeforces

题目大意:

他先定义了一个Equality的指标,代表数组中a[i]==a[i+1]的数量,他让你通过任选a[i]和a[i+1]设置成x的操作使得数组的Equality<=1问你最少要进行多少次这样的操作。

题目做法:

首先你得明白,哪怕最优地进行一次操作可以减少一对equality但是也会增加一对。所以如果数组本身不满足,就要从不满足的头到不满足的尾巴这一段都得进行这样的操作,汇总到中间才能使得Equality变成1。

AC代码:

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define int long long
using namespace std;
void solve(){
    int n;
    vector<int> ps;
    cin >> n;
    int ar[n];
    for(int i = 0; i < n; i++){
        cin >> ar[i];
    }
    for(int i = 0; i < n; i++){
        if(i != n - 1){
            if(ar[i] == ar[i + 1]){
                ps.pb(i);
            }
        }
    }
    //cout << ps.size() << '\n';
    if(ps.size() <= 1){
        cout << "0" << '\n';
    }
    else{
        if((ps[ps.size() - 1] - (ps[0] + 1)) + 1 == 1){
            cout << "1" << '\n';
        }
        else{
            cout << (ps[ps.size() - 1] - (ps[0] + 1)) + 1 - 1 << '\n';
        }
    }
}
signed main(){
    fast int n;
    cin >> n;
    while(n--) solve();
}
D. Epic Transformation

原题链接:Problem - 1506D - Codeforces

题目大意:

经典问题:找俩值不同的元素可以消,问给你一个数组,最终能消得最短是多短。

题目做法:

最多次出现的元素如果小于n / 2则肯定能消到1或者0,如果大于,则把其他所有去消这个最多次出现的都消不完,剩下的为 maxn - (n - maxn)。如果你要说为什么,大于的情况还是很好理解的吧,小于的情况就是先把其他的元素消成最接近最多次出现的元素个数的数量,然后就是相消。。。我也不知道我解释得明不明白,小于的情况你自己列几种情况应该方便理解些,这是个比较经典的问题,我经常遇到,实在不行把结论记住,这个真的蛮经常出现的。。

感觉我还是在刷水题,不过这居然是个Div3的D。

AC代码:

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define int long long
using namespace std;
void solve(){
    int n;
    cin >> n;
    map<int, int> mp;
    int maxn = -1, t;
    for(int i = 0; i < n; i++){
        cin >> t;
        mp[t]++;
        if(mp[t] >= maxn){
            maxn = mp[t];
        }
    }
    if(maxn <= n/2){
        cout << n % 2 << '\n';
    }
    else{
        cout << maxn - (n - maxn) << '\n';
    }
}
signed main(){
    fast int n;
    cin >> n;
    while(n--) solve();
}
D. Decrease the Sum of Digits

原题链接:Problem - 1409D - Codeforces

题目大意:

要求比n大1或者等的数,各位和小于等于s,要求离n最近。

题目做法:

因为是最近,所以从尾巴开始变最优,只能往大变,所以只能寄希望于往前加1,接下来就是细节处理,没什么太大难度,比较好想到。

AC代码:

#include<bits/stdc++.h>
#define pb(element) push_back(element)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
using namespace std;
const int mode=1e9+7;
int dif(string str)
{
    int need=0;
    for(auto its:str) need+=its-'0';
    return need;
}
int tonum(string n)
{
    int res=0,pp=1;
    for(int i=n.length()-1;i>=0;i--,pp*=10) res+=pp*(n[i]-'0'); 
    return res;
}
void solve()
{
    string n;
    int res=0,i,num,bf;
    string fres;
    cin>>n>>num;
    n="0"+n;
    bf=tonum(n);
    for(i=0;i<n.length();i++)
    {
        res+=n[i]-'0';
    }
    for(i=n.length()-1;i>=0;i--)
    {
        if(dif(n)>num)
        {
            int ps=i-1;
            n[ps]=n[ps]+1;
            n[i]='0';
            while(n[ps]==('9'+1))
            {
                n[ps]='0';
                ps--;
                n[ps]+=1;
            }
        }
    }
    // cout<<n<<'\n';
    cout<<tonum(n)-bf<<'\n';
}
signed main()
{
    fast int casen=1;
    cin>>casen;
    while(casen--) solve();
}

2024/3/25 0:16

Yesterday, my parents departed from Beijing without any new occurrences. Perplexingly, all the significance I possess seems to be attached to those I hold DEAR WITH. Thus, when Xian left my life, I felt utterly lost and uncertain of what else I can do except for working hard and striving to become better in order to increase my chances of reconnecting with Xian. However, deep down in my heart, I am fully aware that Xian will not return. The last glimpse, One last conscientious glimpse has long vanished into thin air. Throughout my short life thus far, there are three person whom I profoundly care about. However, two of them are growing older by the day while the other one has disappeared from my life despite our close proximity. Yet as mentioned earlier - devoid of any new occurrences - such boring, dull and endless life that I have hahahah! Even though I have just entered my twenties, it feels as if my life has dumped me into a stagnant water. Since November 1st, 2023 nothing special or exciting has transpired in my world. Nevertheless, I shall continue seeking for something more because what else can I possibly do?
Xian PLZPLZPLZ wait for me.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值