Codeforces Round #697 (Div. 3)A~G解题报告

Codeforces Round #697 (Div. 3)A~G解题报告

题 A Odd Divisor

题目介绍

在这里插入图片描述

解题思路

乍一想本题,感觉有点迷迷糊糊,但是证难则反,直接考虑没有奇数因子的情况,即 N = 2 i 2^{i} 2i,那么当N != 2 i 2^i 2i时,就有 奇数因子
注意使用 LL

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;

bool Check(LL x)
{
    while (x != 1)
    {
        if (x & 1)
        {
            return true;
        }
        x >>= 1;
    }
    return false;
}

int main()
{
    LL n;
    int t;
    cin >> t;
    while (t -- )
    {
        scanf("%lld", &n);
        if (Check(n))
        {
            cout << "YES" << endl;
        }
        else
        {
            cout << "NO" << endl;
        }
    }
    return 0;
}




题 B New Year’s Number

题目介绍

在这里插入图片描述

解题思路

直接就是一个 dp裸题,倘若 x 是2021 与 2020 的若干和,那么 x == 2020 或 x ==2021或者x - 2020 满足要求 或者 x - 2021满足要求
有了上述的递推公式,直接开bool数组进行动态规划即可

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 1000010;
bool st[N];

int main()
{
    int n;
    int t;
    cin >> t;
    st[2020] = st[2021] = true;
    for (int i = 2023; i < N; i ++ )
    {
        st[i] = st[i - 2021] | st[i - 2020];
    }
    while (t -- )
    {
        scanf("%d", &n);
        if (st[n])
        {
            cout << "YES\n";
        }
        else
        {
            cout << "NO\n";
        }
    }
    return 0;
}




题 C Ball in Berland

题目介绍

在这里插入图片描述

解题思路

同样是直接统计不太方便,我们直接反向思考,计算出总方案数量,减去不合法方案数量,得到结果
总方案数量 = k * (k - 1) / 2
不合法方案数量 = 同一个男生被选中两次 + 同一个女生被选中两次
记得开 LL

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 200010;
int boy_cnt[N], girl_cnt[N];
int n, m, k;

int main()
{
    int t;
    cin >> t;
    LL res = 0;
    while (t -- )
    {
        scanf("%d%d%d", &n, &m, &k);
        res = LL(k) * (k - 1);
        memset(boy_cnt, 0, sizeof boy_cnt);
        memset(girl_cnt, 0, sizeof girl_cnt);
        for (int i = 1; i <= k; i ++ )
        {
            static int tmp;
            scanf("%d", &tmp);
            boy_cnt[tmp] ++;
        }
        for (int i = 1; i <= k; i ++ )
        {
            static int tmp;
            scanf("%d", &tmp);
            girl_cnt[tmp] ++;
        }

        for (int i = 1; i <= n; i ++ )
        {
            res -= LL(boy_cnt[i]) * (boy_cnt[i] - 1);
        }
        for (int i = 1; i <= m; i ++ )
        {
            res -= LL(girl_cnt[i]) * (girl_cnt[i] - 1);
        }
        /// cout << "#############\n";
        printf("%lld\n", res / 2);
        /// cout << res / 2 << endl;
    }
    return 0;
}




题 D Cleaning the Phone

题目介绍

在这里插入图片描述

解题思路

错误思路
本来将题目想成了 dp 进行求解,直接超时没商量,考虑一下复杂度,确实有问题
O(N*2N)太大
正解
这个题目应该进行贪心的,先处理出来 b i b_i bi=1数组, b i b_i bi=2数组,然后对数组可以清空的内存进行排序。
排序后进行求取数组的前缀和,方便我们下面两种做法降低复杂度。
下面有两种问题的求解办法,
方法一、二分
对于 对于 每个 b i b_i bi=1的下标进行枚举,然后对 b i b_i bi=2数组进行二分,查找到满足释放内存的最小前缀数组的下标。 O ( N l o g ( N ) ) O(Nlog(N)) O(Nlog(N))
方法二、双指针
先找到一个合法解,然后数组下标进行移动,另一个指针作相应的调整即可。 O ( N ) O(N) O(N)
但是算上排序,最终复杂度为 O ( N l o g ( N ) ) O(Nlog(N)) O(Nlog(N))

但是本题有一个最狗的地方,cmp函数被卡了,可以直接写归并排序,或者cmp函数别写等号,否则会超时

AC代码

双指针
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;

const int N = 200010, INF = 0x3f3f3f3f;
LL a[N];
LL c[N], d[N];
LL n, m;
int idx1, idx2;

bool cmp(LL x, LL y){ return x > y; }
void show()
{
    for (int i = 1; i <= idx1; i ++ )   cout << c[i] << " ";    cout << endl;
    for (int i = 1; i <= idx2; i ++ )   cout << d[i] << " ";    cout << endl;
}

int main()
{
    int T;  cin >> T;
    while (T -- )
    {
        scanf("%lld %lld", &n, &m);
        for (LL i = 1; i <= n; i ++ )
            scanf("%lld", &a[i]);
        c[0] = d[0] = 0LL;
        idx1 = idx2 = 0;
        for (LL i = 1, b; i <= n; i ++ )
        {
            scanf("%lld", &b);
            if (b & 1)  c[++ idx1] = (a[i]);
            else    d[++ idx2] = (a[i]);
        }
        sort(c + 1, c + idx1 + 1, cmp);
        sort(d + 1, d + idx2 + 1, cmp);
        /// show();
        for (int i = 1; i <= idx1; i ++ )   c[i] += c[i - 1];
        for (int i = 1; i <= idx2; i ++ )   d[i] += d[i - 1];
        /// show();
        if (c[idx1] + d[idx2] < m)
        {
            printf("-1\n");
        }
        else
        {
            int i, j, res = INF;
            for (i = 0; i <= idx1; i ++ )   // 尺取法的起点
                if (c[i] + d[idx2] >= m)    // c[i] 的开头
                    break;
            j = idx2;

            res = min(res, i + j + j);

            // 此时i, j可以进行 尺取法 了
            while (i <= idx1 && j >= 0)
            {
                while (j >= 0 && i <= idx1 && c[i] + d[j] < m)
                {
                    i ++;
                }
                if (i <= idx1 && j >= 0)
                    res = min(res, i + j + j);
                while (i <= idx1 && j >= 0 && c[i] + d[j] >= m)
                {
                    if (i <= idx1 && j >= 0)
                        res = min(res, i + j + j);
                    j --;
                }

            }
            if (res == INF) res = -1;
            printf("%d\n", res);
        }
    }
    return 0;
}



二分
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 200010, INF = 0x3f3f3f3f;
int n, m;
int a[N];
LL c[N], d[N];
int szc, szd;


bool cmp(LL a, LL b)
{
    return a > b;
}
int main()
{
    int T;  cin >> T;
    while (T -- )
    {
        static int b;
        cin >> n >> m;
        for (int i = 1; i <= n; i ++ )  scanf("%d", &a[i]);
        c[0] = d[0] = szc = szd = 0;
        for (int i = 1; i <= n; i ++ )
        {
            scanf("%d", &b);
            if (b & 1)
                c[++ szc] = a[i];
            else
                d[++ szd] = a[i];
        }
        sort(c + 1, c + szc + 1, cmp);
        sort(d + 1, d + szd + 1, cmp);

        for (int i = 1; i <= szc; i ++ )    c[i] += c[i - 1];
        for (int i = 1; i <= szd; i ++ )    d[i] += d[i - 1];

        if (c[szc] + d[szd] < m)
        {
            puts("-1");
        }
        else
        {
            int res = INF;
            for (int i = 0; i <= szc; i ++ )
            {
                if (c[i] + d[szd] < m)
                    continue;
                else if (c[i] >= m)
                {
                    res = min(res, i);
                    break;
                }
                else
                {
                    static int tmp;
                    tmp = lower_bound(d + 1, d + 1 + szd, m - c[i]) - d;
                    res = min(res, tmp + tmp + i);
                }
            }
            cout << res << endl;
        }

    }

    return 0;
}

题 E Advertising Agency

题目介绍

在这里插入图片描述

解题思路

肯定是先对 博主的 粉丝数量进行排序,贪心的请博主即可,这个题目主要是求解 排列组合问题。
C i j C_i^j Cij= C i − 1 j C_{i-1}^j Ci1j+ C i − 1 j − 1 C_{i-1}^{j-1} Ci1j1
利用dp直接进行求解,关键是初始化写好就可以了 C i i C_i^i Cii= C i 0 C_i^0 Ci0=1

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 1010, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n, k;
int a[N];
int f[N][N];


bool cmp(LL a, LL b)
{
    return a > b;
}

int main()
{
    memset(f, 0, sizeof f);
    for (int i = 0; i < N; i ++ )
        f[i][i] = f[i][0] = 1;
    for (int i = 1; i < N; i ++ )
        for (int j = 1; j <= i; j ++ )
            f[i][j] = (f[i - 1][j] + f[i - 1][j - 1]) % MOD;

    int T;  cin >> T;
    while (T -- )
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i ++ )
            scanf("%d", &a[i]);
        sort(a + 1, a + n + 1, cmp);

        static int x, sidx, eidx;
        x = a[k], sidx = -1, eidx = -1;
        for (int i = 1; i <= n; i ++ )
        {
            if (a[i] == x)
            {
                if (sidx == -1) sidx = i;
                eidx = i;
            }
        }
        cout << f[eidx - sidx + 1][k - sidx + 1] << endl;

    }

    return 0;
}




题 F Unusual Matrix

题目介绍

在这里插入图片描述

解题思路

题目问的是能否从 A n ∗ n A_{n*n} Ann矩阵转换到 B n ∗ n B_{n*n} Bnn矩阵,由转换的性质,同一个行/列转换两次是没有任何作用的,因此我们枚举第一行需要操作/与不需要操作,那么第一行的元素能操作的对象只有列,因此列是否需要操作就得以确定,列确定,那么,反过来行也就得以确定,最终得到结果。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 1010, INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int n;
char a[N][N], b[N][N];
char c[N][N];

inline void Change(char c[][N], int x, bool row);
bool Same(char c[][N], char d[][N], int x)
{
    for (int i = 1; i <= n; i ++ )
        if (c[x][i] != d[x][i])
            return false;
    return true;
}

bool Check(char c[][N], char d[][N])
{
    // 第一行是不需要动的,我们看看 列 的影响
    for (int i = 1; i <= n; i ++ )
        if (c[1][i] != d[1][i])
            Change(c, i, false);
    for (int i = 2; i <= n; i ++ )
    {
        if (c[i][1] != d[i][1]) // 修改 行
            Change(c, i, true);
        if (!Same(c, d, i))
            return false;
    }
    return true;
}
inline void Change(char c[][N], int x, bool row)
{
    if (row)    // row
        for (int i = 1; i <= n; i ++ )
        {
            c[x][i] = 97 - c[x][i];   // 48 + 49 - c[i]
        }
    else        // col
        for (int i = 1; i <= n; i ++ )
        {
            c[i][x] = 97 - c[i][x];   // 48 + 49 - c[i]
        }
}

int main()
{
    int T;  cin >> T;
    while (T -- )
    {
        cin >> n;
        for (int i = 1; i <= n; i ++ )
            scanf("%s", a[i] + 1);
        for (int i = 1; i <= n; i ++ )
            scanf("%s", b[i] + 1);
        memcpy(c, a, sizeof c);

        if (Check(c, b) || (memcpy(c, a, sizeof c), Change(c, 1, true), Check(c, b)))
            puts("YES");
        else
            puts("NO");

    }
    return 0;
}




题 G Strange Beauty

题目介绍

在这里插入图片描述

解题思路

这个题目是一个比较巧妙地dp题目,对于一个 B e u t i f u l A r r a y Beutiful Array BeutifulArray我们将其非降序排序之后可以发现,后面的数字都是可以整除前面的,这是一个充分必要的条件
那么最长的数组对应着最长的整除序列
而且还有 一个坑点,鸡儿数字还可能相等,也就是我们需要先预处理出 X X X出现的次数
定义一个数组 f i f_i fi表示,以数字 i 作为最大值,可以构成 B e a u t i f u l A r r a y BeautifulArray BeautifulArray的最大长度,
f i f_i fi = i i i出现次数+ m a x max max{因子的 j 的 f j f_j fj}
下面是dp过程,而且为了方便书写,降低时间复杂度,直接将因子的相加写入了 因子的循环中

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

typedef long long LL;
const int N = 200010, INF = 0x3f3f3f3f;

int cnt[N];
int a[N];
bool st[N];
int f[N];
int n;

int main()
{
    int T;  cin >> T;
    while (T -- )
    {
        static int res;
        cin >> n;
        for (int i = 1; i <= n; i ++ )
            scanf("%d", &a[i]);

        res = INF;
        sort(a + 1, a + 1 + n);
        memset(cnt, 0, sizeof cnt);
        memset(st, false, sizeof st);
        memset(f, 0, sizeof f);
        for (int i = 1; i <= n; i ++ )  cnt[a[i]] ++;

        for (int i = 1, val; i <= n; i ++ )
        {
            val = a[i];
            if (st[val])    continue;
            st[val] = true;
            // cnt[val] = max(cnt[val], 1);
            f[val] = f[val] + cnt[val];    // 给自己加的
            for (int j = val + val; j < N; j += val)
            {
                f[j] = max(f[j], f[val]); // 给别的数字加的
            }
            res = min(res, n - f[val]);
        }

        cout << res << endl;

    }
    return 0;
}




本次CF小结

  • 小心快排可能被卡,导致超时,可以通过 修改cmp函数,或者是直接使用 归并排序来解决
  • 其次,考虑问题的时候,尤其是数量的问题,可以使用容斥定理,证难则反
  • 贪心、结合二分、或者是双指针来优化复杂度,有时候考虑dp背包复杂度太高
  • 求解组合数的常用方法要记住, dp,逆元,卢卡斯定理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值