2026牛客寒假算法基础集训营1(B C E G K L)

比赛链接:牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

今天先写做出来的题解。(PS:个人思路,以官方题解为准)

严肃补题👹👹

B Card Game

思路:

B 题虽说简单吧,比赛时没做出来。

最大分数就是比小红最小值大的数字个数,然后把这部分排列一下 k ! k! k! 种然后剩下的又是 ( n − k ) ! (n-k)! (n−k)! 种,最后取乘积再取模。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int N=2e5+5;
const int MOD=998244353;
int a[N];
int f[N];
void init()
{
    f[0]=1;
    for(int i=1;i<N;i++)
    {
        f[i]=f[i-1]*i%MOD;
    }
}
void solve()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int m=1e9;
    int x;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        m=min(x,m);
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>m)cnt++;
    }
    cout<<f[cnt]*f[n-cnt]%MOD<<endl;
}
signed main(){
	IOS
    init();
	int t;
	cin>>t;
	//int t=1;
	while(t--)
	{
		solve();
	}
}

C Array Covering

思路:

可以发现总有办法把数组中最大的数覆盖到除数组两端的位置。所以找到最大值然后进行计算。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int N=5e5+5;
int a[N];
void solve()
{
    int n;
    cin>>n;
    int maxx=-1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        maxx=max(a[i],maxx);
    }
    cout<<maxx*(n-2)+a[1]+a[n]<<endl;
}
signed main(){
	IOS
	int t;
	cin>>t;
	//int t=1;
	while(t--)
	{
		solve();
	}
}

E Block Game

思路:

可以把第一个万能方块放进数组中,然后当作一个环去看,开始就是第一个位置和最后一个位置的和之后继续枚举。枚举间距为 n+1 的数之和找到最大的值。

这里需要注意的是,值可能是负值。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int N=2e5+5;
int a[N];
void solve()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    a[0]=k;
    int ans=-2e7;
    for(int i=0;i<=n;i++)
    {
        int last=(i+n)%(n+1);
        int sum=a[i]+a[last];
        ans=max(sum,ans);
    }
    cout<<ans<<endl;
}
signed main(){
	IOS
	int t;
	cin>>t;
	//int t=1;
	while(t--)
	{
		solve();
	}
}

G Digital Folding

思路:

数字折叠,先写一下数字翻转的写法,不建议用 string 的 reverse ()。拿个循环写就可以,每次取出最后一位,每次乘十也可以去除前导零。

然后就是考虑怎么找到最大的数字:可以先把右边的 r 翻转一下,然后考虑把 r 的最右侧位置都一一换成 9,要保证这个数字比 l 大。

刚开始的 r 肯定得比 10 大,不然翻不翻转没啥意义。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int revers(int x){
    int res=0;
    while(x>0)
    {
        res=res*10+x%10;
        x/=10;
    }
    return res;
}
void solve()
{
    int l,r;
    cin>>l>>r;
    int ans=revers(r);
    int m=10;
    while(r>=10)
    {
        int a=(r/m)*m-1;
        if(a>=l)ans=max(ans,revers(a));
        if(r/10<m)break;
        m*=10;
    }
    cout<<ans<<endl;
}
signed main(){
	IOS
	int t;
	cin>>t;
	//int t=1;
	while(t--)
	{
		solve();
	}
}

K Constructive

思路:

签到题,观察模拟一下可以发现只有 1 和 3 存在其他都是 NO。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
void solve()
{
    int n;
    cin>>n;
    if(n==1)cout<<"YES"<<endl<<"1"<<endl;
    else if(n==3)cout<<"YES"<<endl<<"1 2 3"<<endl;
    else cout<<"NO"<<endl;
}
signed main(){
	IOS
	int t;
	cin>>t;
	//int t=1;
	while(t--)
	{
		solve();
	}
}

L Need Zero

思路:

签到题,要让 n 在一次乘积运算后个位为 0,只需要考虑个位数。所以取出个位数,然后给写个数组就可以了。

个位本来就是 0 的乘 1 就可以了,其他的背一下乘法表就可以出。
代码:

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int a[]={1,10,5,10,5,2,5,10,5,10};
void solve()
{
    int n;
    cin>>n;
    int t=n%10;
    cout<<a[t];
}
signed main(){
	IOS
	//int t;
	//cin>>t;
	int t=1;
	while(t--)
	{
		solve();
	}
}
相关推荐
JieE2123 小时前
LeetCode 101. 对称二叉树|JS 递归 + 迭代双解法,彻底搞懂镜像判断
javascript·算法
JieE2121 天前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack201 天前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
用户497863050733 天前
(一)小红的数组操作
算法·编程语言