URAL 1521 War Games 2 tree array to resolve Joseph Ring, output out of order

Source: Internet
Author: User
Tags integer numbers

1521. War Games 2Time limit:1.0 Second
Memory limit:64 MB
Backgroundduring The latest War games (this story was fully described in the problem "war games") the Minister of Defense O f the Soviet Federation Comrade Ivanov had a good chance to make sure personally, that's an alertness of the Soviet Army und Er his command is just brilliant. But there is a thing, that's continued to worry him. Being an outstanding commander, he realized, that's 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 is appointed to being responsible for the war games again. The general donated the allocated funds to the poor and went to bed free-hearted. In He dream, the tactics manual appeared to him and described a scheme, which allows to organize the war games absolutely Free of charge. Problemin accordance with this scheme, the war games is divided into Nphases; and NSoldiers, successively numbered from 1 to N, is marching round a circle one after another, i.e. the first follows the second, the second follows the third, ..., the ( N-1)-th follows the N-th, and the N-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 was left by a soldier, who is marching KPositions before the one, who left the circle at the previous phase. A soldier, whose number is K, leaves the circle at the first phase. Surely, Mr Rascal cherished no hope about he 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 Assistanc E.inputthe only line contains the integer numbers N(1≤ N≤100000) and K(1≤ KN). Outputyou should output the numbers of soldiers as they leave the circle. The numbers should is separated by a single spaces. Sample
input Output
5 3
3 1 5) 2 4



Test instructions: Joseph ring question. Enter n is the total number of people, the number of K to leave. Output out of order.
Practice: Because you want to output the position in the original string, we use a tree array plus two points to find. The tree array begins with each point initialized to 1, and then the departure is changed to minus one. for sum (x), X is the label of the original ring, and sum (x) indicates the number of rings in the current reduction.



Tree array  single point update  interval query # include <stdio.h> #include <string.h>const int max_n=100000+5;//open to interval size enough int Bit[max_n],n;  int sum (int i) {int s=0;while (i>0) {s+=bit[i];i-=i& (-i);  The feeling is added upward from the bottom.  1}return S at the end of the addition and subtraction of binary;} void Add (int i,int x) {while (i<=n) {bit[i]+=x;i+=i& (-i);}  int find (int x,int N) {int L=1,r=n;while (l<=r) { int mid= (L+R)/2;int tem=sum (mid); if (tem<x) l=mid+1;elser=mid-1;} printf ("%d", l); return l;} int main () {int n,k;scanf ("%d%d", &n,&k); N=n;memset (bit,0,sizeof (bit)); for (int i=1;i<=n;i++) Add (i,1);  







URAL 1521 War Games 2 tree array to resolve Joseph Ring, output out of order

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.