Ural 1521. War Games 2 Joseph Ring SBT implementation

Source: Internet
Author: User
1521. War Games 2 Time Limit: 1.0 second
Memory Limit: 16 MBBackgroundDuring the latest war games (this story is fully described in the problem "War games ") the Minister of Defense of the Soviet Federation comrade Ivanov had a good chance to make sure personally, that an alertness of the Soviet Army under his command is just brilliant. but there was a thing, that continued to worry him. being an outstanding commander, he realized, that only physical conditions of the soldiers were demonstrated. so the time came to organize one more war games and examine their mental capacity. general Rascal was appointed to be responsible for the war games again. the general donated the allocated funds to the poor and went to bed free-hearted. in his dream, the tactics manual appeared to him and described a scheme, that allows to organize the war games absolutely free of charge. problemIn accordance with this scheme, the war games are dividedNPhases; andNSoldiers, successively numbered from 1N, Are marching round a circle one after another, I. e. the first follows the second, the second follows the third,..., (N-1)-th followsN-Th, andN-Th follows the first. at each phase, a single soldier leaves the circle and goes to clean the WC, while the others continue to march. at some phase, the circle is left by a soldier, who is marchingKPositions before the one, who left the circle at the previous phase. A soldier, whose number isK, Leaves the circle at the first phase. surely, Mr. rascal cherished no hope about his soldiers 'abilities to determine an order of leaving the circle. "These fools can not even paint the grass properly",-he sniffed scornfully and went to sergeant Filcher for an alert. inputThe only line contains the integer numbersN(1 ≤N≤ 100000) andK(1 ≤KN). OutputYou shocould output the numbers of soldiers as they leave the circle. The numbers shocould be separated by single spaces. Sample
Input Output
5 3
3 1 5 2 4

Abstract description:
N people are sequentially numbered in a circle, starting from 1 by 1, 2, 3 ...... the number of messages in sequence. The number of people reporting p is out of the circle, and the number of other people starting from 1, 2, and 3. The number of people reporting p is out of the circle, and so on.
Output the original sequence number of each exit person in the exit order.

Input:
Contains an integer N (1 <=n <= 100000) and an integer p.

Output:
There may be multiple groups of test data. For each group of data,
Output the original sequence number of each exit person in the exit order.

Sample input:

5 3

Sample output:
3 1 5 2 4

 

Analysis:
First insert everyone into SBT, then when going out, delete it from SBT, and find the element to delete as pos = (pre + k-1) % n + 1, n each time a person goes out, the pre starts from 0. If someone is already out of the queue, set it to the position where the last person is out of the queue, and then find the minimum pos.

The Code is as follows:

View Code

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 5 using namespace std; 6 7 const int X = 100005; 8 9 int root, tol, n, m; 10 11 struct node {12 int val, l, r, s; 13 void init (int _ val) {14 l = r = 0; 15 s = 1; 16 val = _ val; 17} 18} sbt [X]; 19 20 void left_rotate (int & t) {21 int k = sbt [t]. r; 22 sbt [t]. r = sbt [k]. l; 23 sbt [k]. l = t; 24 sbt [k]. s = sbt [t]. s; 25 sbt [t]. s = sbt [sbt [t]. l]. s + Sbt [sbt [t]. r]. s + 1; 26 t = k; 27} 28 29 void right_rotate (int & t) {30 int k = sbt [t]. l; 31 sbt [t]. l = sbt [k]. r; 32 sbt [k]. r = t; 33 sbt [k]. s = sbt [t]. s; 34 sbt [t]. s = sbt [sbt [t]. l]. s + sbt [sbt [t]. r]. s + 1; 35 t = k; 36} 37 38 void maintain (int & t, bool OK) {39 if (! OK) {40 if (sbt [sbt [sbt [t]. l]. l]. s> sbt [sbt [t]. r]. s) 41 right_rotate (t); 42 else if (sbt [sbt [sbt [t]. l]. r]. s> sbt [sbt [t]. r]. s) {43 left_rotate (sbt [t]. l); 44 right_rotate (t); 45} 46 else return; 47} 48 else {49 if (sbt [sbt [sbt [t]. r]. r]. s> sbt [sbt [t]. l]. s) 50 left_rotate (t); 51 else if (sbt [sbt [sbt [t]. r]. l]. s> sbt [sbt [t]. l]. s) {52 right_rotate (sbt [t]. r); 53 left_rotate (t); 54} 55 else return; 56} 57 maint Ain (sbt [t]. l, 0); 58 maintain (sbt [t]. r, 1); 59 maintain (t, 0); 60 maintain (t, 1); 61} 62 63 void insert (int & t, int val) {64 if (! T) {65 t = ++ tol; 66 sbt [t]. init (val); 67 return; 68} 69 sbt [t]. s ++; 70 if (val <sbt [t]. val) 71 insert (sbt [t]. l, val); 72 else 73 insert (sbt [t]. r, val); 74 maintain (t, val> = sbt [t]. val); 75} 76 77 int del (int & t, int val) {78 if (! T) return 0; 79 sbt [t]. s --; 80 if (val = sbt [t]. val | (val <sbt [t]. val &&! Sbt [t]. l) | (val> sbt [t]. val &&! Sbt [t]. r) {81 if (sbt [t]. l & sbt [t]. r) {82 int pos = del (sbt [t]. l, val + 1); 83 sbt [t]. val = sbt [pos]. val; 84 return pos; 85} 86 else {87 int pos = t; 88 t = sbt [t]. l + sbt [t]. r; 89 return pos; 90} 91} 92 else 93 return del (val <sbt [t]. val? Sbt [t]. l: sbt [t]. r, val); 94} 95 96 int find_k_min (int & t, int k) {// find the k small 97 if (k <= sbt [sbt [t]. l]. s) 98 return find_k_min (sbt [t]. l, k); 99 else if (k> sbt [sbt [t]. l]. s + 1) 100 return find_k_min (sbt [t]. r, k-sbt [sbt [t]. l]. s-1); 101 else102 return sbt [t]. val; 103} 104 105 int main () 106 {107 freopen ("sum. in "," r ", stdin); 108 freopen (" sum. out "," w ", stdout); 109 while (cin> n> m) {110 int pos = 0, temp, val; 111 root = tol = 0; 112 for (int I = 1; I <= n; I ++) 113 insert (root, I); 114 while (n) {115 temp = (pos + s-1) % n + 1; 116 pos = temp-1; 117 val = find_k_min (root, temp); 118 del (root, val); 119 printf ("% d", val ); 120 if (n> 1) 121 putchar (''); 122 n --; 123} 124 puts (" "); 125} 126 return 0; 127}

 

 

 

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.