UVA-133 the Dole Queue (analog link list)

Source: Internet
Author: User

Click to open link

n people surround a ring, and then counter-clockwise number 1-n, a person starting from 1 counter-clockwise number K number, another person from N began clockwise number m number, then the number of two people out (two people may be the same) out, and then continue this process until all the people out.

The idea is to use a circular linked list to simulate, attention to be divided into situations to discuss.

#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) <&lt ; 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 100005using 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;}

UVA-133 the Dole Queue (analog link list)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.