Educational Codeforces Round 79 (Rated for Div. 2)

A. New Year Garland (CF 1279 A)

题目大意

给定红绿蓝三种颜色灯的数量,问能否摆成一排,使得相邻颜色不相同。

解题思路

植树问题。考虑数量最多为(n)的颜色的灯俩俩不相邻,那么其他颜色的灯的数量和要大于(n-1)即可,大过(n-1)的灯直接插到里面就好了。

神奇的代码
#include <bits/stdc++.h>
#define MIN(a,b) ((((a)<(b)?(a):(b))))
#define MAX(a,b) ((((a)>(b)?(a):(b))))
#define ABS(a) ((((a)>0?(a):-(a))))
#define MP(a,b) make_pair((a),(b))
#define PB push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<LL> VL;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;

template <typename T>
void read(T &x) {
    int s = 0, c = getchar();
    x = 0;
    while (isspace(c)) c = getchar();
    if (c == 45) s = 1, c = getchar();
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
    int b[40], l = 0;
    if (x < 0) putchar(45), x = -x;
    while (x > 0) b[l++] = x % 10, x /= 10;
    if (!l) putchar(48);
    while (l) putchar(b[--l] | 48);
    putchar(c);
}

int main(void) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int a,b,c;
    int kase; read(kase);
    for (int i = 1; i <= kase; i++) {
    read(a);
    read(b);
    read(c);
    int maxx=MAX(a,MAX(b,c));
    int sum=a+b+c;
    int ans=sum-maxx;
    if (ans>=maxx-1) printf("Yes
");
    else printf("No
");
    }
    return 0;
}


B. Verse For Santa (CF 1279 B)

题目大意

给定(n,s,)以及(n)个数的数组,问数组从第一个数开始加,其中可以跳过一个数,和不超过(s),问加的个数最多时应该跳过第几个数(不跳过输出(0))。

解题思路

很显然如果我们跳过的话自然是跳过前面最大的那个,是否跳过就看跳过的话增加的时间能不能再增加一个数甚至更多。

神奇的代码
#include <bits/stdc++.h>
#define MIN(a,b) ((((a)<(b)?(a):(b))))
#define MAX(a,b) ((((a)>(b)?(a):(b))))
#define ABS(a) ((((a)>0?(a):-(a))))
#define MP(a,b) make_pair((a),(b))
#define PB push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<LL> VL;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;

template <typename T>
void read(T &x) {
    int s = 0, c = getchar();
    x = 0;
    while (isspace(c)) c = getchar();
    if (c == 45) s = 1, c = getchar();
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
    int b[40], l = 0;
    if (x < 0) putchar(45), x = -x;
    while (x > 0) b[l++] = x % 10, x /= 10;
    if (!l) putchar(48);
    while (l) putchar(b[--l] | 48);
    putchar(c);
}

const int N=1e5+8;

LL sum[N],a[N],ans,s;

int n,qwq,qaq;

int main(void) {
    //ios::sync_with_stdio(false);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int kase; read(kase);
    for (int i = 1; i <= kase; i++) {
        bool flag=false;
        //printf("Case #%d: ", i);
        read(n);
        read(s);
        qwq=qaq=0;
        for(int u,i=1;i<=n;++i){
            read(u);
            if (flag) continue;
            if (s>=0&&s-u<0) {if (u>qwq+s-u) qaq=0; flag=true;}
            s-=u;
            if (u>qwq){
                qwq=u;
                qaq=i;
            }
        }
        if (s>=0&&flag==false) qaq=0;
        printf("%d
",qaq);
    } 
    return 0;
}


C. Stack of Presents (CF 1279 C)

题目大意

给定(n)个礼物,从左到右标号。现在要依次给小朋友送礼物,共(k)个礼物序号分别为(x_1,x_2,...,x_k),如果当前要送的礼物(x_i)不在第一个,那么圣诞老人要依次把前面的礼物放到一边,直到放了(m)个礼物后,(x_i)的礼物在第一个位置,然后送了礼物后再把放出来的礼物放回去,此时放回去的顺序可以自己决定,此时会耗(2*m+1)个体力值。问送完(k)个礼物最少消耗的体力值是多少。

解题思路

对于一个送出去的礼物(x_i),送出去后整理前面的礼物时,按照要送的礼物的顺序(如果在这里面的话)排好,这样总消耗的体力值最小。

神奇的代码
#include <bits/stdc++.h>
#define MIN(a,b) ((((a)<(b)?(a):(b))))
#define MAX(a,b) ((((a)>(b)?(a):(b))))
#define ABS(a) ((((a)>0?(a):-(a))))
#define MP(a,b) make_pair((a),(b))
#define PB push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<LL> VL;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;

template <typename T>
void read(T &x) {
    int s = 0, c = getchar();
    x = 0;
    while (isspace(c)) c = getchar();
    if (c == 45) s = 1, c = getchar();
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
    int b[40], l = 0;
    if (x < 0) putchar(45), x = -x;
    while (x > 0) b[l++] = x % 10, x /= 10;
    if (!l) putchar(48);
    while (l) putchar(b[--l] | 48);
    putchar(c);
}

int main(void) {
    //ios::sync_with_stdio(false);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int kase; read(kase);
    for (int i = 1; i <= kase; i++) {
        //printf("Case #%d: ", i);
        int n,m;
        LL ans;
        read(n);
        read(m);
        vector<int> qwq(n+1);
        for(int u,i=1;i<=n;++i) {
            read(u);
            qwq[u]=i;
        }
        ans=0;
        int l=0;
        for(int u,i=1;i<=m;++i){
            read(u);
            if (l<qwq[u]) {ans+=2ll*(qwq[u]-i)+1ll;l=qwq[u];}
            else ++ans;
        }
        printf("%lld
",ans);
    }
    return 0;
}


D. Santa's Bot (CF 1279 D)

题目大意

给定(n)个孩子喜欢的礼物的序号的清单,然后随机选一个孩子(a),从这个孩子期望的礼物随机选一个(k),再随机选一个孩子(b)(可能为同一个),如果(b)的期望礼物里也有(k),则圣诞老人就可以送礼物出去,否则不可以。问圣诞老人可以送礼物出去的概率是多少。

解题思路

概率题,由于按题目考虑计算的话会在找哪些孩子会期望该礼物耗很大时间,我们考虑枚举礼物,然后考虑该礼物送出去的概率。
我们枚举了一个礼物(gift_i),考虑枚举的两个人,对于第一个人(a),设他期望的礼物数为(num_a),则选他的概率应该是(dfrac{1}{num_a}),对于第二个人(b),由于(b)的礼物清单里要有(gift_i),设选(gift_i)的人数有(cnt_{gift_i}),则选上(b)的概率为(dfrac{1}{cnt_{gift_i}})。由此,对于一个礼物,它能送出去的概率即为(sumlimits_{a wish gift_i}dfrac{1}{num_a}*dfrac{1}{cnt_{gift_i}}),由于各礼物都是等概率抽到,则答案就是(dfrac{1}{kk}sumlimits_isumlimits_{a wish gift_i}dfrac{1}{num_a}*dfrac{1}{cnt_{gift_i}}),其中(kk)是礼物种类,(cnt_i)是期望礼物(i)的人数,(num_i)是孩子(i)期望的礼物数。

神奇的代码
#include <bits/stdc++.h>
#define MIN(a,b) ((((a)<(b)?(a):(b))))
#define MAX(a,b) ((((a)>(b)?(a):(b))))
#define ABS(a) ((((a)>0?(a):-(a))))
#define MP(a,b) make_pair((a),(b))
#define PB push_back
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<LL> VL;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;

template <typename T>
void read(T &x) {
    int s = 0, c = getchar();
    x = 0;
    while (isspace(c)) c = getchar();
    if (c == 45) s = 1, c = getchar();
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
    if (s) x = -x;
}

template <typename T>
void write(T x, char c = ' ') {
    int b[40], l = 0;
    if (x < 0) putchar(45), x = -x;
    while (x > 0) b[l++] = x % 10, x /= 10;
    if (!l) putchar(48);
    while (l) putchar(b[--l] | 48);
    putchar(c);
}

const int N=1e6+8;

const LL mo=998244353;

LL sum[N];

int cnt[N];

int n,ma;

LL invn,invk,ans;

LL kuai(int a,LL b){
    LL qwq=1;
    LL aa=a;
    while(b){
        if (b&1) qwq=qwq*aa%mo;
        aa=aa*aa%mo;
        b>>=1;
    }
    return qwq;
}

int main(void) {
    //ios::sync_with_stdio(false);
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    read(n);
    invn=kuai(n,mo-2);
    ma=0;
    for(int k,i=1;i<=n;++i){
        read(k);
        invk=kuai(k,mo-2);
        for(int u,j=1;j<=k;++j){
            read(u);
            ma=MAX(ma,u);
            sum[u]=(sum[u]+invk)%mo;
            ++cnt[u];
        }
    }
    ans=0;
    for(int i=1;i<=ma;++i){
        if (cnt[i]==0) continue;
        ans=(ans+sum[i]*cnt[i]%mo*invn%mo)%mo;
    }
    ans=(ans*invn)%mo;
    printf("%lld
",ans);
    return 0;
}


写题实况

原文地址:https://www.cnblogs.com/Lanly/p/12121532.html