比赛链接:牛客竞赛_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();
}
}