文章目录
牛客小白月赛99
康复训练
A. 材料打印(思维)
注意到两种打印方式的价格的大小关系。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int ncase;
cin >> ncase;
while(ncase--){
ll a, b, x, y, res;
cin >> a >> b >> x >> y;
if(x < y) res = a * x + b * y;
else res = (a + b) * y;
cout << res << endl;
}
return 0;
}
B. %%%(思维,数论)
思路:
通过简单的思考,n % 2 = 0 or 1,n % 3 = 0,1,2 ,n % 4 = 0,1,2,3,…
观察上述规律,令 n = 2k + 1,则 n % (k+1) 时,取得最大值 k;令 n = 2k,则 n % (k+1)时,取得最大值 k-1。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int ncase;
cin >> ncase;
while(ncase--){
ll n;
cin >> n;
int count = 0;
while(n){
n = n % (n / 2 + 1);
count++;
}
cout << count << endl;
}
return 0;
}
C. 迷宫(BFS)
思路:
如果没有超能力,简单BFS,判断从S是否可以到E。
考虑超能力的使用,假设从S出发可以到达 (xS, yS),在使用超能力时,可以走到 xS 行或 yS列所在的所有点。
在从S出发BFS时,记录过程点所在的行和列。再从E出发BFS,判断是否可以走到第一遍BFS时记录的行或列。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1005;
typedef struct Node{
int x;
int y;
} node;
char ma[maxn][maxn];
int flag[maxn][maxn];
int row[maxn], column[maxn];
int opx[5] = {0, 0, 0, 1, -1};
int opy[5] = {0, 1, -1, 0, 0};
queue<node> qu;
int main(){
int n, m;
cin >> n >> m;
int ex, ey;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> ma[i][j];
if(ma[i][j] == 'S'){
qu.push({i, j});
flag[i][j] = 1;
}
if(ma[i][j]