#对牛客小白月赛的代码注释#
本人是一名编程新手,希望通过记录代码达到理解代码,复习代码,提高编程水平
//A 题
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
void solve() {
long long a[3];
for (int i = 0; i < 3; i++) {
cin >> a[i];
}
sort(a, a + 2);
int f = 0;
for (int i = 0; i < 3; i++) {
a[i] *= 2;
if (a[0] + a[1] > a[2] && a[1] + a[2] > a[0] && a[0] + a[2] > a[1]) {//任意两边之和大于第三边
cout << "Yes";
f = 1;
return;
}
a[i] /= 2;//不要忘记复原
}
if (f == 0) cout << "No";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
while (t--) {
solve();
}
return 0;
}
/*#include<bits/stdc++.h>
using namespace std;
void solve(){
vector<long long> a(3);//用long long
for(int i = 0; i < 3; i++ ){
cin >> a[i];
}
//遍历,暴力枚举每条边都 增加两倍的情况
for(int i = 0; i < 3; i++ ){
vector<long long> v;
for(int j = 0; j < 3; j++ ){
if(i == j){
v.push_back(2 * a[j]);
}
else{
v.push_back(a[j]);
}
}
sort(v.begin(), v.end());//最小的两边之和如果大于第三边,则一定可以构成一个三角形
if(v[0] + v[1] > v[2]){
cout << "Yes";
return;
}
}
cout << "No";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
while(t--){
solve();
}
}*/
B 题
#include<bits/stdc++.h>
using namespace std;
int n;
int main(){
cin >> n;
int a[n + 10];
for(int i = 1; i <= n; i++ ){
cin >> a[i];
}
if(n == 1){
cout << "-1";
}
else {
cout << (n + 1) / 2;
}
return 0;
}
C题
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<int> a(n + 5), b(n + 5);
for(int i = 1; i <= n; i++ ){
cin >> a[i];
}
map<int, int> hash;//用哈希表记录 a[i] 属于哪个类别
for(int i = 1; i <= n; i++ ){
cin >> b[i];
hash[b[i]]++;//属于同
}
int ans = 0;
for(auto it : hash){//寻找a[i] 的属于的集合有几个元素,然后计算
if(it.second == 1){cout << "-1";
return;}//只要出现一个不能交换的就输出 -1, 返回
ans += (it.second + 1) / 2;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
while(t--){
solve();
}
return 0;
}
D题
#include<bits/stdc++.h>
using namespace std;
const int N = 2020;
int n, m, x, y;
char g[N][N];
int dist[N][N];
int tim[N][N];
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};//控制上下左右移动
struct node{//定义一个结构体, 也可以用pair<int, int>
int x, y;
};
int main(){
queue<node> q;
memset(dist, -1, sizeof(dist));
cin >> n >> m >> x >> y;
for(int i = 0; i < n; i++ ){
for(int j = 0; j < m; j++ ){
cin >> g[i][j];
if(g[i][j] == '@'){
q.push({i, j});
dist[i][j] = 0;
}
}
}//将‘@’看做起点
while(!q.empty()){//计算从 ‘@’ 到 ‘.’之间的距离, 用dist[i][j]保存
auto t = q.front();
q.pop();
for(int i = 0; i < 4; i++ ){
int a = t.x + dx[i], b = t.y + dy[i];
if(a < 0 || a >= n || b < 0 || b >= m || g[a][b] == '#' || dist[a][b] != -1){//不满足条件的直接进行下一次循环
continue;
}
else{
dist[a][b] = dist[t.x][t.y] + 1;//当前探索的结点加一 来更新距离
}
q.push({a, b});
}
}
x--;//将输入的坐标转变成 0索引在二位数组中的位置
y--;
q.push({x, y});//计算 终点到起点的距离
memset(tim, -1, sizeof(tim));
int res = 2e9;
tim[x][y] = 0;
while(!q.empty()){
auto tt = q.front();
q.pop();
if(g[tt.x][tt.y] == '@'){
res = min(res, tim[tt.x][tt.y] + dist[2 * x - tt.x][2 * y - tt.y]);// 反射操作的目的是找到一个新的坐标 (a2, b2),
//使得从 (a2, b2) 到起点的路径与从 (t.x, t.y) 到终点的路径在反射后是对称的。x - tt. x + x, y - tt.y + y
}
for(int i = 0; i < 4; i++ ){
int a1 = tt.x + dx[i], b1 = tt.y + dy[i];
if(a1 < 0 || a1 >= n || b1 < 0 || b1 >= m || g[a1][b1] == '#' || tim[a1][b1] != -1){
continue;
}
int a2 = 2 * x - a1, b2 = 2 * y -b1;
if(a2 < 0 || a2 >= n || b1 < 0 || b1 >= m || g[a2][b2] == '#'){
continue;
}
tim[a1][b1] = tim[tt.x][tt.y] + 1;//更新距离
q.push({a1, b1});//在把当前这个可以走的点压入队列
}
}
if(res == 2e9){
cout << "-1" << endl;
}
else cout << res << endl;
return 0;
}
D题暂时只能通过 37.5 的数据, 晚上重新梳理了一遍逻辑, 发现之前错误的原因为第一遍 bfs 的时候, 没有将下一个点推入到队列中
E题
这里应该注意, 用 vector<int> a 存储元素的时候, 不能直接用 cin >> a[i] ; , 否则,会出现错误
排雷的思路:
首先, 我们需要在存储的雷的容器中,找到一组递增的子序列, 序列间隔 为 x, 代表排雷能力, 每次排从当前位置 l 到 l + x 的雷, 枚举 x 的用的时间, 取最短的即可
#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> a;
int check(int x){
int ans = x;
int now = a[0];
for(int i = 0; i < n; i++ ){
auto pos = upper_bound(a.begin(), a.end(), now + x) - a.begin();
ans++;
if(pos == n){
break;
}
else now = a[pos];
}
return ans;
}
int main(){
cin >> n;
for(int i = 0; i < n; i++ ){
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int res = 2e9;
for(int i = 0; i <= 1000; i++ ){
res = min(res, check(i));
}
cout << res << endl;
return 0;
}
F:实力不够,不想取钻研, 等有 那个水平了再讨论
1745

被折叠的 条评论
为什么被折叠?



