数据结构(入门)

· · 算法·理论

数据结构

今天,我们又要认识两位新的数据结构:vector 和 pair 。

\textrm v \textrm e \textrm c \textrm t \textrm o \textrm r

$vector$ 插入模式是怎样的? $vector$ 就像队列一样,是从尾巴那放入元素的,然而,它可以像数组一样访问元素(下标从 $0$ 开始) 。 ## $\textrm v \textrm e \textrm c \textrm t \textrm o \textrm r$ 的操作: - 定义: ```cpp vector<int> v;//创建一个类型为 int 的自动延长数组 ``` - 插入: ```cpp v[1]=1; ``` 这是非法的插入 。 ```cpp v.push_back(val); ``` 这是合法的插入,表示插入了一个元素 $val$ 到 $v$ 的尾部 。 插入的时间复杂度为 $O( \log n )$ 。 - 可以使用下标,小标只能从零开始 。 - 长度: ```cpp v.size(); ``` 表示获取 $v$ 的长度(元素的个数) 。 - 清空: ```cpp v.clear(); ``` 表示清空 $v$ 中所有的元素,此时长度为 $0$ 。 - 插入指定位置: ```cpp v.insert(v.begin()+i,val); ``` $i$ 是下标,表示将元素 $val$ 插入到 $i+1$ 号位置前面 。 - 删除: ```cpp v.erase(v.begin()+i); ``` 表示删除第 $i+1$ 号位置前面的元素 。 - 初始化: ```cpp v.resize(n); v.resize(n,val); ``` 第一行表示将 $v$ 长度变为 $n$ 值为 $0$ ;第二行表示将 $v$ 长度变为 $n$ ,值都是 $val$ 。 - 排序: ```cpp sort(v.bigin(),v.end()); ``` 表示将 $v$ 按照从小到大的规则进行排序 。 # 例题 ## [F1501 数组存数](https://gonoi.com.cn/problem/F1501) - 这题不仅要将元素一个一个放入,还要将每一个元素排序,这样用二维数组会很麻烦(总不可能定义一堆一维数组) - 要解决题目中的输入格式,可以用一个 $vector$ 来实现,而排序上面也讲过了,这样,距离实现只剩处理多个数组了 。 - 我们可以定义一个结构体,里面写一个 $vector$ ,这样就能达到多个数组的效果。 ## 代码: ```cpp #include <bits/stdc++.h> using namespace std; int n, m; struct no{ vector<int> v; } a[100005]; int main() { cin >> n >> m; for (int i = 1;i<=m;++i) { int b, s; cin>>b>>s; a[b].v.push_back(s); } for (int i = 1; i <= n;++i) { cout<<a[i].v.size()<<' '; sort(a[i].v.begin(),a[i].v.end()); for (int j = 0;j<a[i].v.size();++j) { cout<<a[i].v[j]<<" "; } cout << endl; } return 0; } ``` # 数据结构:$ \textrm p \textrm a \textrm i \textrm r ## 操作: - 定义: ```cpp pair<int,int> pa; ``` 表示定义了一个两个元素类型都为 $int$ 的 $pair$ 类型的 $pa$ 。 - 插入: ```cpp pa=make_pair(11,45); ``` 表示往 $pa$ 的第一个元素修改为 $11$ ,第二个修改为 $45$ 。 - 查询: ```cpp cout<<pa.first; cout<<endl; cout<<pa.second; ``` 表示输出了 $pa$ 中的第一个元素,输出换行,再输出第二个元素 。 好了,基本的使用你掌握了吗? ~~(没有)~~ # 例题 ## [B3702 华小科的旅行开始了](https://www.luogu.com.cn/problem/B3702) - 这题我们可以用 $pair$ 来存当前的行和列,然后用递归求解即可 。 ## 代码: ```cpp #include<bits/stdc++.h> #define fi first #define se second using namespace std; int n,m,sx,sy; pair<int,int> pa[1005][1005]; void f(int x,int y) { if(pa[x][y].fi==0&&pa[x][y].se==0) { cout<<x<<' '<<y; exit(0); } else { cout<<x<<' '<<y<<'\n'; f(pa[x][y].fi,pa[x][y].se); } } int main() { cin>>m>>n>>sx>>sy; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { int x,y; cin>>x>>y; pa[i][j]=make_pair(x,y); } } f(sx,sy); return 0; } ``` # 综合 ## [F1443 走出迷宫的最短路径](https://gonoi.com.cn/problem/F1443) - 这题怎么用 $pair$ 和 $vector$ 来做呢 ? 可以使用 $pair$ 记下坐标,用 $vector$ 来存储最短路径所经过的坐标,也就是 $vector$ 套 $pair$ 。 ## 代码: ```cpp #include<bits/stdc++.h> using namespace std; const int N=25; int n,m,sx,sy,ex,ey; int a[N][N]; bool vis[N][N]; int dx[]={0,0,0,1,-1}; int dy[]={0,1,-1,0,0}; struct no{ int x,y; vector<pair<int,int> > path; }; bool check(int x,int y) { if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]) return true; return false; } void bfs() { queue<no> q; vector<pair<int,int> > path1; path1.push_back(make_pair(sx,sy)); q.push(no{sx,sy,path1}); while(!q.empty()) { no tmp=q.front(); q.pop(); for(int i=1;i<=4;++i) { int nx=tmp.x+dx[i]; int ny=tmp.y+dy[i]; if(check(nx,ny)) { tmp.path.push_back(make_pair(nx,ny)); q.push(no{nx,ny,tmp.path}); tmp.path.pop_back(); if(nx==ex&&ny==ey) { vector<pair<int,int> > path2=tmp.path; for(int j=0;j<path2.size();++j) { cout<<"("<<path2[j].first<<","<<path2[j].second<<")->"; } cout<<"("<<ex<<","<<ey<<")"; exit(0); } } } } } int main() { cin>>n>>m; for(int i=1;i<=n;++i) { for(int j=1;j<=m;++j) { cin>>a[i][j]; if(a[i][j]==1) vis[i][j]=1; } } cin>>sx>>sy>>ex>>ey; bfs(); cout<<"no way"; return 0; } ``` # 作业: ## [P1825 Corn Maze S](https://www.luogu.com.cn/problem/P1825) ## [P6207 Cows on Skates G](https://www.luogu.com.cn/problem/P6207) # The End.