第十五届蓝桥杯 C++A组

题目清单链接(各届真题在此链接页面最底部):备赛蓝桥杯 - 蓝桥云课

1.五子棋对弈

link:0五子棋对弈 - 蓝桥云课

code

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll ans = 0, a[10][10], cnt0 = 0, cnt1 = 0;// 0白

bool check()
{
  if(cnt0 != 13 || cnt1 != 12)return false;
  for(int i = 0; i < 5; i++) 
    if(a[i][0] == a[i][1] && a[i][1] == a[i][2] && a[i][2] == a[i][3] && a[i][3] == a[i][4]) return false;
  for(int j = 0; j < 5; j++)
    if(a[0][j] == a[1][j] && a[1][j] == a[2][j] && a[2][j] == a[3][j] && a[3][j] == a[4][j]) return false;
  if(a[0][0] == a[1][1] && a[1][1] == a[2][2] && a[2][2] == a[3][3] && a[3][3] == a[4][4]) return false;
  if(a[0][4] == a[1][3] && a[1][3] == a[2][2] && a[2][2] == a[3][1] && a[3][1] == a[4][0]) return false;
  return true;
}

void dfs(ll dep)
{
  if(dep == 25)
  {
    if(check()) ans++;
    return;
  }
  ll x = dep / 5, y = dep % 5;
  a[x][y] = 0; cnt0++;
  dfs(dep + 1);cnt0--;
  a[x][y] = 1;cnt1++;
  dfs(dep + 1);cnt1--;
}

int main()
{
  dfs(0);
  cout<<ans<<endl;
  return 0;
}

2.艺术与篮球

link:P10385 [蓝桥杯 2024 省 A] 艺术与篮球 - 洛谷

code

复制代码
#include <bits/stdc++.h> 
using namespace std;
using ll = long long;

ll y = 2000, m = 1, d = 1;

ll fc[] = {13, 1, 2, 3, 5, 4, 4, 2, 2, 2};
ll mp[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool isLeap()
{
	return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}

ll Day()
{
	if(!isLeap() || m!=2) return mp[m];
	return 29;
}

bool check()
{
	ll ret = 0;
	ret += fc[y%10] + fc[(y%100)/10] + fc[(y%1000)/100] + fc[(y/1000)];
	ret += fc[m/10] + fc[m%10];
	ret += fc[d/10] + fc[d%10];
	
	return ret > 50;
}

int main()
{
	// 2024 年 4 月 13 日
	ll ans = 0;
	while(!(y==2024 && m==4 && d==14))
	{
		if(check()) ans++;
		d++;
		if(d > Day())
		{
			d = 1;
			m++;
			if(m > 12)
			{
				m=1;
				y++;
			}
		}
	}
	cout<<ans;
	return 0;
}

3.训练士兵

link:0训练士兵 - 蓝桥云课

code

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 10;
ll p[MAXN], c[MAXN], n, s, INF = 0x3f3f3f3f3f3f3f3f;

struct node{
  ll p;
  ll c;
} a[MAXN];

ll fc(ll t)
{
  ll ret = t * s;
  for(int i = 1; i <= n; i++)
  {
    ret += a[i].p * (max(t, a[i].c) - t);
  }
  return ret;
}

int main()
{
  cin>>n>>s;
  ll maxc = 0;
  for(int i = 1; i <= n; i++) 
  {
    cin>>a[i].p>>a[i].c;
    maxc = max(maxc, a[i].c);
  }
  ll ans = INF;
  for(int t = 0; t <= maxc; t++) ans = min(ans, fc(t));
  cout<<ans<<endl;
  return 0;
}

4.团建

link:

code

复制代码
#include <bits/stdc++.h>
#define N 200005
using namespace std;
map<int,vector<int>> m1,m2;
int n,m,a[N],b[N],ans,u,v;
void dfs(int x,int y,int count)
{
  if(a[x]!=b[y])return;
  ans=max(ans,count+1);
  for(int i=0;i<m1[x].size();i++)
   for(int j=0;j<m2[y].size();j++)
   {int a1=m1[x][i];int b1=m2[y][j];
   dfs(a1,b1,count+1);
   }
}
int main()
{
  cin>>n>>m;
  for(int i=1;i<=n;i++)cin>>a[i];
  for(int i=1;i<=m;i++)cin>>b[i];
  for(int i=1;i<=n-1;i++)
  {
    cin>>u>>v;m1[u].push_back(v);
  }
  for(int i=1;i<=m-1;i++)
  {
    cin>>u>>v;m2[u].push_back(v);
  }
  dfs(1,1,0);
cout<<ans;
}

5.成绩统计

link:0成绩统计 - 蓝桥云课

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 10;
ll n, k, T, a[MAXN], b[MAXN], ans = -1;
ll s[MAXN], sq[MAXN];

bool check(ll x)
{
  memcpy(b+1, a+1, sizeof(ll) * x);
  sort(b + 1, b + 1 + x);
  // init s[], sq[]
  for(int i = 1; i <= x; i++)
  {
    s[i] = s[i-1] + b[i];
    sq[i] =  sq[i-1] + b[i] * b[i];
  }
  // check
  for(int i = k; i <= x; i++)
  {
    double avg = 1.0 * (s[i] - s[i-k]) / k;
    if(sq[i] - sq[i-k] - k * avg * avg < k*T)  return true;
  }
  return false;
}

void solve(ll l, ll r)
{
  while(l <= r)
  {
    ll m = (l + r) / 2;
    if(check(m)) 
    {
      ans = m;
      r = m - 1;
    }
    else l = m + 1;
  }
}

int main()
{
  cin>>n>>k>>T;
  for(int i = 1; i <= n; i++)cin>>a[i];
  
  solve(k, n);
  cout<<ans<<endl;
  return 0;
}

6. 因数计算

link:0因数计数 - 蓝桥云课

code(20%AC)

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 10;
ll n, a[MAXN];

int main()
{
  cin>>n;
  for(int i = 1; i <= n; i++)cin>>a[i];

  ll ans = 0;
  for(int i= 1; i <= n; i++)
    for(int j = 1; j <= n; j++) if (j == i) continue; else
      for(int k = 1; k <= n; k++) if(k == i || k == j) continue; else
        for(int l = 1; l <= n; l++) if(l == i || l == j || l == k) continue; else
          if(a[k] % a[i] == 0 && a[l] % a[j] == 0) ans++;

  cout<<ans<<endl;
  return 0;
}

7.零食采购

link:0零食采购 - 蓝桥云课

code

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 10;
ll prefix[MAXN][25], dep[MAXN], fa[MAXN][25], n, q, c[MAXN];
unordered_map<ll, vector<ll>> eg;

void dfs(ll x, ll f)
{
  // init prefix[][]
  for(int i = 1; i <= 20; i++)
  {
    prefix[x][i] += prefix[f][i];
  }
  prefix[x][c[x]]++;
  // dep & fa
  dep[x] = dep[f] + 1;
  fa[x][0] = f;
  for(int i = 1; i <= 20; i++)
  {
    fa[x][i] = fa[fa[x][i-1]][i-1];
  }
  for(int i = 0; i < eg[x].size(); i++)
  {
    if(eg[x][i] == f)continue;
    dfs(eg[x][i], x);
  }
}

ll lca(ll x, ll y)
{
  if(dep[x] < dep[y]) swap(x, y);
  for(int i = 20; i >= 0; i--) if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
  if(x == y) return x;
  for(int i = 20; i >= 0; i--)if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}

int main()
{
  cin>>n>>q;
  for(int i = 1; i <= n; i++) cin>>c[i];
  for(int i = 1; i <= n - 1; i++)
  {
    ll u, v; cin>>u>>v;
    eg[u].push_back(v);
    eg[v].push_back(u);
  }

  dfs(1, 0);

  while(q--)
  {
    ll s, t; cin>>s>>t;
    ll r = lca(s, t);
    ll ans = 0;
    for(int i = 1; i <= 20; i++)
    {
      if(prefix[s][i] + prefix[t][i] - 2 * prefix[r][i] + (c[r] == i ? 1 : 0) > 0) ans++;
    }
    cout<<ans<<endl;
  }
  return 0;
}

8.封印宝石

link:0封印宝石 - 蓝桥云课

code(暴力,60%AC)

复制代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAXN = 1e5 + 10;
ll n, k, a[MAXN], ans[MAXN];// b[i]: [i+1, n] 内最大魔法石的下标
bool vis[MAXN];


int main()
{
  // input
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin>>n>>k;
  for(int i = 1; i <= n; i++)cin>>a[i];

  for(int i = 1; i <= n; i++)
  {
    ll maxStone = -1, maxi = i;
    for(int j = i; j <= min(n, i + k); j++)
    {
      if(a[j] > maxStone && !vis[j] && a[j] != ans[i-1]) 
      {
        maxStone = a[j];
        maxi = j;
      }
    }
    if(maxStone == -1)
    {
      ans[i] = -1;
      continue;
    }
    k -= (maxi - i);
    vis[maxi] = true;
    ans[i] = maxStone;
  }
  for(int i = 1; i <= n; i++) cout<<ans[i]<<" ";
  return 0;
}
相关推荐
Rainy Blue8832 小时前
前缀和与差分(蓝桥杯高频考点)
数据结构·算法·蓝桥杯
-Excalibur-2 小时前
IP数据包在计算机网络传输的全过程
java·网络·c++·笔记·python·网络协议·智能路由器
历程里程碑2 小时前
41 .UDP -3 群聊功能实现:线程池助力多客户端通信
linux·开发语言·网络·数据结构·c++·网络协议·udp
山栀shanzhi2 小时前
【FFmpeg】是什么是未压缩的裸流?
c++·ffmpeg
不染尘.2 小时前
排序算法详解1
开发语言·数据结构·c++·算法·排序算法
啊我不会诶2 小时前
25CCPC东北邀请赛vp补题
c++·算法
plus4s2 小时前
3月20日(进阶11)
c++·算法
2301_816651222 小时前
移动语义在容器中的应用
开发语言·c++·算法