Codevs1052 Hamster Game

Source: Internet
Author: User

    • The main topic: From time to time there are n hamsters appear, each hamster has its stay times and scores, ask the most can get how much score.

    • Ideas:

      • 1. Place the hamster in chronological order of occurrence time.
      • 2. According to the occurrence time binate to after traversal, will appear the hamster's score joins to a minimum heap, each time to view the current hamster's t[i] and the current time ti satisfies Ti<=t[i]. If Ti<=t[i], the current hamster score is added to the minimum heap, ti++, that is, to enter the next if Ti>t[i], because ti every +1, so there must be a ti=t[i]+1, so at this time to compare the first ti time the smallest score and the current score, that is, the smallest heap of heap top, If the current score is greater than this minimum score, the heap is out of the heap, the current score is pressed into the heap, that is, to maintain the pre-ti moment of the score and maximum, if the current score is less than equal to this minimum score, it is not processed. At this point ti does not change.
      • 3. Finally, the score in the minimum heap is counted and is the maximum score.
    • Note: When the hammer goes down, 1s means that if the vanishing time is at the end of the 1th second, you need to hit 0 seconds at the latest.

    • The code is as follows:

#include <iostream>#include <cstdio>#include <queue>#include <algorithm>structnode{int value, Tim; Node (int value=0,inttim=0):value(value), Tim (Tim) {}BOOL operator< (ConstNode &a)Const{returntim>a.tim; }};structmousetype{int value, Tim; Mousetype (int value=0,inttim=0):value(value), Tim (Tim) {}BOOL operator< (ConstMousetype &a)Const{return value>a.value; }};usingnamespace Std;Const intmaxn= the;intNvalue[Maxn],tim[maxn];mousetype MOUSE[MAXN];voidInit () {priority_queue<node> q; scanf"%d", &n); for(intI=1; i<=n;++i) scanf ("%d", &tim[i]); for(intI=1; i<=n;++i) scanf ("%d",&value[i]); for(intI=1; i<=n;++i) Q.push (node (value[I],tim[i]));intCnt=0; while(!q.empty ())        {node tmp=q.top ();        Q.pop (); MOUSE[++CNT].value=tmp.value;    Mouse[cnt].tim=tmp.tim; }}voidWork () {priority_queue<mousetype> q;intt=0; for(intI=1; i<=n;++i) {if(mouse[i].tim>t)            {Q.push (mouse[i]);        t++; }Else{Mousetype tmp=q.top ();if(TMP.value<mouse[i].value) {Q.pop ();            Q.push (Mouse[i]); }        }    }intans=0; while(!q.empty ()) {Ans+=q.top ().value;    Q.pop (); } printf ("%d", ans);}intMain () {init (); Work ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codevs1052 Hamster Game

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.