文章目录
The First Week
未觉池塘春草梦,阶前梧叶已秋声。 ————朱熹
一、 前言
周二oi赛制,蓝桥杯训练,打完就跑了没来得及补题,360分打得很差。
周四补题周二的蓝桥杯训练,前俩天网站的几场比赛也都来不及参加。
周六蓝桥杯,别的没有了。
周日天梯训练,可以说是最简单的一次了,但还是好几题有一个案例没过,调整心态吧,下次直接往后走,别浪费时间了。
周六天梯赛,最佳女队和国二。
周日CF训练赛。
二、使用步骤
1.线性DP
<1>(洛谷P8725)
[蓝桥杯 2020 省 AB3] 画中漂流
蓝桥杯 2020 第三轮省赛 AB 组 I 题。
没看出来是dp,看过解析以后能写,用的dfs拿了二十分,oi赛制改也不知道改。实在没有敏感度
题解:
给出三个整数底线D,时间T,和体力M,不进则退的划船,体力用完时间到了没有到达底线的方案有多少种。
二维dp的状态表示第i秒剩余j体力有几种情况,dp必须把答案设成dp状态,d用来判断状态即可。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int dp[3005][1505];
//dp[i][j]表示第i秒留下j点体力的方案数
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int d,t,m;
cin >> d >> t >> m;
dp[0][m] = 1;
//初始状态一种情况
for (int i = 1; i <= t; i++ ) {
for (int j = 0; j <= m; j++) {
int len = 2 * (m - j) + d - i;
//是否安全
if (len > 0) {
dp[i][j] = (dp[i-1][j] + dp[i-1][j+1]) % 1000000007;
}
//不mod的话只能过三个点
}
}
cout << dp[t][0]<< endl;
return 0;
}
2.并查集
for (int i = 1; i <= n; i++) {
a[i] = i;
}
//初始所有父节点是自己
int find(int q) {
if (q == a[q])return q;
return a[q] = find(a[q]);
}
//查找根节点
a[find(x)] = find(y);
//将x的集合连到y上去
<1>(洛谷P8654)
[蓝桥杯 2017 国 C] 合根植物
以前没有过的一道题,学了并查集以后又来补了一下,很典型。
题解:
给出m行n列空格,k组合根数据,求最后有多少株合根植物。并查集走一轮,求有几个父节点即可。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000005];
int find(int q){
if(a[q] == q)return q;
return a[q] = find(a[q]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n ,m;
cin >> m >> n;
int k;
cin >> k;
int ans = 0;
for (int i = 1;i <= m*n; i++) {
a[i] = i;
}
while (k--) {
int c,b;
cin >> c >> b;
a[find(c)] = find(b);
}
for (int i = 1; i <= n * m; i++) {
if (find(i) == i)ans++;
}
cout << ans << endl;
return 0;
}
<2>(洛谷P8710)
[蓝桥杯 2020 省 AB1] 网络分析
蓝桥杯 2020 第一轮省赛 A 组 J 题(B 组 J 题)
周二做的训练题,并查集意外又看到了,当时硬拿了20分,用并查集暴力写了一遍70,tle了,好好好非常好,写不出来只有70
题解:
没写出来,放个70的代码吧,如果以后写出来了会来替换。一个很暴力的并查集。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int a[10005];
int ans[10006];
int find(int q){
if(a[q] == q)return q;
return a[q] = find(a[q]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n,m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
a[i] = i;
}
while (m--) {
int w;
cin >> w;
int c, b ;
cin >> c >> b;
if (w == 1) {
a[find(c)] = find(b);
}
else {
for (int i = 1; i <= n; i++ ) {
if(find(i) == find(c)){
ans[i] += b;
}
}
}
}
for(int i = 1; i <= n; i++ ){
cout << ans[i] << ' ';
}
return 0;
}
3.归并排序
还是y总的课,以及几个可视化的博客,看到了一道归并排序的题就去学了。但老感觉不如sort。
从中间位置开始递归,直到只剩一个元素,一一排序并返回。主要是合并俩个有序序列。
void merge_sort(int i, int j) {
if(i >= j)return ;
//仅剩一个元素,开始返回
int mid = i + j >> 1;
//中间位置,+优先级大于>>
merge_sort(i,mid);
merge_sort(mid+1,j);
//给左右俩边分别排序
int k = 0;
int l = i, r = mid + 1;
//k用来储存此次排序的数字
while(l <= mid && r <= j) {
if(q[l] <= q[r]){
tmp[k++] = q[l++];
}
else {
tmp[k++] = q[r++];
}
}
//以上在合并俩个有序数组
while(l <= mid) {
tmp[k++] = q[l++];
}
while(r <= j) {
tmp[k++] = q[r++];
}
//哪个还有剩余的直接输出
for (int p = i,w = 0;p <= j; p++,w++) {
q[p] = tmp[w];
}
//把它有序的存进原数组
}
<1>(AcWing788)
模版题
题解:
长度为n的数列求逆序对,也就是在元素后面但比元素小的个数总和。在归并排序中插入一行result在选用第二种数组的情况下计算即可。
题解:
#include <iostream>
#include <map>
using namespace std;
int a[100005];
long long result = 0;
void merge_sort(int l,int r){
int temp[100005];
if(l >= r){
return ;
}
int mid = l + r >> 1;
merge_sort(l,mid);
merge_sort(mid + 1,r);
int k = 0;
int i = l, j = mid + 1;
while(i <= mid && j <= r) {
if(a[i] <= a[j]){
temp[k++] = a[i++];
}
else {
temp[k++] = a[j++];
result += (mid - i + 1);
}
}
while(i <= mid) {
temp[k++] = a[i++];
}
while(j <= r) {
temp[k++] = a[j++];
}
for (int p = l, w = 0;p <= r; p++,w++) {
a[p] = temp[w];
}
}
int main() {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> a[i];
}
merge_sort(0, n - 1);
cout << result << endl;
return 0;
}
4.快速幂
行我知道我学过,结果果然还是没有写出来,在队内训练赛的时候发现脑子里有点概念但是写不出来,蓝桥杯也有一道类似的没有写出来。
long long quick_power(int base, int power) {
long long result = 1;
//储存快速幂结果
while(power > 0) {
//一直要左移到二进制最后一位
if(power & 1) {
//二进制为1表示要计算这位
result *= base;
result %= 998244353;
//防止过大所以要取模
}
base *= base;
//二进制,直接在底数部分相乘
base %= 998244353;
power = power >> 1;
//二进制左移一位
}
return result;
}
<1>(CF SMU Spring 2024 Team Round 1 H)
Hile and Subsequences’ MEX
三人组队赛,我基本就是看着他写的这题,迷迷糊糊的感觉。这题真的很难懂!
题解:
MEX的是在这个数组中,从0开始不存在的最小数,例如
.𝑀𝐸𝑋 of [1,2,3,4] is 0
.𝑀𝐸𝑋 of [0,1,2,3,4] is 5
.𝑀𝐸𝑋 of [2,3,4,0,2] is 1
输入t组数字n,输出从0到n-1的数组a的所有子数组的MEX和。
以4为例,也就是4个1+2个2+1个3+1个4,也就是2的4次方-1,经过试验确认另一个数也满足此条件,答案要模998244353。
代码:
#include<iostream>
using namespace std;
long long quick_power(long long base, long long power)
//一定要记得给里外都开long long
{
long long result = 1;
//储存快速幂结果
while(power > 0) {
//一直要左移到二进制最后一位
if(power & 1) {
//二进制为1表示要计算这位
result *= base;
result %= 998244353;
//防止过大所以要取模
}
base *= base;
//二进制,直接在底数部分相乘
base %= 998244353;
power = power >> 1;
//二进制左移一位
}
return result;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--) {
long long n;
cin >> n;
cout << quick_power(2,n) - 1 << endl;
}
return 0;
}
5.其它
<1>(洛谷P8715)
[蓝桥杯 2020 省 AB2] 子串分值
蓝桥杯 2020 第二轮省赛 A 组 H 题(B 组 H 题)
题解:
给定一个字符串s,求它的所有非空子串的单个字母的和。
以每个位置为单位,计算这个位置会给多少子串提供价值,用pre和nex数组分别计算前后相同字母出现的位置,中间所有子串都是它的贡献范围。
代码:
#include<iostream>
#include<algorithm>
using namespace std;
int pre[100005],nex[100005];
//分别用来存储前后相同字母出现的位置
int a[30];
//起一个传递的作用
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
string s;
cin >> s;
int ans = 0;
for (int i = 0; i < 30; i++)a[i] = -1;
//第一次出现就将前面赋值为-1
for (int i = 0; i < s.length(); i++) {
pre[i] = a[s[i] - 'a'];
a[s[i] - 'a'] = i;
}
//字母转换为数字数组
for (int i = 0; i < 30; i++)a[i] = s.length();
//最后一次出现就将后置为赋值为s.length()
for (int i = s.length() - 1; i >= 0; i--) {
nex[i] = a[s[i] - 'a'];
a[s[i] - 'a'] = i;
}
for (int i = 0; i < s.length(); i++) {
ans += (i - pre[i]) * (nex[i] - i);
}
//所有当前字母贡献的子串个数
cout << ans << endl;
return 0;
}
<2> (SMU 2024 spring天梯赛4 7-6)
九宫格输入法 分数15
非常有意思的一道题呵呵
题解:
题意非常简单,手机九宫格的使用。
代码:
#include<bits/stdc++.h>
using namespace std;
int main() {
char a[10][6] = {"0 ","1,.?!","2ABC","3DEF","4GHI","5JKL","6MNO","7PQRS","8TUV","9WXYZ"};
string t;
while(cin >> t){
int q = t[0] - '0';
int m = (t.length()-1) % strlen(a[q]);
cout << a[q][m];
}
}
//修正后的代码
以下是我训练原代码,12分
#include<iostream>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
string m;
while(cin >> m) {
int t;
if(m == "00"){
cout << ' ';
continue;
}
else {
t = stoi(m);
}
if(t % 10 == 1) {
if(t == 1){
cout << '1';
}
else if(t == 11) {
cout << ',';
}
else if(t == 111) {
cout << '.';
}
else if(t == 1111) {
cout << '?';
}
else if(t == 11111) {
cout << '!';
}
}
else if(2 <= t % 10 && t % 10 <= 6) {
if(t < 10){
cout << t;
}
else if(t < 100) {
cout << char('A' + 3 * (t % 10 - 2));
}
else if(t < 1000) {
cout << char('B' + 3 * (t % 10 - 2));
}
else {
cout << char('C' + 3 * (t % 10 - 2));
}
}
else if(t == 7){
cout << t;
}
else if(t == 77) {
cout << 'P';
}
else if(t == 777) {
cout << 'Q';
}
else if(t == 7777) {
cout << 'R';
}
else if(t == 77777) {
cout << 'S';
}
else if(t == 8) {
cout << '8';
}
else if(t == 88) {
cout << 'T';
}
else if(t == 888) {
cout << 'U';
}
else if(t == 8888) {
cout << 'V';
}
else if(t == 9) {
cout << '9';
}
else if(t == 99) {
cout << 'W';
}
else if(t == 999) {
cout << 'X';
}
else if(t == 9999) {
cout << 'Y';
}
else if(t == 99999) {
cout << 'Z';
}
else if(t == 0) {
cout << '0';
}
}
return 0;
}
不多说了高下立见
<3>(CF SMU Spring 2024 Team Round 1 C)
这题我也光是题目就看了好久,不知道那个哥怎么看懂还打表打出来的,真的厉害
题解:
一个美丽的数组,例如【1,2,1,0】代表的是0出现1次,1出现俩次,2出现1次,3出现0次,𝑎0=1,𝑎1=2,𝑎2=1,𝑎3=0。给出一个数字,如果无法输出美丽的数组,就输出-1。
代码:
#include<iostream>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
if(n == 1 || n == 2 || n == 3 || n == 6) {
cout << "-1" << endl;
}
else if(n == 4) {
cout << "1 2 1 0" << endl;
}
else if(n == 5) {
cout << "2 1 2 0 0" << endl;
}
else {
cout << n - 4 << ' ' << "2 1 ";
for(int i = 3; i < n; i++) {
if(i == n -4) {
cout << "1 ";
}
else {
cout << "0 ";
}
}
}
return 0;
}
三、总结
比赛有点多,好几周没写周报了好像,慢慢补题ing。