Educational Codeforces 79

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";
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值