都是循环队列的最基本应用,都是简单模拟,就不多讲了。
POJ 3125
题目传送门:http://poj.org/problem?id=3125
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAX = 105;
int main()
{
int t;
scanf("%d",&t);
int n,loc;
int q[MAX];
while (t --) {
scanf("%d%d",&n,&loc);
int maxm = 0;
int first = 0, rear = n;
for (int i = 0; i < n; i ++) {
scanf("%d",&q[i]);
if (q[i] > maxm) maxm = q[i];
}
int ans = 0;
while (1) {
int now = q[first];
if (now == maxm) {
ans ++;
if (first == loc) {
break;
}
first = (first+1) % (MAX - 2);
maxm = 0;
for (int i = first; i != rear ; i = (i + 1) % (MAX - 2)) {
if (maxm < q[i]) {
maxm = q[i];
}
}
} else {
q[rear] = now;
if (first == loc) {
loc = rear;
}
first = (first+1) % (MAX - 2);
rear = (rear + 1) % (MAX - 2);
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 3750
题目传送门:http://poj.org/problem?id=3125
约瑟夫环的模拟
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
char name[64][20];
int n,i,j;
int w,s,pos;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",name[i]);
scanf("%d,%d",&w,&s);
pos = w - 2;
for(int i = 0;i < n;i ++)
{
for(j = 0;j < s;j ++)
{
pos ++;
if(pos >= n)
pos %= n;
if(name[pos][18] == 1 )
j --;
}
puts(name[pos]);
name[pos][18]=1;
}
}