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;
}
时间: 2024-10-07 17:14:27