UVa 133:the Dole Queue Data Structure topic

Source: Internet
Author: User
Tags prev printf

Topic Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_ problem&problem=69

Type of topic: data structure, linked list

For: N applications, in a circle, in a counter-clockwise direction numbered 1~n. There are two civil servants, 1 of the civil servants standing at 1, to the counterclockwise direction to the K, selected, the civil servant 2 standing in N, to the clockwise direction of the number m, selected. Take away the selected number of the application, the output number. If the number is the same, only one number is output. Until all applications have been taken out of the light.

Disintegration of ideas: the use of circular chain table simulation, take away the number from the list deleted. The hardest part of this problem is that two people delete their new position after the decision and the choice, because the first person's new position may be the second person to delete the position, similarly, the second person's new position may also be the first person to delete the location. Finally, the loop ends the judgment. These will have detailed comments in the code.

Input:

4 3
0 0 0

Output:

4 8, 9 5, 3 1, 2 6, 10, 7

where represents a space.

#include <stdio.h> #include <string.h> int prev[25];  
int next[25];  
    
int N, POS1, pos2,m,n;  
    Delete node void remove (int n) {next[prev[n]] = next[n];  
Prev[next[n]] = prev[n];  
    } void Init () {memset (prev,0,sizeof (prev));  
    memset (Next,0,sizeof (next));  
        for (int i=1; i<=n;i++) {prev[i]=i-1;  
    next[i]=i+1;  
    } prev[1]=n;  
    Next[n]=1;  
    Pos1=1;  
Pos2=n; } void Solve () {while (1) {if (Prev[pos2]==pos2 | | next[pos1]==pos1) {printf ("%3d\n", p  
            OS1);  
        Break  
        ///The first person to move int stepnum=1;  
        while (stepnum++<m) pos1 = next[pos1];  
        The second person carries on the mobile stepnum=1;  
    
        while (stepnum++<n) Pos2 = Prev[pos2];  
            If two persons move to the same place if (POS1==POS2) {printf ("%3d,", POS1); Find a new location for two people//This article URL address: Http://www.bianceng.cn/Programming/sjjg/201410/45540.htm int Newpos1=next[pos1],newpos2=prev[pos2];  
            If the first person's new position is the position to be deleted by the second one, then skip that position if (NEWPOS1==POS2) NEWPOS1=NEXT[NEWPOS1];  
                
            If the second person's new position is the first person to delete the position, then also want to skip that position if (NEWPOS2==POS1) Newpos2=prev[newpos2];  
                
            Delete the node remove (POS1);  
            POS1=NEWPOS1;  
        Pos2=newpos2;  
            
            else {int Newpos1=next[pos1],newpos2=prev[pos2];       
            If the first person's new position is the position to be deleted by the second one, then skip that position if (NEWPOS1==POS2) NEWPOS1=NEXT[NEWPOS1];  
                
            If the second person's new position is the first person to delete the position, then also want to skip that position if (NEWPOS2==POS1) Newpos2=prev[newpos2];  
            Special case: When only two nodes are left and two are selected, two are deleted//At this point, the new position of one person must still be the position it was originally to delete//Because another person is to be deleted  
            if (NEWPOS1==POS1) {printf ("%3d%3d\n", Pos1,pos2); if (newpos2==POS2) {printf ("%3d%3d\n", Pos1,pos2);  
    
            printf ("%3d%3d,", Pos1,pos2);  
            Remove (POS1);  
                
            Remove (POS2);  
            POS1=NEWPOS1;  
        Pos2=newpos2;  
    ()} int main () {freopen ("Input.txt", "R", stdin);  
        while (scanf ("%d%d%d", &n,&m,&n)) {if (n==0 && m==0 && n==0) return 0;  
        Init ();        
    Solve ();  
return 0;         }

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.