<>第十四届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组

. T)

<>试题 A: 日期统计

dfs+剪枝即可，因为前四位一定是2023，五六位一定是1-12月，七八为也要满足条件，所以实际上搜索的范围比较少

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; int a[N]; int mon[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; vector<
int> now; map<int,int> mp; int ans = 0; void dfs(int len,int dep){ if(len == 8){
int sum = 0; int m = now[4]*10 + now[5]; int d = now[6]*10 + now[7]; if(m < 1 ||
m> 12) return ; if(d < 1 || d > mon[m]) return ; for(int i = 0;i < 8;i++){ sum
*= 10; sum += now[i]; } if(!mp[sum]){ ans++; mp[sum] = 1; } return ; } for(int i
= dep;i <= 100;i++){ if(len == 0){ if(a[i] == 2){ now.push_back(a[i]); dfs(len+1
,i+1); now.pop_back(); } }else if(len == 1){ if(a[i] == 0){ now.push_back(a[i]);
dfs(len+1,i+1); now.pop_back(); } }else if(len == 2){ if(a[i] == 2){ now.
push_back(a[i]); dfs(len+1,i+1); now.pop_back(); } }else if(len == 3){ if(a[i]
== 3){ now.push_back(a[i]); dfs(len+1,i+1); now.pop_back(); } }else{ now.
push_back(a[i]); dfs(len+1,i+1); now.pop_back(); } } } void sol(){ for(int i = 1
;i <= 100;i++){ scanf("%d",&a[i]); } dfs(0,1); printf("%d",ans); } int main(){
sol(); return 0; }
<>试题 B: 01 串的熵

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; void sol(){ int n = 23333333; for(int i = 0;i <= n/2;i++){ double ans =
-1.0*i*i/n*log2(1.0*i/n)-1.0*(n-i)*(n-i)/n*log2(1.0*(n-i)/n); if(abs(ans -
11625907.5798) <= 0.0001){ printf("%d",i); } } } int main(){ sol(); return 0; }
<>试题 C: 冶炼金属

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; int main(){ int n; scanf("%d",&n); int maxx = 1000000000,minn = 0; for(
int i = 1;i <= n;i++){ int a,b; scanf("%d %d",&a,&b); maxx = min(maxx,a/b); minn
= max(minn,(a+b+1)/(b+1));//比赛时手快分子少加了个1，麻了(代码已改过) } printf("%d %d",minn,maxx);
return 0; }
<>试题 D: 飞机降落

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; struct node{ int t,d,l; }p[N]; int a[20]; void sol(){ int n; scanf("%d"
,&n); for(int i = 1;i <= n;i++){ scanf("%d %d %d",&p[i].t,&p[i].d,&p[i].l); a[i]
= i; } do{ int now = 0; int flag = 1; for(int i = 1;i <= n;i++){ int t = p[a[i]]
.t,d = p[a[i]].d,l = p[a[i]].l; if(now > t + d){ flag = 0; break; }else{ if(t >
now) now = t + l; else now = now + l; } } if(flag){ printf("YES\n"); return; } }
while(next_permutation(a+1,a+1+n)); printf("NO\n"); } int main(){ int t; scanf(
"%d",&t); while(t--){ sol(); } return 0; }
<>试题 E: 接龙数列

dp,dp[i]表示以i为结尾最长可以组成多长，那么答案就是n-max(dp[0-9])

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; struct node{ int t,w; }p[N]; int dp[20]; void sol(){ int n; scanf("%d",
&n); for(int i = 1;i <= n;i++){ int x; scanf("%d",&x); p[i].w = x % 10; while(x
>= 10){ x/=10; } p[i].t = x; } // for(int i = 1;i <= n;i++){ // printf("%d
%d\n",p[i].t,p[i].w); // } for(int i = 1;i <= n;i++){ dp[p[i].w] = max(dp[p[i].w
],dp[p[i].t] + 1); } int ans = 1000000000; for(int i = 0;i <= 9;i++){ ans = min(
ans,n-dp[i]); } printf("%d",ans); } int main(){ sol(); return 0; }
<>试题 F: 岛屿个数

#include<bits/stdc++.h> using namespace std; const int N = 2e3+9; typedef long
long ll; int a[59][59]; int vis[59][59]; int dx[] = {0,0,1,-1}; int dy[] = {1,-1
,0,0}; int fobid = 0; int n,m; int ans[N]; int nc = 0; void dfs(int x,int y,int
co){ vis[x][y] = co; for(int i = 0;i <= 3;i++){ int xx = x + dx[i]; int yy = y +
dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > m) continue; if(vis[xx][yy]||a[xx]
[yy] == 0){ continue; } dfs(xx,yy,co); } } int v[59][59]; void dfs2(int x,int y,
int co){ v[x][y] = 1; for(int i = 0;i <= 3;i++){ int xx = x + dx[i]; int yy = y
+ dy[i]; if(xx < 1 || xx > n || yy < 1 || yy > m){ ans[co] = 1; continue; } if(v
[xx][yy] || vis[xx][yy] == fobid){ continue; } dfs2(xx,yy,co); } } void sol(){
memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); nc = 0; scanf("%d %d",&n,&
m); for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ scanf("%1d",&a[i][j]);
} } for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ if(a[i][j]){ if(!vis[i
][j]){ dfs(i,j,++nc); } } } } for(fobid = 1;fobid <= nc;fobid++){ for(int i = 1;
i<= n;i++){ for(int j = 1;j <= m;j++){ if(vis[i][j] && fobid != vis[i][j]){ if(
ans[vis[i][j]]) continue; memset(v,0,sizeof(v)); dfs2(i,j,vis[i][j]); } } } }
int num = 0; for(int i = 1;i <= nc;i++){ num += ans[i]; } printf("%d\n",num); }
int main(){ int t; scanf("%d",&t); while(t--) sol(); return 0; }
<>试题 G: 子串简写

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; char s[N]; char a,b; ll sum[N]; void sol(){ int k,n; scanf("%d",&k);
scanf("%s",s+1); n = strlen(s+1); scanf(" %c",&a); scanf(" %c",&b); for(int i =
1;i <= n;i++){ if(s[i] == b) sum[i] = sum[i-1]+1; else sum[i] = sum[i-1]; } ll
ans= 0; for(int i = 1;i <= n;i++){ if(s[i] == a && i + k - 2 <= n){ ans += (sum[
n] - sum[i+k-2]); } } printf("%lld",ans); } int main(){ sol(); return 0; }
<>试题 H: 整数删除

#include<bits/stdc++.h> using namespace std; const int N = 2e6+9; typedef long
long ll; struct nodevp{ int pos; ll val; bool operator < (const nodevp &p) const
{ if(val == p.val) return pos < p.pos; else return val < p.val; } bool operator
== (const nodevp &p) const{ return pos == p.pos && val == p.val; } }; struct
nodepv{ int pos; ll val; bool operator < (const nodepv &p) const{ if(pos == p.
pos) return val < p.val; else return pos < p.pos; } bool operator == (const
nodepv&p) const{ return pos == p.pos && val == p.val; } }; ll a[N]; set<nodevp>
svp; set<nodepv> spv; int n,k; void changepv(nodepv a,nodepv b){ spv.erase(a);
spv.insert(b); } void changevp(nodevp a,nodevp b){ svp.erase(a); svp.insert(b);
} void sol(){ scanf("%d %d",&n,&k); for(int i = 1;i <= n;i++){ scanf("%d",&a[i])
; nodevp v; v.val = a[i]; v.pos = i; svp.insert(v); nodepv p; p.val = a[i]; p.
pos= i; spv.insert(p); } for(int i = 1;i <= k;i++){ nodevp v = *svp.begin(); //
svp.erase(v); ll add = v.val; nodepv p; p.val = v.val; p.pos = v.pos; auto apv =
spv.find(p); if(apv != spv.begin()){ apv--; nodepv temp,nex; temp.pos = apv->
pos; temp.val = apv->val; nex.pos = temp.pos; nex.val = temp.val + add; nodevp
temp2,nex2; temp2.pos = apv->pos; temp2.val = apv->val; nex2.pos = temp2.pos;
nex2.val = temp2.val + add; changepv(temp,nex); changevp(temp2,nex2); // apv++;
} apv = spv.find(p); apv++; if(apv == spv.end()){ spv.erase(p); svp.erase(v);
continue; } nodepv temp,nex; temp.pos = apv->pos; temp.val = apv->val; nex.pos =
temp.pos; nex.val = temp.val + add; nodevp temp2,nex2; temp2.pos = apv->pos;
temp2.val = apv->val; nex2.pos = temp2.pos; nex2.val = temp2.val + add; changepv
(temp,nex); changevp(temp2,nex2); spv.erase(p); svp.erase(v); } while(spv.size()
){ nodepv p = *spv.begin(); printf("%lld ",p.val); spv.erase(p); } } int main(){
sol(); return 0; } // 5 3 // 4 1 2 8 7 // 5 4 // 1 4 2 8 7
<>试题 I: 景区导游

asum - dis(a[i],a[i+1]) - dis(a[i],a[i-1]) + dis(a[i-1],a[i+1]),删掉开头和结尾特判，入代码所示。

#include<bits/stdc++.h> using namespace std; const int N = 2e5+9; typedef long
long ll; int n,k; int f[N][29]; int dep[N]; int a[N]; ll sum[N]; ll ans[N];
struct edge{ ll dis; int to; }; vector<edge> g[N]; int lca(int u,int v){ if(dep[
u] < dep[v]) swap(u,v); for(int i = 20;i >= 0;i--){ if(dep[f[u][i]] >= dep[v]) u
= f[u][i]; } if(u == v) return u; for(int i = 20;i >= 0;i--){ if(f[u][i] != f[v]
[i]){ u = f[u][i]; v = f[v][i]; } } return f[u][0]; } ll dis(int u,int v){
return (sum[u] + sum[v] - sum[lca(u,v)]*2); } void dfs(int now,int fa){ //
sum[now] = sum[now] + sum[fa]; f[now][0] = fa; dep[now] = dep[fa] + 1; for(int i
= 1;i <= 20;i++){ if(f[now][i-1]) f[now][i] = f[f[now][i-1]][i-1]; else break; }
for(auto j : g[now]){ if(j.to == fa) continue; sum[j.to] += sum[now] + j.dis;
dfs(j.to,now); } } void sol(){ scanf("%d %d",&n,&k); for(int i = 1;i <= n-1;i++)
{ int u,v; ll dis; scanf("%d %d %lld",&u,&v,&dis); edge p; p.dis = dis;p.to = v;
g[u].push_back(p); p.to = u; g[v].push_back(p); } dfs(1,0); ll asum = 0; for(
int i = 1;i <= k;i++){ scanf("%d",&a[i]); } for(int i = 2;i <= k;i++){ asum += (
sum[a[i-1]] + sum[a[i]] - sum[lca(a[i-1],a[i])]*2); } for(int i = 1;i <= k;i++){
if(i == 1){ ans[i] = asum - dis(a[i],a[i+1]); }else if(i == k){ ans[i] = asum -
dis(a[i],a[i-1]); }else{ ans[i] = asum - dis(a[i],a[i+1]) - dis(a[i],a[i-1]) +
dis(a[i-1],a[i+1]); } } for(int i = 1;i <= k;i++){ printf("%lld ",ans[i]); } }
int main(){ sol(); return 0; }
<>试题 J: 砍树

#include<bits/stdc++.h> using namespace std; const int N = 2e5+9; typedef long
long ll; int n,m; int f[N][29]; int dep[N]; ll sum[N]; int ans = -1; int fnum[N]
; struct edge{ ll dis; int to; }; vector<edge> g[N]; int lca(int u,int v){ if(
dep[u] < dep[v]) swap(u,v); for(int i = 20;i >= 0;i--){ if(dep[f[u][i]] >= dep[v
]) u = f[u][i]; } if(u == v) return u; for(int i = 20;i >= 0;i--){ if(f[u][i] !=
f[v][i]){ u = f[u][i]; v = f[v][i]; } } return f[u][0]; } void dfs(int now,int
fa){ // sum[now] = sum[now] + sum[fa]; f[now][0] = fa; dep[now] = dep[fa] + 1;
for(int i = 1;i <= 20;i++){ if(f[now][i-1]) f[now][i] = f[f[now][i-1]][i-1];
else break; } for(auto j : g[now]){ if(j.to == fa) continue; fnum[j.to] = j.dis;
dfs(j.to,now); } } void dfs2(int now,int fa){ for(auto j : g[now]){ if(j.to ==
fa) continue; dfs2(j.to,now); sum[now] += sum[j.to]; } if(sum[now] == m){ ans =
max(ans,fnum[now]); } } void sol(){ scanf("%d %d",&n,&m); for(int i = 1;i <= n-1
;i++){ int u,v; scanf("%d %d",&u,&v); edge p; p.dis = i;p.to = v; g[u].push_back
(p); p.to = u; g[v].push_back(p); } dfs(1,0); for(int i = 1;i <= m;i++){ int u,v
; scanf("%d %d",&u,&v); sum[u]++; sum[v]++; sum[lca(u,v)]-=2; } dfs2(1,0);
printf("%d",ans); } int main(){ sol(); return 0; }

GitHub

Gitee