UVA - 133 The Dole Queue(模拟链表)

点击打开链接

n的人围成一个环,然后按逆时针编号1-n,一个人从1开始逆时针数k个数,另一个人从N开始顺时针数m个数,然后 数出来的两个人出列(两个人可能一样)出列,然后继续此过程,直到全部人都出列为止。

思路是用循环链表来模拟,注意 要分情况来讨论。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define N 100005
using namespace std;
int n,k,m,l[25],r[25];
void init()
{
    int i;
    for(i=1;i<=n;i++)
    {
        l[i]=i-1;
        r[i]=i+1;
    }
    r[n]=1;l[1]=n;
}
void link(int x,int y)
{
    l[y]=x; r[x]=y;
}

int main()
{
    //Read();
    int i,j,t,x,y,c1,c2;
    while(scanf("%d%d%d",&n,&k,&m)!=EOF&&n+k+m)
    {
        init();
        t=0;
        x=1;y=n;
        c1=c2=1;
        while(t<n)
        {
            //if(t) printf(",");
            if(c1==k&&c2==m)
            {
                if(x==y)
                {
                    printf("%3d",x);
                    x=r[x];
                    y=l[y];
                    link(y,x);
                    t++;
                }
                else if(r[x]==y)
                {
                    printf("%3d%3d",x,y);
                    x=r[r[x]];
                    y=l[l[y]];
                    link(y,x);
                    t+=2;
                }
                else if(r[y]==x)
                {
                    printf("%3d%3d",x,y);
                    x=r[x];
                    y=l[y];
                    link(y,x);
                    t+=2;
                }
                else
                {
                    printf("%3d%3d",x,y);
                    x=r[x];
                    y=l[y];
                    link(l[l[x]],x);
                    link(y,r[r[y]]);
                    t+=2;
                }
                //printf("%d %d\n",x,y);
                c1=c2=1;
                if(t!=n) printf(",");
            }
            if(c1!=k) {c1++;x=r[x];}
            if(c2!=m) {c2++;y=l[y];}
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-08 03:11:38

UVA - 133 The Dole Queue(模拟链表)的相关文章

UVa 133 The Dole Queue

 The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone i

uva - 133 The Dole Queue(成环状态下的循环走步方法)

类型:循环走步 1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype> 10 #include &

133 - The Dole Queue

The Dole Queue In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is

UVa 11988 Broken Keyboard(数组模拟链表)

题目链接: https://cn.vjudge.net/problem/UVA-11988 1 /* 2 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键,表示光标跳到行末 3 4 解题思路 5 可以发现[]是可以嵌套的,采用纯模拟比较复杂,采用类似KMP中的next数组一样,记录一下每个字符的下一个字符位置,最后 6 输出即可. 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #i

The Dole Queue UVA - 133

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily cho

uva The Dole Queue

题目如下: The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The NewNational Green Labour Rhinoceros Party has decided on the followingstrategy. Every day all dole applicants will be placed in a largecircle, facing inwards. Someone

uva133 The Dole Queue ( 约瑟夫环的模拟)

题目链接: 啊哈哈,选我选我 思路是: 相当于模拟约瑟夫环,仅仅只是是从顺逆时针同一时候进行的,然后就是顺逆时针走能够编写一个函数,仅仅只是是走的方向的标志变量相反..还有就是为了(pos+flag+n-1)%n+1的妙用... 题目:  The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decid

UVa 11988 Broken Keyboard(模拟链表)

题意  有一个键盘坏了  会在你不知道的情况下按下home或者end  给你这个键盘的实际输入  要求输出显示器上的实际显示 输入最大5MB  所以直接数组检索肯定会超时的  用数组模拟链表  就可以很快了 #include<cstdio> #include<cstring> using namespace std; const int N=100005; char s[N]; int next[N]; int main() { int last,cur; while(~scanf

UVa 11988 数组模拟链表

题目:在一个没有显示器的电脑上输入一个字符串,键盘坏掉了,会随机的出现home,和end按键, 字符串中'['代表home键(句首),']'代表end键(句尾),问最后输出的字符串的格式. 分析:模拟屏幕操作,移动光标,模拟缓冲区输出操作. 说明:数组模拟链表操作,跟随链表操作,形象化模拟. 1 // UVa11988 Broken Keyboard 2 // Rujia Liu 3 #include<cstdio> 4 #include<cstring> 5 const int