Educational Codeforces 79

本文介绍了三道算法竞赛题目,涉及排序、字符串匹配和堆栈操作。对于A题,通过排序和交错组合判断是否能形成无相同颜色的串;B题使用前缀和与最大值来确定跳跃方案;C题模拟按顺序发放礼物的过程,计算所需花费。这些题目考察了排序、字符串处理和数据结构的应用。

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

Educational Codeforces Round 79 (Rated for Div. 2)

A. New Year Garland

题目链接

思路:

首先排序,假设排序后, a ≤ b ≤ c a\le b\le c abc,那么我们将a与b交错的叠一起,那么得到一个 a + b a+b a+b的串,之后判断与 c c c串能否叠一起而不会有相同颜色。具体判断看代码。

代码实现:

#include <iostream>
#include <algorithm>
using namespace std;
int t;
int a[10];
int main(){
    cin>>t;
    while(t--){
        cin>>a[1]>>a[2]>>a[3];
        sort(a+1,a+1+3);
        int cnt1=a[1]+a[2];
        int cnt2=a[3];
        if(cnt2-cnt1<=1) cout<<"Yes\n";
        else cout<<"No"<<endl;
    }
}

B. Verse For Santa

题目链接

思路:

从前往后枚举,维护个前缀和 s u m sum sum m a x max max。枚举到i时,如果 s u m > s sum>s sum>s则需要看跳过 m a x max max能否满足 1 − i 1-i 1i,若不满足,则 b r e a k break break,否则记录 i d id id

代码实现:

#include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+7;
int t;
int n,m,a[N],b[N],ma[N];
int main(){
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++) ma[i]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            ma[a[i]]=i;
        }
        for(int i=1;i<=m;i++) cin>>b[i];
        int l=0,cnt=0;
        ll ans=0;
        for(int i=1;i<=m;i++){
            if(l+1<=ma[b[i]]){
                while(l+1<=ma[b[i]]){
                    cnt++;
                    l++;
                }
                ans+=2ll*(cnt-i)+1ll;
            }else{
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

C - Stack of Presents

题目链接

思路:

按序列 b i b_i bi的顺序发放礼物,如果在 a i a_i ai中发放在 b i b_i bi之前的礼物,之后都只需花费 1 1 1取出,其余的只需记录前面出栈的数量和所在的位置则可算出花费。

代码实现:

#include <iostream>
using namespace std;
typedef long long ll;
const int N=1e5+7;
int t;
int n,m,a[N],b[N],ma[N];
int main(){
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(int i=1;i<=n;i++) ma[i]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            ma[a[i]]=i;
        }
        for(int i=1;i<=m;i++) cin>>b[i];
        int l=0,cnt=0;
        ll ans=0;
        for(int i=1;i<=m;i++){
            if(l+1<=ma[b[i]]){
                while(l+1<=ma[b[i]]){
                    cnt++;
                    l++;
                }
                ans+=2ll*(cnt-i)+1ll;
            }else{
                ans++;
            }
        }
        cout<<ans<<endl;
    }
}

D. Santa’s Bot

题目链接

思路:

概率公式为: ∑ c n t [ x ] n ∗ n ∗ k x \sum\frac{cnt[x]}{n*n*k_x} nnkxcnt[x] c n t [ x ] cnt[x] cnt[x]为x出现的次数。

#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int N=1e6+7;
ll n;
ll a[N],sz[N],ma[N];
vector <ll> ho[N];
ll ksm(ll x,ll p){
    ll res=1;
    while(p){
        if(p%2) res=res*x%mod;
        p/=2;
        x=x*x%mod;
    }
    return res;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        ll x;
        cin>>sz[i];
        for(int j=1;j<=sz[i];j++) cin>>x,ho[i].push_back(x),ma[x]++;
    }
    
    ll ans=0;
    for(int i=1;i<=n;i++){
        ll fz=0,fm=(ll)sz[i]*n*n%mod;
        for(int j=0;j<ho[i].size();j++){
            fz=(fz+ma[ho[i][j]])%mod;
        }
        ans=(ans+fz*ksm(fm,mod-2)%mod)%mod;
    }
    cout<<ans<<"\n";
}
### 关于Codeforces Educational Round 173 的题目与解答 #### A. 多测试用例转换 当决定在Codeforces上举办竞赛并准备了一系列材料之后,可能会遇到协调员要求更改最简单问题中的所有测试案例为多测试用例的情况[^1]。对于具体的Educational Round 173而言,这类修改通常涉及调整输入输出格式以适应多个独立的查询或数据集。 ```python def convert_to_multiple_test_cases(tests): """ 将单个测试用例转化为多个测试用例的形式。 参数: tests (list): 原始单一测试用例列表 返回: str: 转化后的多测试用例字符串表示形式 """ result = [] num_tests = len(tests) result.append(str(num_tests)) for test in tests: result.append("\n".join(map(str, test))) return "\n\n".join(result) tests_example = [ ["2", "1 2"], ["3", "1 2 3"] ] print(convert_to_multiple_test_cases(tests_example)) ``` 此代码片段展示了如何将给定的一系列单独测试用例组合成一个多测试用例结构,这通常是根据平台特定的要求来完成的。 #### 输入处理示例 针对具体比赛中的某些问题,比如涉及到用户数量和朋友组数目的情况,可以参考如下输入处理方式: ```cpp #include <iostream> using namespace std; int main() { int n, m; cin >> n >> m; // 获取用户数目n以及好友分组m cout << "Number of users: " << n << endl; cout << "Number of friend groups: " << m << endl; } ``` 这段C++程序读取两个整数值作为输入参数,并打印出来自标准输入流的信息。这种类型的输入解析适用于描述中提到的场景,在其中指定了`n`代表用户的数量而`m`则对应着朋友群体的数量[^2]。 然而,关于Codeforces Educational Round 173的具体题目及其解决方案并未直接提供在此处所列的参考资料之中。为了获取该轮次的确切题目详情及官方解法建议访问Codeforces官方网站或者查阅对应的公告页面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值