2023第十四届蓝桥杯C++B组题目解析

起因

24年蓝桥杯将至,把去年题复习一遍。emmm,其实是有一个很厉害的学妹说“要被圈钱杯把钱圈光了”。
现在只是初稿,如果有需要细写解析的可以在下面评论,欢迎留言。

日期统计

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 1e5 + 10 , INF = 1e9 + 7 ;
using namespace  std ;
int  n , m ;

int date[ 618 ] = {
        5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7,
        5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5, 8, 6, 1, 8, 3, 0, 3, 7, 9,
        2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3,
        8, 5, 1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6,
        1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1, 2, 8, 5, 0, 2, 5, 3, 3
};
int monthNum[ 13 ] = {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

void AC(){
    LL ans = 0;

    for (int month = 1 ; month <= 12 ; ++month) {
        for (int day = 1 ; day <= monthNum[ month ] ; ++day ) {
            int realdate[ 8 ] = {2, 0, 2, 3, month / 10, month % 10, day / 10, day % 10};
            int k = 0;

            for (int i = 0; i < 100; ++i) {
                if ( date[ i ] == realdate[ k ] ) {
                    ++k;
                    if ( k == 8 ) {
                        ans++;
                        break;
                    }
                }
            }
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    //cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

01串的熵

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 1e5 + 10 , INF = 1e9 + 7 ;
const double ans = 11625907.5798 , eps = 1e-2 ;
using namespace  std ;
int  n , m ;

void AC(){
    n = 23333333 ;
    for( int i = 0 ; i <= n / 2 ; i ++ ){ // i 对应0的个数
        double nn = n ; // 强转n的类型
        double sum = i * ( i / nn ) * log2( i / nn ) + ( nn - i )*( ( nn - i ) / nn ) * log2( ( nn - i ) / nn );
        sum = -sum ;
        //cout << "su m= " << sum << endl;
        if( abs( sum - ans ) <= eps ){
            cout << i << endl;
            return ;
        }
    }
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    //cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

冶炼金属

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 1e5 + 10 , INF = 1e9 + 7 ;
const double ans = 11625907.5798 , eps = 1e-2 ;
using namespace  std ;
int  n , m ;

void AC(){
    cin >> n ;
    int mx = INF , mn = 1 ;
    for( int i = 1 ; i <= n ; i++ ){
        int x , y ; cin >> x >> y ;
        mx = min( mx , x / y ) ;
        mn = max( mn , ( x / ( y + 1 ) ) + 1 ) ;
    }
    cout << mn << " " << mx << endl;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    //cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

飞机降落

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 1e5 + 10 , INF = 1e9 + 7 ;
const double ans = 11625907.5798 , eps = 1e-2 ;
using namespace  std ;
int  n , m ;
struct op{
    int st , ed , cont ;
}f[ 15 ];
bool vis[ 15 ] , fg ;

void dfs( int cnt , int now_time ){
    if( cnt == n || fg ){
        fg = true ;return ;
    }
    for( int i = 1 ; i <= n ; i++ ){
        if( !vis[ i ] && now_time <= f[ i ].ed ){
            vis[ i ] = true ;
            dfs( cnt + 1 , max( now_time , f[ i ].st  ) + f[ i ].cont ) ;
            vis[ i ] = false ;
        }
    }
}

void AC(){
    cin >> n ;
    for( int i = 1 ; i <= n ; i++ ){
        cin >> f[ i ].st >> f[ i ].ed >> f[ i ].cont ;
        f[ i ].ed += f[ i ].st ;
    }
    fg = false ;
    dfs(  0 , 0 ) ;
    if( fg ){
        cout << "YES\n";
    }else{
        cout << "NO\n";
    }
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

接龙序列

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 1e5 + 10 , INF = 1e9 + 7 ;
const double ans = 11625907.5798 , eps = 1e-2 ;
using namespace  std ;
int  n , m ;
int dp[ 15 ] ;
void AC(){
    cin >> n ;
    int ans = 0 ; string s ;
    for( int i = 1 ; i <= n ;i ++ ){
        cin >> s ;
        int pre = s[ 0 ] , hou = s[ (int)s.size() - 1 ] ;
        dp[ hou ] = max( dp[ hou ] , dp[ pre ] + 1 ) ;
        ans = max( ans , dp[ hou ] ) ;
    }
    cout << n - ans << endl ;
    return ;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    //cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

岛屿个数

这题不想写,暂时套用别人代码() 。

#include <bits/stdc++.h>
#define MAX 51
using namespace std;
int T,m,n;
int vis[MAX][MAX],used[MAX][MAX];
int dx[]={1,-1,0,0,1,1,-1,-1};
int dy[]={0,0,1,-1,1,-1,1,-1};
string mp[MAX];
void bfs_col(int x,int y){
    queue<int>qx,qy;
    qx.push(x);qy.push(y);vis[x][y]=1;
    while(!qx.empty()){
        x=qx.front();qx.pop();
        y=qy.front();qy.pop();
        for(int i=0;i<4;i++){
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx<0||ny<0||nx>m-1||ny>n-1||vis[nx][ny]==1||mp[x][y]=='0') continue;
            qx.push(nx);qy.push(ny);vis[nx][ny]=1;
        }
    }
}
bool bfs_out(int x,int y){
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            used[i][j]=0;
        }
    }
    queue<int>qx,qy;
    qx.push(x);qy.push(y);used[x][y]=1;
    while(!qx.empty()){
        x=qx.front();qx.pop();
        y=qy.front();qy.pop();
        if(x==0||x==m-1||y==0||y==n-1) return true;
        for(int i=0;i<8;i++){
            int nx=x+dx[i];
            int ny=y+dy[i];
            if(nx<0||ny<0||nx>m-1||ny>n-1||used[nx][ny]==1||mp[nx][ny]=='1') continue;
            qx.push(nx);qy.push(ny);used[nx][ny]=1;
        }
    }
    return false;
}
void solve(){
    int ans=0;
    cin>>m>>n;
    for(int i=0;i<m;i++){
        cin>>mp[i];  
        for(int j=0;j<n;j++){
            vis[i][j]=0;
        }
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(!vis[i][j]&&mp[i][j]=='1'){
                bfs_col(i,j);
                if(bfs_out(i,j)) ans++;
            }
        }
    }
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int T;
    cin>>T;
    while(T--)    solve();
    return 0;
}

子串简写

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 5e5+10;
int k;
int cntb[N]; 
string a;
char x,y;

int main()
{
	int cnta = 0;
	cin >> k;
	cin >> a >> x >> y;
	cntb[a.size()] = 0;
	for(int i = a.size()-1; i >= 0; i--)
	{
		cntb[i] = cntb[i+1];        //cntb[i]来统计包括下标i及之后给出字母y的个数
		if(a[i] == y)
		{
			cntb[i] ++;
		}
	}
	
	long long ans = 0;
	
	for(int i = 0; i < a.size(); i++)
	{
		if(a[i] == x && i+k-1 <= a.size()-1 )   //如果这个a[i]==x  那就看它i+k-1及后面有多少个y,及是cntb[i+k-1]。
		{
			ans += cntb[i+k-1];
		}                //还可以统计一下x的个数,如果没有x了,就可以直接跳出循环。
	}
	
	cout << ans <<endl;    //输出结果
	
	return 0;
}

整数删除

#include<bits/stdc++.h>
typedef  long long LL ;
const int N = 5e5 + 10 , INF = 1e9 + 7 ;
using namespace  std ;
LL  n , m , k , l[ N ] , r[ N ] , ans[ N ] , ad[ N ] ;
struct op{
    LL num , pos ;
    
    bool operator < ( const op& now )const{
        if( num == now.num ){
            return pos > now.pos ;
        }
        return num > now.num ;
    }
};
priority_queue< op > qu;

void AC(){
    cin >> n >> k ;
    for( int i = 1; i <= n ; i ++ ){
        int x ; cin >> x ;
        l[ i ] = i - 1 ; r[ i ] = i + 1 ;
        qu.push( { x , i } ) ;
    }
    while( (int)qu.size() > n - k ){
        auto [ x , y ] = qu.top() ; qu.pop() ;
        if( ad[ y ] ){
            qu.push( { x +ad[ y ] , y } ) ;
            ad[ y ] = 0 ;
        }else{
            LL nl = l[ y ] , nr = r[ y ] ;
            ad[ nl ] += x ; ad[ nr ] += x ;
            l[ nr ] = nl , r[ nl ] = nr ;
        }
    }
    while( (int)qu.size() ){
        auto[ x , y ] = qu.top() ; qu.pop() ;
        ans[ y ] = x + ad[ y ] ;
    }
    for( LL i = 1 ; i <= n ; i++ ){
        if( ans[ i ] ) cout << ans [ i ] << " " ;
    }
    return ;
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t  = 1 ;
    //cin >> t ;
    while( t-- ){
        AC();
    }
    return 0 ;
}

景区导游(挂)

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll N=1e5+9,P=1e9+7,inf=2e18;
ll n,k,u,v,t,a[N],dep[N],dist[N],fa[N][18],sum;
vector<pair<ll,ll>> g[N];

void dfs(ll x,ll p,ll d){
    fa[x][0]=p;dist[x]=d;
    for(int i=1;i<=17;++i){
        fa[x][i]=fa[fa[x][i-1]][i-1];
    }
    for(int i=0;i<=g[x].size()-1;++i){
        if(g[x][i].first==p)continue;
        dep[g[x][i].first]=dep[x]+1;
        dfs(g[x][i].first,x,d+g[x][i].second);
    }
}
ll lca(ll u,ll v){
    if(dep[u]<dep[v])swap(u,v);
    for(int i=17;i>=0;--i){
        if(dep[fa[u][i]]>=dep[v]) u=fa[u][i];
    }
    for(int i=17;i>=0;--i){
        if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
    }
    if(u==v)return u;
    return fa[u][0];
}
ll getd(ll u,ll v){
    return dist[u]+dist[v]-dist[lca(u,v)]*2;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n-1;++i){
        cin>>u>>v>>t;
        g[u].push_back({v,t});
        g[v].push_back({u,t});
    }
    dfs(1,0,0);
    for(int i=1;i<=k;++i)cin>>a[i];
    
    //for(int i=1;i<=n;++i){
    //    cout<<dep[i]<<' '<<dist[i]<<'\n';
    //}
    for(int i=1;i<=k-1;++i){
        sum+=getd(a[i],a[i+1]);
    }
    //cout<<sum;
    for(int i=1;i<=k;++i){
        ll _sum=sum;
        if(i!=k)_sum-=getd(a[i],a[i+1]);
        if(i!=1)_sum-=getd(a[i-1],a[i]);
        if(i!=1&&i!=k)_sum+=getd(a[i-1],a[i+1]);
        cout<<_sum<<' ';
    }

    return 0;
}

砍树(挂)

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;

typedef long long i64;
typedef array<int, 3> arr;
typedef pair<int, int> PII;

const int N = 1e5 + 10;

int n, m;
vector<PII> e[N];
int l[N], r[N], id[N];
int sz[N], hs[N], tot, top[N], dep[N], fa[N];
int cnt[N], s[N];

// 第一遍DFS,子树大小,重儿子,父亲,深度
void dfs1(int u,int f) {
    sz[u] = 1;
    hs[u] = -1;
    fa[u] = f;
    dep[u] = dep[f] + 1;
    for (auto [v, id] : e[u]) {
        if (v == f) continue;
        dfs1(v, u);
        sz[u] += sz[v];
        if (hs[u] == -1 || sz[v] > sz[hs[u]])
            hs[u] = v;
    }
}

// 第二遍DFS,每个点DFS序,重链上的链头的元素。
void dfs2(int u, int t) {
    top[u] = t;
    l[u] = ++tot;
    id[tot] = u;
    if (hs[u] != -1) {
        dfs2(hs[u], t);
    }
    for (auto [v, id] : e[u]) {
        if (v != fa[u] && v != hs[u]) {
            dfs2(v, v);
        }
    }
    r[u] = tot;
}

int LCA(int u, int v) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) v = fa[top[v]];
        else u = fa[top[u]];
    }
    if (dep[u] < dep[v]) return u;
    else return v;
}

void dfs_cal(int u, int f) {
    for (auto [v, id] : e[u]) if (v != f) {
        dfs_cal(v, u);
        cnt[id] = s[v];
        s[u] += s[v];
    }
}


signed main() {
    cin.tie(nullptr)->ios::sync_with_stdio(false);
    
    cin >> n >> m;
    for (int i = 1; i < n; i++) {
        int u, v; cin >> u >> v;
        e[u].push_back({v, i});
        e[v].push_back({u, i});
    }
    dfs1(1, 0);
    dfs2(1, 1);
    for (int i = 1; i <= m; i++) {
        int u, v; cin >> u >> v;
        int lca = LCA(u, v);
        s[u]++, s[v]++, s[lca] -= 2;
    }
    dfs_cal(1, 0);
    
    int ans = -1;
    for (int i = 1; i <= n - 1; i++) if (cnt[i] == m) {
        ans = max(i, ans);
    }
    
    cout << ans << '\n';

    return 0;    
}

end—

经验总结,注意数据范围,改开longlong就开,不会就暴力,不会就暴力。
希望大家不要收到“喜报”。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

粥旖跌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值