2024牛客寒假算法基础集训营4 题解( A,B,C,D,G )
2024牛客寒假算法基础集训营4
A 柠檬可乐
题意
根据给定的
思路
题意非常直接
代码
/*******************************
| Author: AlwaysBeShine
| Problem: 柠檬可乐
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67744/A
| When: 2024-02-19 13:08:06
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a,b,k;
cin >> a >> b >> k;
if(a >= k*b)cout << "good" << "\n";
else cout << "bad" << "\n";
return 0;
}
B 左右互博
题意
讨厌鬼在和小甜妹在玩石头游戏。 游戏一开始有
思路
假设某堆石子共有
所以对这堆石子操作的最优策略肯定是将其分为
所以只要判断 个数不为
奇数就是 sweet 赢,偶数就是 gui 赢
代码
/*******************************
| Author: AlwaysBeShine
| Problem: 左右互博
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67744/B
| When: 2024-02-19 13:09:33
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
ll sum = 0;
int cnt = 0;
std::vector<int> a(n);
for(int i = 0;i < n;i++){
cin >> a[i];
if(a[i] != 1){
cnt++;
sum+=a[i];
}
}
if((sum - cnt) % 2 == 0)cout << "sweet";
else cout << "gui";
return 0;
}
C 冬眠
题意
阿宁生活在一个
在每一天,都会有
如果是第
如果是第
阿宁将要冬眠
思路
数据范围允许暴力模拟,直接模拟即可
代码
/*******************************
| Author: AlwaysBeShine
| Problem: 冬眠
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67744/C
| When: 2024-02-19 13:20:34
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char zf[110][110];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,m,x,y;
cin >> n >> m >> x >> y;
for(int y = 0;y < n;y++){
for(int x = 0; x < m;x++){
cin >> zf[y][x];
}
}
int p,q;
cin >> p >> q;
vector<pair<int,int>>cz(q);
for(auto &[op,z]:cz){
cin >> op >> z;
}
while(p--){
for(auto &[op,z]:cz){
if(op == 1){
char tp = zf[z-1][m-1];
for(int i = m-2;i >= 0;i--){
zf[z-1][i+1] = zf[z-1][i];
}
zf[z-1][0] = tp;
}else{
char tp = zf[n-1][z-1];
for(int i = n-2;i >= 0;i--){
zf[i+1][z-1] = zf[i][z-1];
}
zf[0][z-1] = tp;
}
}
// for(int y = 0;y < n;y++){
// for(int x = 0;x < m;x++){
// cout << zf[y][x] << " \n"[x == m-1];
// }
// }
}
cout << zf[x-1][y-1];
return 0;
}
D 守恒
题意
有一个长度为
可以进行任意次操作,每次操作选择数组
想知道操作结束后,数组的最大公约数可能有多少种不同的值?
数组的最大公约数指,该数组的所有数共有约数中最大的那个数。
思路
设数组的最大公约数为
如果数组共有
令
当
当
代码
/*******************************
| Author: AlwaysBeShine
| Problem: 守恒
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67744/D
| When: 2024-02-19 17:39:11
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll sum = 0;
ll n;
cin >> n;
for(int i = 1;i <= n;i++){
ll x;
cin >> x;
sum += x;
}
ll cnt = 0;
for(ll i = 1;i <= sum / n;i++){
if(sum % i == 0)cnt++;
}
cout << (n == 1 ? 1 : cnt);
return 0;
}
G 数三角形(easy)
认为一个大小为
大小为
现在给出一个
思路
等腰三角形的斜边,必须满足如下
*
.
*
.
这样的规律间隔的
如果暴力,会达到
考虑用标记数组,预处理每个点作为等腰三角形的两底角顶点能支持构建起的最长的斜边长度和底边长度。
例如当前遍历到的点为
要满足条件如下:
代码
/*******************************
| Author: AlwaysBeShine
| Problem: 数三角形(easy)
| Contest: NowCoder
| URL: https://ac.nowcoder.com/acm/contest/67744/G
| When: 2024-02-19 14:17:05
|
| Memory: 524288 MB
| Time: 2000 ms
*******************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cnt;
int n,m;
char jz[505][505];
int qzh[505][505];
int qzh1[505][505];
int qzh2[505][505];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int y = 1;y <= n;y++){
for(int x = 1;x <= m;x++){
cin >> jz[x][y];
if(jz[x][y] == '.'){
qzh[x][y] = 0;
qzh1[x][y] = 0;
qzh2[x][y] = 0;
}else{
qzh[x][y] = qzh1[x][y] = qzh2[x][y] = 1;
qzh[x][y] += qzh[x-1][y];
qzh1[x][y] += qzh1[x+1][y-1];
qzh2[x][y] += qzh2[x-1][y-1];
}
}
}
if(n < 2 || m < 3){
cout << 0;
}else{
for(int x = 2;x <= m-1;x++){ //遍历每一个点
for(int y = 1;y <= n-1;y++){
if(jz[x][y] == '*'){
for(int size = 1; n - y + 1 >= size + 1 && x - size >= 1 && x + size <= m;size++){ //遍历每个有可能作为顶点的位置
if(qzh1[x-size][y+size] - 1 >= size && qzh2[x+size][y+size] -1 >= size && qzh[x+size][y+size] -1 >> 1 >= size)cnt++;
}
}
}
}
cout << cnt;
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!