当前位置: 首页 > news >正文

C++算法之双指针、BFS和图论

一、双指针

1.AcWing 1238.日志统计

分析思路

前一区间和后一区间有大部分是存在重复的
我们要做的就是利用这部分 来缩短我们查询的时间
并且在使用双指针时要注意对所有的博客记录按时间从小到大先排好顺序
因为在有序的区间内才能使用双指针记录两个区间相差
相当于把一个有序的时间序列进行每次递增1的划分

代码实现
#include<iostream>
#include<algorithm>#define x first
#define y second
using namespace std;
const int N=100010;
typedef pair<int,int>PII;
PII logs[N];
bool st[N];
int cnt[N];
int main()
{int n,d,k;cin>>n>>d>>k;for(int i=0;i<n;i++) cin>>logs[i].x>>logs[i].y;sort(logs,logs+n);for(int i=0,j=0;i<n;i++){int t=logs[i].y;cnt[t]++;while(logs[i].x-logs[j].x>=d){cnt[logs[j].y]--;j++;}if(cnt[t]>=k) st[t]=true;}for(int i=0;i<100000;i++){if(st[i]) cout<<i<<endl;}return 0;
}

2.AcWing 1240.完全二叉树的权值  

分析思路

双指针:

从第一层根节点i=1,开始每一层开头都是2i,而每一层的长度就为pow(2,d-1)(d为层数)

前缀和:

因为是完全二叉树,所以算到最后要考虑是否越界。

代码实现

双指针

#include<bits/stdc++.h>using namespace std;
typedef long long LL;
const int N=100010;
int q[N];
LL sum[N];
int n,t;int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&q[i]);LL m=-1e18;for(int i=1,d=1;i<=n;i*=2,d++){LL s=0;for(int j=i;j<i+pow(2,d-1)&&j<=n;j++) s+=q[j];if(s>m){m=s;t=d;}}cout<<t<<endl;return 0;
}

前缀和

#include<bits/stdc++.h>using namespace std;
typedef long long LL;
const int N=100010;
int q[N];
LL sum[N];
int n,t;int main()
{scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&q[i]);for(int i=1;i<=n;i++) sum[i]=sum[i-1]+q[i];LL m=-1e18;for(int i=1,d=1;i<=n;i*=2,d++){LL s=0;LL r=i+pow(2,d-1)-1;if(r<=n) s=sum[r]-sum[i-1];else s=sum[n]-sum[i-1];if(s>m){m=s;t=d;}}cout<<t<<endl;return 0;
}

二、BFS

1.AcWing 1101.献给阿尔吉侬的花束

分析思路

BFS广度优先遍历,就是队首出队,然后与队首相关的入队。上图为模拟过程!

代码实现
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>#define x first
#define y secondusing namespace std;typedef pair<int,int> PII;const int N=210;int n,m;
char g[N][N];
int dist[N][N];int bfs(PII start,PII end)
{//队列queue<PII>q;memset(dist,-1,sizeof dist);//初始化距离为-1;dist[start.x][start.y]=0;q.push(start);int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};while(q.size())//队列不为空{auto t=q.front();//队首q.pop();//枚举四种方向上的情况for (int i = 0; i < 4; i ++ ){int x = t.x + dx[i], y = t.y + dy[i];if (x < 0 || x >= n || y < 0 || y >= m) continue;  // 出界if (g[x][y] == '#') continue;  // 障碍物if (dist[x][y] != -1) continue;  // 之前已经遍历过dist[x][y] = dist[t.x][t.y] + 1;if (end == make_pair(x, y)) return dist[x][y];q.push({x, y});}}return -1;
}int main()
{int t;cin>>t;while(t--){cin>>n>>m;for(int i=0;i<n;i++) scanf("%s",g[i]);//读入的是字符串,不需要加&PII start,end;//起点与终点for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(g[i][j]=='S') start={i,j};else if(g[i][j]=='E') end={i,j};}}int distance = bfs(start, end);if (distance == -1) puts("oop!");else printf("%d\n", distance);}return 0;
}

2.AcWing 1096.地牢大师

分析思路

二维上多加了一维,就有六个方向,读入用二维读入。

代码实现
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>using namespace std;struct Point
{int x,y,z;  
};const int N=100;
char g[N][N][N];
int dist[N][N][N];
int l,r,c;
Point q[N * N * N];
int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};int bfs(Point start,Point end)
{int hh=0,tt=0;q[0]=start;memset(dist,-1,sizeof dist);dist[start.x][start.y][start.z]=0;while(hh<=tt){auto t=q[hh++];for(int i=0;i<6;i++){int x=t.x+dx[i],y=t.y+dy[i],z=t.z+dz[i];if(x<0||y<0||z<0||x>=l||y>=r||z>=c) continue;if(g[x][y][z]=='#'||dist[x][y][z]!=-1) continue;dist[x][y][z]=dist[t.x][t.y][t.z]+1;if(g[x][y][z]=='E') return dist[x][y][z];q[++tt]={x,y,z};}}return -1;}int main()
{while(cin>>l>>r>>c,l||r||c){Point start,end;for(int i=0;i<l;i++){for(int j=0;j<r;j++){scanf("%s",g[i][j]);//三维用两维来存for(int k=0;k<c;k++){if(g[i][j][k]=='S') start={i,j,k};else if(g[i][j][k]=='E') end={i,j,k};}}}int distance=bfs(start,end);if(distance==-1) puts("Trapped!");else printf("Escaped in %d minute(s).\n",distance);}return 0;
}

用队列的方式: 

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>using namespace std;struct Point
{int x,y,z;  
};const int N=100;
char g[N][N][N];
int dist[N][N][N];
int l,r,c;int dx[6] = {1, -1, 0, 0, 0, 0};
int dy[6] = {0, 0, 1, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};int bfs(Point start,Point end)
{queue<Point> q;int hh=0;q.push(start);memset(dist,-1,sizeof dist);dist[start.x][start.y][start.z]=0;while(q.size()){auto t=q.front();for(int i=0;i<6;i++){int x=t.x+dx[i],y=t.y+dy[i],z=t.z+dz[i];if(x<0||y<0||z<0||x>=l||y>=r||z>=c) continue;if(g[x][y][z]=='#'||dist[x][y][z]!=-1) continue;dist[x][y][z]=dist[t.x][t.y][t.z]+1;if(g[x][y][z]=='E') return dist[x][y][z];q.push({x,y,z});}q.pop();}return -1;}int main()
{while(cin>>l>>r>>c,l||r||c){Point start,end;for(int i=0;i<l;i++){for(int j=0;j<r;j++){scanf("%s",g[i][j]);//三维用两维来存for(int k=0;k<c;k++){if(g[i][j][k]=='S') start={i,j,k};else if(g[i][j][k]=='E') end={i,j,k};}}}int distance=bfs(start,end);if(distance==-1) puts("Trapped!");else printf("Escaped in %d minute(s).\n",distance);}return 0;
}

http://www.lryc.cn/news/297192.html

相关文章:

  • 【大厂AI课学习笔记】1.5 AI技术领域(3)自然语言处理
  • 【数字电子技术课程设计】多功能数字电子钟的设计
  • 【新书推荐】7.3 for语句
  • 爬山算法优化遗传算法优化极限学习机的多分类预测,p-ga-elm多分类预测
  • 挑战杯 opencv 图像识别 指纹识别 - python
  • 【Docker】了解Docker Desktop桌面应用程序,TA是如何管理和运行Docker容器(2)
  • PHP、Python、Java 和 Go语言对比
  • 算法题目题单+题解——图论
  • 车载测试中:如何处理 bug
  • 亲测解决vscode的debug用不了、点了没反应
  • 立足智能存取解决方案|HEGERLS智能托盘四向车储存制动能量 实现能源回收
  • 2024.2.8日总结(小程序开发5)
  • Spring Boot配置文件优先级
  • Rust 初体验1
  • 【深度学习】实验7布置,图像超分辨
  • 【八大排序】归并排序 | 计数排序 + 图文详解!!
  • Netty应用(三) 之 NIO开发使用 网络编程 多路复用
  • 融资项目——配置redis
  • npm修改镜像源
  • K8S系列文章之 [基于 Alpine 使用 kubeadm 搭建 k8s]
  • JVM相关-JVM模型、垃圾回收、JVM调优
  • 提升图像分割精度:学习UNet++算法
  • 排序算法---冒泡排序
  • 基于数据挖掘的微博事件分析与可视化大屏分析系统
  • 数学建模-灰色预测最强讲义 GM(1,1)原理及Python实现
  • 智慧自助餐饮系统(SpringBoot+MP+Vue+微信小程序+JNI+ncnn+YOLOX-Nano)
  • 零基础学编程从入门到精通,系统化的编程视频教程上线,中文编程开发语言工具构件之缩放控制面板构件用法
  • 【MySQL进阶之路】MySQL 中表空间和数据区的概念以及预读机制
  • JVM 性能调优 - 常用的垃圾回收器(6)
  • 【java】Hibernate访问数据库