第六章-数据结构入门
来源:互联网 发布:snmp linux版本查询 编辑:程序博客网 时间:2024/05/21 00:45
6.1 forest
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; struct person { int number; int streng;//体力 int seat;//站位 }per[10086]; bool compare(const person &p1,const person &p2) { return p1.seat < p2.seat; } int main() { int i,j; int n; cin >> n; for(i = 1; i <= n; i++) { cin >> per[i].streng; per[i].seat = i; per[i].number = i; } int head = 1; int tail = n; for(i = 1; i <= n; i++) { if(per[i].streng > per[head].streng) { per[i].seat = per[head].seat - 1; head = i; } else { per[i].seat = per[tail].seat + 1; tail = i; } } sort(per+1,per+n+1,compare); for(i = 1; i <= n; i++) cout << per[i].number <<" "; return 0; }
6.2 queue1
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; int arr[10086]; int main() { int i,j; int n; cin >> n; int head= 0; int len = 0; string s; int num; for(i = 0; i < n; i++) { cin >> s; if(s == "INSERT") { cin >> num; arr[len++] = num; } else { if(head > len-1) cout << "ERROR" <<endl; else { cout << arr[head] <<endl; head++; } } } return 0; }
6.3 queue2
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; struct person { bool in; int left; int right; }per[100086]; int main() { int i,j; int n; cin >> n; per[0].right = 1; per[0].in = false; per[1].left = 0; per[1].right = n+1; per[1].in = true; per[n+1].in = false; int num,op; for(i = 2; i <= n; i++) { cin >> num >> op; if(op == 0) { per[per[num].left].right = i; per[i].left = per[num].left; per[num].left = i; per[i].right = num; per[i].in = true; } else { per[per[num].right].left = i; per[i].right = per[num].right; per[num].right = i; per[i].left = num; per[i].in = true; } } int m; cin >> m; for(i = 0; i < m; i++) { cin >> num; per[num].in = false; } i = 0; while(per[i].right < n+1) { if(per[per[i].right].in) cout << per[i].right << " "; i = per[i].right; } return 0; }
6.4 power
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<stack> using namespace std; stack<int> s; int main() { int i,j; int n,size; cin >> n >> size; int now= 0; int num; for(i = 0; i < n; i++) { cin >> num; while(now < num) s.push(++now); if(s.size() > size) break; if(s.top() != num) break; else s.pop(); } if(i < n) cout <<"ERROR " << num; else cout <<"OK"; return 0; }
6.5 maze1
#include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<queue> using namespace std; struct maze { int x; int y; }a,b; int vis[1010][1010]; int sum[1010][1010]; int step[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; queue<maze> que; int main() { int i,j; int n,m,t; cin >> n >> m >> t; //n行m列 t障碍物 int sx,sy; //起始坐标 int fx,fy; //终点坐标 cin >> sx >> sy >> fx >> fy; int tx,ty; for(i = 0; i < t; i++) { cin >> tx >> ty; vis[tx][ty] = 1; } //障碍物标记1 a.x = sx; a.y = sy; que.push(a); sum[sx][sy] = 1; while(!que.empty()) { a = que.front(); que.pop(); if(a.x == fx && a.y == fy) { cout << sum[a.x][a.y]; return 0; } else { for(i = 0; i < 4; i++) { int X = a.x + step[i][0]; int Y = a.y + step[i][1]; //在图内且未访问 if(!vis[X][Y] && X > 0 && Y > 0 && X <= n && Y <= m) { vis[X][Y] = 1; sum[X][Y] = sum[a.x][a.y] + 1; b.x = X; b.y = Y; que.push(b); } } } } cout <<"-1"; return 0; }
0 0
- 第六章-数据结构入门
- 算法竞赛入门经典 第六章 数据结构基础
- 算法竞赛入门经典:第六章 数据结构基础 6.2铁轨
- 算法竞赛入门经典:第六章 数据结构基础 6.4测试
- 算法竞赛入门经典:第六章 数据结构基础 6.11迷宫
- 《算法竞赛入门经典》-第六章-数据结构基础
- 数据结构 第六章 图
- 数据结构 第六章 图
- 数据结构第六章图
- 数据结构第六章结构图
- 第六章 数据结构
- [数据结构]第六章-树
- 《数据结构》第六章 图 学习指南
- 数据结构概述 第六章 图
- 《数据结构》第六章 图实验
- 数据结构第六章知识总结
- 第六章 对象和数据结构
- 《大话数据结构》第六章 树
- CSS实现loading小动画(附源码)
- 易學原論 (4288_3)
- 《初等算法》中文版
- iOS开发框架
- 三和韓長庚 著 易學原理總論 對讀 041_080
- 第六章-数据结构入门
- 【多题合集】网络流24题练习(更新至魔术球问题)
- 【bzoj 1923】[Sdoi2010]外星千足虫(高斯消元)
- 三联莎士比亚阅读摘记
- Arm9+linux fl2440 lcd驱动移植、添加MMC支持、添加U盘支持
- String HDU5672
- dto与po转换类
- 深夜课堂:JavaScript基本概念
- C语言综合练习题(01)