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();
	}
}
相关推荐
江畔柳前堤13 分钟前
消息队列:从直觉到精通的完整认知地图
开发语言·人工智能·算法·机器学习·ruby on rails·scala
ai产品老杨24 分钟前
明火识别算法接入AI视频分析平台全流程与误报优化指南(附接口字段与排查表)
人工智能·算法·音视频
讨厌吃蛋黄酥27 分钟前
📚408数据结构|最短路径算法一篇彻底搞懂!BFS、Dijkstra、Floyd别再傻傻分不清了
算法
Escalating_xu1 小时前
【C++ string 上篇】从字符串基础到容量管理、迭代器与经典算法题
java·c++·算法
Keven_112 小时前
算法札记:ACM中将高精度算法融于题目的模板
算法·acm·精度
小七在进步2 小时前
数据结构:快速排序
数据结构·算法·排序算法
闻缺陷则喜何志丹2 小时前
【计算几何 第十一章】凸包:混合物
数学·算法·计算几何·凸包·混合物·凸组合
土司大王2 小时前
LeetCode hot100——二叉树的最大深度
算法·leetcode·职场和发展
高亦真2 小时前
今天是学习嵌入式的第31天
linux·学习·算法
让学习成为一种生活方式2 小时前
黄花蒿LHC基因家族的串联重复驱动扩张及其UV-B胁迫适应性--BMC Plant Biology
人工智能·算法·机器学习