The optimum range for solving HUT-XXXX digital games

Source: Internet
Author: User

This question is greedy, but here is a bit of mathematical thinking.

For the n-digit to take off K bit, then we leave is the N-K bit, we may wish to set t = n-k; so we choose the first digit after the number of T-1 let us choose, so the first number of selection range is
[1, N-T + 1], when we select the first number (assuming the position is P), we select the range of the second number is [p + 1, N-T-2, and so on. Because we want to obtain the maximum number, we select the range each time.
Use the line segment tree for optimization.

CodeAs follows:

# Include <cstdlib> # Include <Cstring> # Include <Cstdio> # Include <Algorithm> # Include <Queue> Using   Namespace  STD;  Int N, K, leave;  Char S [ 500005  ];  Struct  Node {  Int  L, R, POs, Max;} e [  2000005  ]; Vector < Int > V;  Void Push_up ( Int  P ){  If (E [P < 1 ]. Max> = E [P < 1 | 1  ]. Max) {e [p]. Max = E [P < 1  ]. Max; E [p]. Pos = E [P < 1  ]. Pos ;}  Else  {E [p]. Max = E [P < 1 | 1  ]. Max; E [p]. Pos = E [P <1 | 1  ]. Pos ;}}  Void Build ( Int P, Int L, Int  R) {e [p]. L = L, E [p]. r = R;  If (L! = R ){  Int Mid = (L + r)> 1  ; Build (P <1  , L, mid); Build (P < 1 | 1 , Mid + 1  , R); push_up (p );}  Else  {E [p]. Pos = L, E [p]. max = s [l]- '  0  '  ;  //  Initialization  }} Void Query ( Int P, Int L, Int R, Int & Rec, Int & Pos ){  If (L = E [p]. L & R = E [p]. R ){  If (REC! = E [p]. max ){  If (REC < E [p]. max) {rec = E [p]. Max; POS = E [p]. Pos ;}}  Else   If (E [p]. Pos < Pos) {pos = E [p]. Pos ;}  Return  ;}  Int Mid = (E [p]. L + E [p]. R)> 1  ;  If (R <= Mid ){ Return Query (P < 1  , L, R, REC, POS );}  Else   If (L> Mid ){  Return Query (P < 1 | 1  , L, R, REC, POS );}  Else  {Query (P < 1  , L, mid, REC, POS); query (P < 1 | 1 , Mid + 1  , R, REC, POS );}}  Int  Main (){  Int  POs, REC;  While (Scanf ( "  % D  " , & N, & K) = 2  ) {V. Clear (); POS =0  ; Scanf (  "  % S  " , S + 1 ); //  From the beginning Build ( 1 , 1  , N); leave = N-k; //  Number of remaining digits to be deleted          While (Leave ){ // Select the number of digits Rec =- 1  ; Query (  1 , POS + 1 , N-leave + 1  , REC, POS); V. push_back (REC ); -- Leave ;}  For ( Int I = 0 ; I! = ( Int ) V. Size (); ++ I) {printf ( "  % D  "  , V [I]);} puts (  ""  );}  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.