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

题 A Odd Divisor

题目介绍

解题思路

乍一想本题,感觉有点迷迷糊糊,但是证难则反,直接考虑没有奇数因子的情况,即 N = 2i2^{i}2i,那么当N != 2i2^i2i时,就有 奇数因子
注意使用 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)太大
正解
这个题目应该进行贪心的,先处理出来bib_ibi​=1数组,bib_ibi​=2数组,然后对数组可以清空的内存进行排序。
排序后进行求取数组的前缀和,方便我们下面两种做法降低复杂度。
下面有两种问题的求解办法,
方法一、二分
对于 对于 每个 bib_ibi​=1的下标进行枚举,然后对 bib_ibi​=2数组进行二分,查找到满足释放内存的最小前缀数组的下标。 O(Nlog(N))O(Nlog(N))O(Nlog(N))
方法二、双指针
先找到一个合法解,然后数组下标进行移动,另一个指针作相应的调整即可。O(N)O(N)O(N)
但是算上排序,最终复杂度为 O(Nlog(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];elsed[++ 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

题目介绍

解题思路

肯定是先对 博主的 粉丝数量进行排序,贪心的请博主即可,这个题目主要是求解 排列组合问题。
CijC_i^jCij​=Ci−1jC_{i-1}^jCi−1j​+Ci−1j−1C_{i-1}^{j-1}Ci−1j−1​
利用dp直接进行求解,关键是初始化写好就可以了 CiiC_i^iCii​=Ci0C_i^0Ci0​=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

题目介绍

解题思路

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

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)    // rowfor (int i = 1; i <= n; i ++ ){c[x][i] = 97 - c[x][i];   // 48 + 49 - c[i]}else        // colfor (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");elseputs("NO");}return 0;
}



题 G Strange Beauty

题目介绍

解题思路

这个题目是一个比较巧妙地dp题目,对于一个 BeutifulArrayBeutiful ArrayBeutifulArray我们将其非降序排序之后可以发现,后面的数字都是可以整除前面的,这是一个充分必要的条件
那么最长的数组对应着最长的整除序列
而且还有 一个坑点,鸡儿数字还可能相等,也就是我们需要先预处理出 XXX出现的次数
定义一个数组 fif_ifi​表示,以数字 i 作为最大值,可以构成 BeautifulArrayBeautifulArrayBeautifulArray的最大长度,
fif_ifi​ = iii出现次数+maxmaxmax{因子的 j 的fjf_jfj​}
下面是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,逆元,卢卡斯定理

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

  1. Codeforces Round #697 (Div.3) A~G解题报告与解法证明

    题目大体概括 A #include <cstdio> #include <cstring> #include <algorithm> #include <io ...

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

    Codeforces Round #693 (Div. 3)A~G解题报告 A Cards for Friends 原题信息 http://codeforces.com/contest/1472/pr ...

  3. Codeforces Round #700 (Div. 2)A~D2解题报告

    Codeforces Round #700 (Div. 2)A~D2解题报告 A Yet Another String Game 原题链接 http://codeforces.com/contest/ ...

  4. Codeforces Round #830 (Div. 2) B. Ugu 解题报告

    原题链接: Problem - B - Codeforces 题目描述: A binary string is a string consisting only of the characters 0 ...

  5. 【Codeforces Round #570 (Div. 3) A-H 】解题报告

    cf570 cf570A 题意 一个数的数位和%4如果为 0 是我们要的答案 问加几次能使得他数位和 %4 为0 暴力即可 /*if you can't see the repayWhy not ju ...

  6. Codeforces Round #701 (Div. 2)赛后补题报告(A~D)

    Codeforces Round #701 (Div. 2)赛后补题报告(A~D) A. Add and Divide 原题信息 http://codeforces.com/contest/1485/ ...

  7. Codeforces Round #827 (Div. 4) A~G

     比赛链接:Dashboard - Codeforces Round #827 (Div. 4) - Codeforces 目录 A Sum B Increasing C Stripes D. Cop ...

  8. Codeforces Round #697 (Div. 3) G. Strange Beauty 线性筛变形思维

    原题链接 题意: n个数,问删除多少个数之后剩下的数组中的数满足:对于任意一对i,j满足ai%aj==0 || aj%ai ==0. 思路: 题目给的ai只有2e5,很容易想到从ai入手,算出如果当前 ...

  9. Educational Codeforces Round 47 (Div 2) (A~G)

    目录 Codeforces 1009 A.Game Shopping B.Minimum Ternary String C.Annoying Present D.Relatively Prime Gr ...

最新文章

  1. UTA研究团队提出首个3D点云+GAN新方法,让机器人“眼神”更犀利 | AI日报
  2. PyTorch 实现 GAN 生成式对抗网络 含代码
  3. 数据结构与算法 / 冒泡排序最坏情况下的时间复杂度解析
  4. 如何更换outlook邮件的背景色
  5. 第一次冲刺-站立会议03
  6. 商标注册流程与注意事项
  7. Windows系统端口占用,使用命令行查找并杀进程
  8. mvcframeworkProgramming ASP.NET MVC-Fundamentals of ASP.NET MVC(四)Controller
  9. Linux Ubuntu18系统下最简单开机自启动程序方法 | 九七的Linux
  10. Linux下安装SVN服务(CentOS7下)单仓库版(老威改良版)
  11. Netty实战:设计一个IM框架就这么简单!
  12. 中国足球有救了,因为这两个人已经成为中超新时代教练的一股清流
  13. 在3D空间中绘制四边形
  14. 《解析深度学习》部分笔记
  15. Error reading Prometheus: An error occurred within the plugin
  16. Webpack工具 - 打包执行中的奇奇怪怪
  17. 艾伟:Memcached深度分析
  18. 设计改变世界,2022年戴森设计大奖作品征集正式开启
  19. 通过 命令行PowerShell 开启、关闭 Windows 10 移动热点,可修改SSID、密码。解决神州网信政府版win10无法管理windows自带移动热点问题。
  20. python学生成绩管理系统-增删查改

热门文章

  1. 二分法的应用:POJ1064 Cable master
  2. CodeForces 903D Almost Difference
  3. 转!!ftp的主动模式(port)与被动模式(PASV)
  4. 13.强符号和弱符号
  5. Maven整合Spring3.0+Mybatis3.2+Struts2.3+查找坐标+jar包依赖(五)
  6. 【转】构建微服务架构的最佳实践2/3
  7. asp.net学习之再论sqlDataSource
  8. STL中empty()函数的误用
  9. boost.asio防止恶意空连接的方法
  10. Service 的生命周期;两种启动方法和区别