2023第十四届蓝桥杯:
A 日期统计(填空题)
蓝桥杯经典的日期题。
巨巨巨巨巨ex的臭模拟,我会的只有一种八层循环做法,赛时没想到还准备自己亲手动手查数(
但实际上我现在用的编译器跑八层循环跑不出来,所以只能拆成两部分;
第一部分找符合2023年的,然后记录下最后一位3的位置就可以。
后续跟着3的位置继续找剩下四位就可以。
然后剩下四位最好的方式是:
直接分别求出月份和日期,将月份限制在1~12,将日期限定在1~31(对应月份的最大天数)
而不是像我一样忽略了两个0组在一起的可能
我个人觉得set最方便,这样不容易记录空;
看别人还有一种dfs的写法,但我感觉那个跟八层循环的复杂度差不多,我机子也跑不动,就不试了。
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
struct d
{
int a, b, c, d;
};
void solve()
{
// 1.八层循环?
std::vector<int> a(1010);
for (int i = 0; i < 100; i++)
{
std::cin >> a[i];
}
std::set<PII> s;
std::vector<d> ds;
for (int a1 = 0; a1 < 100; a1++)
{
for (int a2 = a1 + 1; a2 < 100; a2++)
{
for (int a3 = a2 + 1; a3 < 100; a3++)
{
for (int a4 = a3 + 1; a4 < 100; a4++)
{
if (a[a1] != 2 || a[a2] != 0 || a[a3] != 2 || a[a4] != 3)
continue;
ds.push_back({a1, a2, a3, a4});
}
}
}
}
for (auto p : ds)
{
int a4 = p.d;
for (int a5 = a4 + 1; a5 < 100; a5++)
{
for (int a6 = a5 + 1; a6 < 100; a6++)
{
for (int a7 = a6 + 1; a7 < 100; a7++)
{
for (int a8 = a7 + 1; a8 < 100; a8++)
{
int month = a[a5] * 10 + a[a6];
int day = a[a7] * 10 + a[a8];
if (month > 12 || day > 31)
continue;
if (month <= 0 || day <= 0)
continue;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
s.insert({month, day});
continue;
}
else if (month == 2)
{
if (day > 29)
continue;
s.insert({month, day});
}
else
{
if (day > 30)
continue;
s.insert({month, day});
}
// std::cout << month << " " << day << "\n";
}
}
}
}
}
std::cout << s.size() << "\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
B 01 串的熵(填空题)
这种填空题还不如编程题,编程题起码还能骗个分(
今年如果还这么ex建议先别做,做不出来真的很搞心态
而且去年我连log函数怎么写都不知道,公式推出来都没用
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void solve()
{
// 设a为0出现的次数,b为1出现的次数
// a<b
// p(0)=p0=(a/n)
// p(1)=p1=(b/n)
// H=a*(-p0*log2p0)+b*(-p1*log2p1);
double n = 23333333;
int n0 = n / 2, n1 = 0;
double H = 0;
while (1)
{
n1 = n - n0;
H = -n0 * (n0 / n) * log2(n0 / n) - n1 * (n1 / n) * log2(n1 / n);
if (H > 11625907.5797 && H < 11625907.5799)
break;
n0--;
}
printf("%d", n0);
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
C 冶炼金属(模拟/思维/二分/贪心)
思路:
这道题别人说用二分,但我更愿意用贪心的思想去考虑;
首先对于每种情况,会给出一个l,r,即耗费物品数和产生的金属数;
那么对于最小效率,直接用l整除r即可;
而对于最大效率,假设l原本可以造出(r+1)个金属,那么给l/(r+1)再加上1,它就造不出r+1个金属了,这样就能保证效率最大,答案最优。
同时为了满足所有情况,最小效率要取最大,最大效率要取最小。
代码:
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void solve()
{
int n;
std::cin >> n;
int mi = 0, mx = 1e9;
for (int i = 0; i < n; i++)
{
int l, r;
std::cin >> l >> r;
mi = std::max(mi, l / (r + 1) + 1LL);
mx = std ::min(mx, l / r);
}
std::cout << mi << " " << mx << "\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
思路:
数据较小,可以用dfs遍历所有飞机的所有降落顺序,只要保证每个飞机都能降落即可;
同时,每架飞机有三个参数:能开始降落的时间l,能逗留的时间r,以及降落耗费的时间;
定义u为当前降落了几架飞机,s为之前的所有飞机降落完到现在的时间是多少;
那么目标状态就是n架飞机全部降落完毕;
以及一架飞机能否降落,主要取决于它开始降落的时间+逗留的时间是否>=现在的时间;
同时根据贪心,我们在更新s时,要保证给之后的飞机留的时间要足够久,所以要找l和s的最大值,以便飞机能够卡点降落,不浪费时间。
代码:
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
struct love
{
int l, r, w;
};
int n;
std::vector<love> a;
std::vector<bool> st;
bool dfs(int u, int s)
{
//std::cout<<u<<" "<<n<<"\n";
//std::cout<<s<<"\n";
if (u == n)
{
//std::cout<<s<<"\n";
return true;
}
for (int i = 0; i < n; i++)
{
//std::cout << u << " " << s << "\n";
if (!st[i] && a[i].r + a[i].l >= s)
{
int pos = std::max(a[i].l, s) + a[i].w;
st[i] = true;
if (dfs(u + 1, pos))
return true;
st[i] = false;
}
//std::cout << "\n";
}
return false;
}
void solve()
{
std::cin >> n;
a.resize(n);
st.resize(n);
//std::cout<<n<<"\n";
for (int i = 0; i < n; i++)
{
st[i]=false;
int l, r, w;
std::cin >> l >> r >> w;
a[i] = {l, r, w};
}
if (dfs(0, 0))
std::cout << "YES\n";
else
std::cout << "NO\n";
return ;
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> t;
while (t--)
solve();
return 0;
}
E 接龙序列(DP)
思路:
本人也不太会DP,偶然看到过这个思路。
首先对于每个Ai,有用的只有首末两位数,因此只需存这两位数即可。
题目的意思要保留最长的子序列,也就是说求出最长子序列的长度,用n减去这个长度即可。
然后这种最长子序列,一般跟dp都牵扯比较深。
设每个Ai的首位为x,末位为y;
经过观察,可以发现每个末位为y的Ai只会影响Ai+1及其之后的所有x=y的数;
换句话说,首位为x的Ai,只会继承之前的y=x的Ai的长度;
那么,就可以通过维护以0~9为结尾的几种情况的最长长度即可。
设f[i][j]表示到第i个数为止,以j为结尾的序列的长度最长是多少。
转移方程即为f[i][a[i][1]]=f[i][a[i][0]]+1;
同时注意边界,注意维护其他f[i][j]中j不为a[i][1]的情况即可。
代码:
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void solve()
{
int n;
std::cin >> n;
std::vector<std::vector<int>> a(n, std::vector<int>(2, 0LL));
std::vector<std::vector<int>> f(n+10,std::vector<int>(10,0LL));
for (int i = 0; i < n; i++)
{
std::string s;
std::cin >> s;
int l = s.size() - 1;
a[i][0] = s[0] - '0';
a[i][1] = s[l] - '0';
// std::cout << a[i][1] << "\n";
}
for (int i = 0; i < n; i++)
{
int x = a[i][0], y = a[i][1];
if (i == 0)
{
f[i][y]++;
continue;
}
else
f[i][y] = f[i - 1][x] + 1;
for (int j = 0; j < 10; j++)
{
f[i][j] = std::max(f[i - 1][j], f[i][j]);
//std::cout << f[i][j] << " ";
}
//std::cout << "\n";
}
int mx = 0;
for (int i = 0; i < 10; i++)
{
mx = std::max(mx, f[n - 1][i]);
}
std::cout << n - mx << "\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
思路:如果一个岛屿没有被完全另一个岛屿围起来,那么它就是独立的一个个体。
用海水向八个方向搜索,找到一个‘1’后,岛屿个数记1并且将所连‘1’全部标记;
这样岛屿必定搜索不到在其他岛屿内部的岛,搜到的必然能计数。
代码:
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dx2[8] = {0, 1, 0, -1, 1, 1, -1, -1}, dy2[8] = {1, 0, -1, 0, 1, -1, 1, -1};
int n, m;
char g[55][55];
bool st[55][55];
int ans;
void bfs2(int i, int j)
{
std::queue<PII> q;
q.push({i, j});
st[i][j] = true;
while (!q.empty())
{
PII s = q.front();
q.pop();
int x = s.first, y = s.second;
for (int i = 0; i < 8; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 1 && ny <= m && nx <= n && ny >= 1 && !st[nx][ny] && g[nx][ny] == '1')
{
q.push({nx, ny});
st[nx][ny] = true;
}
}
}
}
void bfs1()
{
std::queue<PII> q;
q.push({0, 0});
st[0][0] = true;
while (!q.empty())
{
PII s = q.front();
q.pop();
int x = s.first, y = s.second;
for (int i = 0; i < 8; i++)
{
int nx = x + dx2[i], ny = y + dy2[i];
if (nx >= 0 && ny <= m + 1 && nx <= n + 1 && ny >= 0 && !st[nx][ny])
{
if (g[nx][ny] == '1')
{
ans++;
// std::cout << nx << " " << ny << "\n";
bfs2(nx, ny);
}
else
{
q.push({nx, ny});
st[nx][ny] = true;
}
}
}
}
}
void solve()
{
ans = 0;
std::cin >> n >> m;
for (int i = 0; i <= n + 1; i++)
{
for (int j = 0; j <= m + 1; j++)
{
g[i][j] = '0';
st[i][j] = false;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
std::cin >> g[i][j];
}
}
bfs1();
std::cout << ans << "\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> t;
while (t--)
solve();
return 0;
}
思路:
有个特殊的就是c1和c2相等的情况容易忽略
其余用二分求解即可
思路就是找首位两个字符在数组中的位置,符合条件的对数有多少个
有一点双指针的思想
ps:手写二分还是不会,只能用lower_bound凑合了
代码:
#include <bits/stdc++.h>
typedef std::pair<int, int> PII;
#define int long long
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
void solve()
{
int k;
std::cin >> k;
std::string s;
std::cin >> s;
char c1, c2;
std::cin >> c1 >> c2;
int l = s.size();
int ans = 0;
if (c1 == c2)
{
std::vector<int> a;
for (int i = 0; i < l; i++)
{
if (s[i] == c1)
a.push_back(i);
}
int la = a.size();
for (int i = 0; i < la; i++)
{
int x = a[i] + k - 1;
int pos = std::lower_bound(a.begin(), a.end(), x) - a.begin();
ans += la - pos;
// std::cout << la << " " << pos << "\n";
// std::cout << ans << "\n";
}
std::cout << ans << "\n";
return;
}
std::vector<int> a, b;
for (int i = 0; i < l; i++)
{
if (s[i] == c1)
a.push_back(i);
else if (s[i] == c2)
b.push_back(i);
}
int la = a.size(), lb = b.size();
/*
0 2 4
0 1 2
1 3 5 7
0 1 2 3
*/
// std::cout << la << " " << lb << "\n";
for (int i = 0; i < la; i++)
{
int x = a[i] + k - 1;
int pos = std::lower_bound(b.begin(), b.end(), x) - b.begin();
// std::cout << pos << "\n";
ans += lb - pos;
// std::cout << ans << "\n";
}
std::cout << ans << "\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
思路:
代码:
#include <bits/stdc++.h>
#define int long long
typedef std::pair<int, int> PII;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dx2[8] = {0, 1, 0, -1, 1, 1, -1, -1}, dy2[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int N = 5e5 + 10;
int l[N], r[N], w[N];
bool st[N];
void del(int x)
{
r[l[x]] = r[x];
l[r[x]] = l[x];
w[l[x]] += w[x];
w[r[x]] += w[x];
}
void solve()
{
int n, m;
std::cin >> n >> m;
std::priority_queue<PII, std::vector<PII>, std::greater<PII>> q;
r[0] = 1, l[n + 1] = n;
for (int i = 1; i <= n; i++)
{
std::cin >> w[i];
l[i] = i - 1;
r[i] = i + 1;
q.push({w[i], i});
}
while (m--)
{
PII t = q.top();
q.pop();
int s = t.first, i = t.second;
if (s != w[i])//如果这个数被修改过,将其在优先队列里的状态修改
q.push({w[i], i}), m++;
else//否则直接删除即可
del(i);
}
for (int i = r[0]; i != n + 1; i = r[i])
std::cout << w[i] << " ";
// std::cout<<"\n";
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}
思路:
代码:
(此代码为95分,剩下的不知道错哪了,找半天找不出来)
#include <bits/stdc++.h>
#define int long long
typedef std::pair<int, int> PII;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
const int dx2[8] = {0, 1, 0, -1, 1, 1, -1, -1}, dy2[8] = {1, 0, -1, 0, 1, -1, 1, -1};
const int N = 3e5 + 10;
int n, m;
int h[N], e[N], ne[N], idx; // 建图数组
int w[N]; // 边的权值数组
int s[N][25], fa[N][25]; // 前缀和数组、父亲数组
int d[N]; // 深度数组
int a[N];
void add(int x, int y, int z)
{
e[idx] = y;
w[idx] = z;
ne[idx] = h[x];
h[x] = idx++;
}
void dfs(int x, int ff, int dis, int hh) // 初始节点、父亲结点、传递参数、当前深度
{
d[x] = hh;
fa[x][0] = ff;
s[x][0] = dis;
for (int i = 1; i <= 23; i++)
{
s[x][i] = s[x][i - 1] + s[fa[x][i - 1]][i - 1];
fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
for (int i = h[x]; i != -1; i = ne[i])
{
int j = e[i];
if (j == ff)
continue;
dfs(j, x, w[i], hh + 1);
}
}
int lca(int u, int v)
{
if (d[u] < d[v])
std::swap(u, v);
int ans = 0;
for (int i = 23; i >= 0; i--)
{
if (d[fa[u][i]] >= d[v])
{
ans += s[u][i];
u = fa[u][i];
}
}
if (u == v)
return ans;
for (int i = 23; i >= 0; i--)
{
if (fa[u][i] != fa[v][i])
{
ans += s[u][i] + s[v][i];
u = fa[u][i];
v = fa[v][i];
}
}
ans += s[u][0] + s[v][0];
return ans;
}
void solve()
{
std::cin >> n >> m;
for (int i = 1; i <= n; i++)
h[i] = -1;
for (int i = 1; i < n; i++)
{
int u, v, z;
std::cin >> u >> v >> z;
add(u, v, z);
add(v, u, z);
}
dfs(1, 0, 0, 1);
int ans = 0;
for (int i = 1; i <= m; i++)
{
std::cin >> a[i];
if (i > 1)
ans += lca(a[i], a[i - 1]);
}
for (int i = 1; i <= m; i++)
{
if (i == 1)
std::cout << ans - lca(a[1], a[2]) << " ";
else if (i == n)
std::cout << ans - lca(a[m], a[m - 1]) << " ";
else
std::cout << ans - lca(a[i], a[i - 1]) - lca(a[i], a[i + 1]) + lca(a[i - 1], a[i + 1]) << " ";
}
return;
}
signed main()
{
int t = 1;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// std::cin >> t;
while (t--)
solve();
return 0;
}