Solution to a problem P3620 [[APIO / CTSC 2007] Data Backup]

Direct greedy (each selected minimum), then obviously does not pass all the sample ... ... selected when two building, another building obviously can not cross, and thus not preferable ... first original series is processed into the number of n-1 (on every office and a distance), the presence of a [] in the subject is asked to choose the number of K groups not adjacent, and such that the minimum number of selected

Still considered greedy, but every election minimal if a [] is 2126, to choose K = 2 Ge, first select the 1, then no method will find two 2 election, we had to choose 6, so embarrassing 1 picked up ... we chose it, we consider remedies ... 1 is selected when, in two pile delete the 2, and then insert the stack 2 + later we can take the equivalent of 2-1 2 to that two, and may represent 2-1 + 2 is selected from the two numbers, because just one chose, so here -1

... There is a problem, such as taking the boundary of the first or the last digit number, then the a [0] assigned to a maximum value and a [n] ... chanting

Because the number of sides of the border is only a valid number, it can not be remedied (the time needed to remedy number two ...), assigned to the maximum then the future can not be taken to

It would have to store a number in front of a number is the number, the number is a number (conveniently deleted)

721268 Take 1, 2 deleted both, and then open a new list spatial a [++ n], + 2 storage 2-1, substituted with this list space 212

The new list of space, the precursor is 7 location, location successor is 6 ... Do not forget to change 7 and 6 predecessor and successor ...

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm> 
 5 #define N 100009
 6 using namespace std;
 7 int n,dis[N],la,lheap,heap[N<<3],pos[N<<3],K,now,point[N<<3][2],prev,next;
 8 long long a[N<<3],ans;
 9 inline void push(int x)
10 {
11     int i=++lheap;
12 is      heap [I] = x;
 13 is      POS [x] = I; // POS list to record a [] x-th position in the stack 
14      the while (I> . 1 )
 15          IF (A [heap [I ]] <A [heap [I / 2 ]])
 16              {
 . 17                  the swap (heap [I], heap [I / 2 ]);
 18 is                  the swap (POS [heap [I]], POS [heap [I / 2 ] ]);
 . 19                  I >> = . 1 ;
 20 is              }
 21 is          the else  return ;
 22 is  }
 23 is inline intdel ( int X) // Here, when deleted, it is assigned only one maximum value, the stack stuffed lowermost position it ~ 
24  {
 25      int I = POS [X];
 26 is      A [X] = 1e12;
 27      the while (I * 2 <= lheap)
 28          {
 29              int P = I * 2 ;
 30              IF (A [heap [P + . 1 ]] <A [heap [P]]) P ++ ;
 31 is              IF heap (A [[P ]] < A [heap [I]])
 32                  {
 33 is                      the swap (heap [I], heap [P]);
 34 is                     swap(pos[ heap[i] ],pos[ heap[p] ]);
35                     i=p;               
36                 }
37             else break;
38         }
39 }
40 inline int pop()
41 {
42     int i=1,rt=heap[1];
43     heap[1]=heap[lheap--];
44     pos[ heap[1] ]=1;
45     while (i*2<=lheap)
46         {
47             int p=i*2;
48             if ( a[ heap[p+1] ]<a[ heap[p] ] ) p++;
49             if ( a[ heap[p] ]<a[ heap[i] ] )
50                 {
51                     swap(heap[i],heap[p]);
52                     swap(pos[ heap[i] ],pos[ heap[p] ]);
53                     i=p;
54                 }
55             else return rt;
56         }
57     return rt;
58 }
59 intmain ()
 60  {
 61 is      int I, J, K;
 62 is      CIN K >> >> >> n-DIS [ . 1 ];
 63 is      A [ 0 ] = A [n-] = 1e12; // boundary assigned a maximum value. .. (since a [] is the number of n-1, apparently the boundary n-) 
64      Point [ 0 ] [ . 1 ] = . 1 , Point [ 0 ] [ 0 ] = 0 , Point [n-] [ . 1 ] = n-, Point [n-] [ 0 ] = N- . 1 ; // explanations are explained ... 
65      Push ( 0 ), Push (n-);
 66      for (I = . 1 ; I <n-; I ++ )
67          {
 68              Scanf ( " % D " , & DIS [I + . 1 ]);
 69              A [I] = DIS [I + . 1 ] - DIS [I];
 70              Point [I] [ 0 ] = I- . 1 ;
 71 is              Point [ I] [ . 1 ] = I + . 1 ;
 72              Push (I);
 73 is          }
 74  
75      for (n-La =; K; K-- )
 76          {
 77              ; ANS = A + [now = pop ()] // after finished pop , first push, because the heap is expanding, that element remains in the pop heap 
78             Point = PREV [now] [ 0 ], = Next Point [now] [ . 1 ]; // last array, it can push the cover 
79              A [++ La] = A [PREV] + [Next] A - A [now];
 80  
81              Point [La] [ 0 ] = Point [PREV] [ 0 ];
 82              Point [Point [PREV] [ 0 ]] [ . 1 ] = La;
 83              Point [La] [ . 1 ] = Point [Next] [ . 1 ];
 84              Point [Point [Next] [ . 1 ]] [ 0 ] = La;
 85  
86              Push (La);// Push prior to del, 
87              del (PREV), del (Next);
 88          }
 89      COUT ANS << << endl;
 90 }
View Code

 

Guess you like

Origin www.cnblogs.com/Xchu/p/11355880.html