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\n");
    else printf("No\n");
    }
    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\n",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\n",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}}\)。由此,对于一个礼物,它能送出去的概率即为\(\sum\limits_{a\ wish\ gift_i}\dfrac{1}{num_a}*\dfrac{1}{cnt_{gift_i}}\),由于各礼物都是等概率抽到,则答案就是\(\dfrac{1}{kk}\sum\limits_i\sum\limits_{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\n",ans);
    return 0;
}


Educational Codeforces Round 79 (Rated for Div. 2)

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

时间: 2024-10-28 21:58:13

Educational Codeforces Round 79 (Rated for Div. 2)的相关文章

Educational Codeforces Round 79 (Rated for Div. 2) D. Santa&#39;s Bot

链接: https://codeforces.com/contest/1279/problem/D 题意: Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of

Educational Codeforces Round 79 (Rated for Div. 2) C. Stack of Presents

链接: https://codeforces.com/contest/1279/problem/C 题意: Santa has to send presents to the kids. He has a large stack of n presents, numbered from 1 to n; the topmost present has number a1, the next present is a2, and so on; the bottom present has numbe

比赛记录[Educational Codeforces Round 79 (Rated for Div. 2)]

首先声明,这是本蒟蒻第一次打CF,也是第一次发博客,大佬勿喷 A                 B                 C              D              E               F 数学      贪心 N/A           N/A             N/A          N/A A. New Year Garland 由于第一次打CF,比赛节奏和心态调整不太好,加上英文题目难以理解(本蒟蒻英语太垃圾),在第一题上水了15分钟. 第

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm