UVA 11491 erasing and Winning prize value (greedy)

Source: Internet
Author: User

Test instructions: give you an n-bit integer, let you delete the D number, the remaining number should be as large as possible.

Solution: Because the last digit number is determined, and the low number of the answer contribution is not necessarily high number, so preferred to choose the largest and most on the left of the number, but there is a limit, choose this number after the left of the number to ensure enough next choice, so I think of the priority queue, record a message, The position of the selected number, and the position of the previous number, if the current number of the outgoing team is directly discarded in front of the last selected position, the number of the remaining selected after each selection is reduced, the number of conditions that meet the limit increases, and the new number to be selected is added to the queue.

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e5+5;structdig{intVal,pos; BOOL operator< (ConstDig & RHS)Const {        returnVal < Rhs.val | | (val = = Rhs.val && pos >Rhs.pos); }}D[MAXN];p riority_queue<dig>Q;intn,d;voidSovle () { while(Q.size ()) Q.pop (); inti;  for(i =0; I <= D; i++) {Q.push (d[i]); }    intNeed = N-d,pre =-1;  while(need) { while(Q.top () .pos<pre) Q.pop (); ConstDig &u =Q.top (); if(U.pos = = Nneed) {             for(i = u.pos; i < n; i++) printf ("%d", D[i].val);  Break; } Pre=U.pos; printf ("%d", U.val); Need--; Q.pop (); Q.push (D[i++]); } Putchar ('\ n');}intMain () {//freopen ("In.txt", "R", stdin);     while(SCANF ("%d%d",&n,&d), N) { for(inti =0; I < n; i++) {scanf ("%1d",&d[i].val); D[i].pos=i;    } sovle (); }    return 0;}

UVA 11491 erasing and Winning prize value (greedy)

Related Article

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.