整理的算法模板合集: ACM模板

点我看算法全家桶系列!!!

实际上是一个全新的精炼模板整合计划


目录

  • Educational Codeforces Round 106 (Rated for Div. 2)(A ~ D)
  • A. Domino on Windowsill
  • B. Binary Removals
  • C. Minimum Grid Path
  • D. The Number of Pairs
  • E. Chaotic Merge
  • F. Diameter Cuts
  • G. Graph Coloring

Educational Codeforces Round 106 (Rated for Div. 2)(A ~ D)

A. Domino on Windowsill

Problem

Solution

签到题…画个图算一下就好

Code

// Problem: A. Domino on Windowsill
// Contest: Codeforces - Educational Codeforces Round 106 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1499/problem/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>using namespace std;
const int N = 50007;int n, k1, k2, w, b;void solve()
{scanf("%d%d%d%d%d", &n, &k1, &k2, &w, &b);if(k1 < k2) swap(k1, k2);if(w <= (k1 - k2) / 2 + k2 && b <= (k1 - k2) / 2 + n - k1) {puts("YES");}else puts("NO");
}int main()
{int t;scanf("%d", &t);while(t -- ) {solve();}return 0;
}

B. Binary Removals

Problem

Solution

经典排序题,显然相邻的不能同时丢掉,也就意味着如果出现两个需要丢掉的数是相邻的,那么就失败了,否则一定成功。所以我们只需要找一下两个需要丢掉的,也就是前面是两个1,后面是两个0,这样就没办法同时丢掉,输出 NO

Code

// Problem: B. Binary Removals
// Contest: Codeforces - Educational Codeforces Round 106 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1499/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>using namespace std;const int N = 50007;int n, m, t;
string a;void solve()
{bool left = false, right = false;cin >> a;int len = a.length();int pos = -1;for(int i = 0; i < len; ++ i) {if(a[i] == '1' && a[i + 1] == '1') {left = true;pos = i + 2;break;}}for(int i = pos; i < len; ++ i) {if(a[i] == '0' && a[i + 1] == '0') {right = true;break;}}if(left && right) {puts("NO");}else puts("YES");
}int main()
{scanf("%d", &t);while(t -- ) {solve();}return 0;
}

C. Minimum Grid Path

Problem

Solution

显然第一印象就是我们对于 cic_ici​ 小的就多走一点,cic_ici​ 大的就少走一点,发现少走一点,也就至少走一步,如果我们当前的 cic_ici​ 是最小的,那么显然直接走到头也就是走完这个方向的所有步数是最优的。有了这个印象以后,我们分析题意,发现每次选择方向必须是不同的方向,所以我们走的时候必须上,右,上,右…这样交替地行走,所以我们直接奇偶交替行走,分别选择不同的 cic_ici​ ,因为往右走和往上走都是一样的,都是走 nnn 步,一共走 2×n2\times n2×n 步,所以向上向右,谁奇谁偶是无所谓的,我们O(n)O(n)O(n)贪心地走一遍即可。

Code

// Problem: C. Minimum Grid Path
// Contest: Codeforces - Educational Codeforces Round 106 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1499/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>using namespace std;
#define int long long
const int N = 500007, INF = 2e9;int t, n, m;
int a[N];void solve()
{scanf("%lld", &n);for(int i = 1; i <= n; ++ i) scanf("%lld", &a[i]);int even = INF, odd = INF, cnt_even = 0, cnt_odd = 0;int ans_even = 0, ans_odd = 0;int ans = (a[1] + a[2]) * n;for(int i = 1; i <= n; ++ i) {if(i & 1) {ans_odd += a[i];cnt_odd ++ ;odd = min(odd, a[i]);}else {ans_even += a[i];cnt_even ++ ;even = min(even, a[i]);}ans = min(ans, ans_even + even * (n - cnt_even) + ans_odd + odd * (n - cnt_odd));}printf("%lld\n", ans);
}signed main()
{scanf("%lld", &t);while(t -- ) {solve();}return 0;
}

D. The Number of Pairs

Problem


Solution

这题之前我写过hhh放一下当时的题解吧,当时直接秒的(那个 t=gcd⁡(a,b)t=\gcd(a,b)t=gcd(a,b) 一定是整数所以一定整除)

以及新鲜出炉的代码(虽然跟之前我写的那份代码没有任何本质上的区别)

Code

// Problem: D. The Number of Pairs
// Contest: Codeforces - Educational Codeforces Round 106 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1499/problem/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)#include <bits/stdc++.h>using namespace std;
#define int long long
const int N = 20000007;int primes[N], cnt;
int n, t;
bool vis[N];
int m[N];
int c, d, x;void init(int n)
{for(int i = 2; i <= n; ++ i) {if(vis[i] == 0) {primes[ ++ cnt] = i;m[i] = 1;}for(int j = 1; j <= cnt && i * primes[j] <= n; ++ j) {vis[i * primes[j]] = true;if(i % primes[j] == 0) {m[i * primes[j]] = m[i];break;}m[i * primes[j]] = m[i] + 1;}}
}int f(int y)
{if((y + d) % c != 0) return 0;return 1 << (m[(y + d) / c]);
}void solve()
{int ans = 0;scanf("%lld%lld%lld", &c, &d, &x);for(int i = 1; i * i <= x; ++ i) {if(x % i == 0) {ans += f(i);if(i * i != x) ans += f(x / i);}}printf("%lld\n", ans);
}signed main()
{init(N - 7);scanf("%lld", &t);while(t -- ) {solve();} return 0;
}

E. Chaotic Merge

我DP不太行,丢给我队友了,等神仙队友写完了更…

官方题解:https://codeforces.com/blog/entry/88812

F. Diameter Cuts

树形DP,有点像2020ICPC南京的M题

G. Graph Coloring

300+的标答就离谱(

Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )相关推荐

  1. Educational Codeforces Round 106 (Rated for Div. 2)

    Educational Codeforces Round 106 (Rated for Div. 2) 题号 题目 知识点 A Domino on Windowsill B Binary Remova ...

  2. Educational Codeforces Round 106 (Rated for Div. 2)D. The Number of Pairs

    Educational Codeforces Round 106 (Rated for Div. 2)D. The Number of Pairs 题目大意 给你三个正整数c,d,k\displays ...

  3. Educational Codeforces Round 133 (Rated for Div. 2)(CD题解)

    Educational Codeforces Round 133 (Rated for Div. 2)CD题解 过AB补CD C. Robot in a Hallway 题意 题意:现有 2∗m 的方 ...

  4. Educational Codeforces Round 106 (Rated for Div. 2) C. Minimum Grid Path 奇偶 + 思维

    传送门 文章目录 题意: 思路: 题意: 给一个二维平面,起点在(0,0)(0,0)(0,0),终点在(n,n)(n,n)(n,n),每次只能往上和往右走,距离随意,总步数不超过nnn,每一步有一个代 ...

  5. Educational Codeforces Round 106 (Rated for Div. 2) D. The Number of Pairs 数论gcd

    传送门 文章目录 题意: 思路: 题意: 给三个数c,d,xc,d,xc,d,x,求满足c∗lcm(a,b)−d∗gcd(a,b)=xc*lcm(a,b)-d*gcd(a,b)=xc∗lcm(a,b) ...

  6. Educational Codeforces Round 137 (Rated for Div. 2)A~D题解

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 目录 A. Password B. Permutation Val C. Save the Magazines D. Pro ...

  7. Educational Codeforces Round 140 (Rated for Div. 2)A~D题解

    文章目录 A B C D A 思路:就是三个点如果任意两个点的横坐标相同,并且任意两个点的纵坐标相同的话就不行. #include <cstring> #include <iostr ...

  8. Educational Codeforces Round 113 (Rated for Div. 2) A~D题解

    文章目录 A. Balanced Substring B. Chess Tournament C. Jury Meeting D. Inconvenient Pairs A. Balanced Sub ...

  9. Educational Codeforces Round 127 (Rated for Div. 2) A~E 题解

    A. String Building 题意: 问是否可以用 a a . a a a . b b . b b b aa.aaa.bb.bbb aa.aaa.bb.bbb 拼成所给字符串. 思路: 将原字 ...

最新文章

  1. jquery checkbox勾选/取消勾选的诡异问题
  2. Linux web服务安装apache 思路 (源码编译,自己定义服务)
  3. 操作系统 综合应用题知识点更新【章节考试重点(进程同步、处理机调度与死锁、存储管理、设备管理、文件管理)】
  4. 写 Python 爬虫 5 年,复制粘贴一直是我赖以生存的核心技能,直到我看到这些腾讯阿里大佬们的技术公众号,太强了...
  5. 用HTML+CSS画出一个同心圆
  6. 使用Mongo Shell和Java驱动程序的MongoDB Map Reduce示例
  7. bilstmcrf词性标注_BiLSTM+CRF 的实现详解
  8. cartographer探秘第二章之论文解析
  9. easyexcel怎么设置表头宽度_EasyExcel的不确定表头(根据数据生成表头)的excel导出和二级表头以及设置表头的宽度自适应...
  10. 美化版缤纷彩色文字广告代码文字+网站添加AD教程
  11. 【Python cursor指针】——Python Tkinter Cursor鼠标指针属性值
  12. 泛微OA-流程存储数据说明(表单主表+明细表)
  13. 统计案例分析之预测社会消费品零售总额
  14. 求字符串的全排列的递归实现(对字符串中有相同字符也适用)
  15. 继承CAcUiStringEdit,改变编辑框的字体颜色,以及背景的颜色
  16. Python-rot13-替换式密码
  17. Ruby On Rails-2.0.2源代码分析(1)-Rails的启动
  18. 智慧图书馆,RFID技术在图书借还,图书防盗中的应用优势
  19. ppt如何转换pdf
  20. [ XJTUSE ]JAVA语言基础知识——7.12 JTable实现表格

热门文章

  1. 推荐 | Pair,医学图像标注神器
  2. 10 个开源 Python OpenCV 小项目,YouTube热门
  3. 基于K-Means聚类算法的主颜色提取
  4. 2014百度之星 Xor Sum(字典树+贪心)
  5. 4K P60 444 相关的事
  6. P1515 旅行(简单搜索)
  7. NodeJS Events模块源码学习
  8. linux装nginx
  9. onCompositionStart与compositionend
  10. TI PDK3.0 qt 交叉编译环境设置