HDU ------ (4302) holedox eating (tree array + binary)

Source: Internet
Author: User
Holedox eating

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3362 accepted submission (s): 1145


Problem descriptionholedox is a small animal which can be considered as one point. it lives in a straight pipe whose length is L. holedox can only move along the pipe. cakes may appear anywhere in the pipe, from time to time. when holedox wants to eat cakes, it always goes to the nearest one and eats it. if there are missing pieces of cake in different directions holedox can choose, holedox will choose one in the direction which is the direction of its last movement. if there are no cakes present, holedox just stays where it is.

 

Inputthe input consists of several test cases. the first line of the input contains a single integer T (1 <= T <= 10), the number of test cases, followed by the input data for each test case. the first line of each case contains two integers L, n (1 <= L, n <= 100000), representing the length of the pipe, and the number of events.
The next n lines, each line describes an event. 0x(0 <= x <= L, X is a integer) represents a piece of cake appears in the X position; 1 represent holedox wants to eat a cake.
In each case, holedox always starts off at the position 0.

 

Outputoutput the total distance holedox will move. holedox don't need to return to the position 0.

 

Sample input310 80 10 510 20 0111 10 10 510 20 01110 80 10 510 20 011

 

Sample outputcase 1: 9 Case 2: 4 Case 3: 2

 

Authorbupt

 

Source 2012 multi-university training contest 1 code:
1 # include <cstring> 2 # include <cstdio> 3 # define maxn 100080 4 # define INF 0x3f3f3f3f 5 Int next [maxn]; 6 int Len, N; 7 int lowbit (int x) {8 return X & (-x); 9} 10 void Update (INT St, int Val) {11 while (ST <= Len + 1) {12 next [st] + = val; 13 st + = lowbit (ST); 14} 15} 16 int query (INT st) {17 int ans = 0; 18 while (ST> 0) {19 ans + = next [st]; 20 St-= lowbit (ST); 21} 22 return ans; 23} 24 25 int main () {26 int tes T; 27 int Jud, TEM, POs, ans; 28 // freopen ("test. in "," r ", stdin); 29 // freopen (" test1.in "," W ", stdout); s 30 scanf (" % d ", & test ); 31 For (INT I = 1; I <= test; I ++) {32 scanf ("% d", & Len, & N); 33 memset (next, 0, sizeof (next); 34 bool flag = true; // start from 0, so you must go to 35 pos on the right = 1; // The initial bull position starts at the beginning of 1. 36 ans = 0; 37 while (n --) 38 {39 scanf ("% d", & Jud); 40 if (! Jud) {41 scanf ("% d", & TEM); 42 Update (TEM + 1, 1 ); 43} 44 else {// If Jud = 1, it indicates that the ox is going to eat cake 45 // but I don't know there is a cake... 46 int left = POs, Right = Len + 1; 47 int mid; 48 int RR = inf; // record the last right cake position 49 int LL =-INF; // record the nearest left cake position 50 bool iseat_r = false; 51 int st_num = query (pos-1 ); // Number of cakes under the starting position 52 while (left <= right) {53 mid = left + (right-left)> 1 ); 54 if (query (MID)> st_num) {55 right = mid-1; 56 RR = mid; 57 iseat_r = true; // indicates the cake has been eaten 58} 59 else 60 left = Mid + 1; 61} 62 left = 1; 63 right = Pos; 64 st_num = query (POS ); 65 bool iseat_l = false; 66 while (left <= right) {67 mid = left + (right-left)> 1 ); 68 if (st_num-query (mid-1)> 0) {69 left = Mid + 1; 70 LL = mid; 71 iseat_l = true; 72} 73 else right = mid-1; 74} 75 if (iseat_l | iseat_r) // you can eat cake on the other side. Otherwise, do not do anything 76 {77 int len1 = pos-ll; 78 int len2 = Rr-Pos; 79 if (len1 <len2) {80 Pos = ll; 81 ans + = len1; 82 flag = false; 83} 84 else {// if they are equal, can I eat them at will to keep the original direction first 85 if (len1> len2) {86 Pos = RR; 87 ans + = len2; 88 flag = true; 89} 90 else if (FLAG) {pos = RR; 91 ans + = len2; 92} 93 else {94 Pos = ll; 95 ans + = len1; 96} 97} 98 Update (Pos,-1); // eat a cake 99} 100} 101} 102 printf ("case % d: % d \ n ", I, ANS); 103} 104 return 0; 105}
View code

 

HDU ------ (4302) holedox eating (tree array + binary)

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.