HDU 4398 Template Library Management (greedy, STL)

Source: Internet
Author: User
Template Library Management

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 298 Accepted Submission (s): 87

Problem DescriptionAs an experienced ACMer, you must have known the importance of "code template library ". with the help of pre-printed code library, you can implement the complicated algorithms correctly and efficiently. however, the size of the library is strictly limited during the contest. for example, you can only take at most 25 pages of printed codes in World Finals. so you must choose carefully which code template shocould be encoded ded.
Now your team is participating a programming contest whose rules are slightly different from ICPC. this contest consists of N problems, and you must solved them in order: Before you solve the (I + 1) th problem, you must solve the ith problem at first. and solving the ith problem requires a specified code template Ti.
You are allowed to hold M code templates only. at the beginning of the contest, your are holding templates numbered 1, 2 ,..., m. during the contest, if the problem you are trying to solve requires code template Ti, and Ti is happened at your hand (I. e, one of the M code templates you are holding is Ti), you can solve it immediately. on the other hand, if you are not holding Ti, you must call your friends who are outside the arena for help (yes, it is permitted, not cheating ). they can give you the code template you need. because you are only allowed to hold M code templates, after solving current problem, you must choose to drop the code you get from your friends just now, or to keep it and drop one of the M templates at your hand previusly.
Given the problem sequence in the contest and the limitation M, you want finish all the problems with minimum number of calling your friends.

 

InputThe first line of each test case contains two numbers N (1 <= N <= 100000) and M (1 <= M <= 100000 ). the second line contains N numbers T1, T2 ,..., TN (1 <= Ti <= 109), indicating the code templates required by each problem.

 

OutputOutput one line for each test case, indicating the minimum number of calling friends for help.

 

Sample Input4 3 1 2 3 4 11 3 4 1 2 5 3 4 1 2 3

 

Sample Output1 4

 

AuthorRoBa

 

Source2012 Multi-University Training Contest 10

 

Recommendzhuyuanchen520 greedy. Optimal Scheduling Algorithm. If a template T is to be eliminated, the farthest one from the next T coordinate will be eliminated. This program is implemented using STL, eliminating the need for discretization and other operations, and the code is relatively simple to write. It seems that mastering STL can get twice the result with half the effort.
/* HDU 4398G ++ 156 ms 2868K greedy, maintains a set of M elements, and selects the next position based on the element at the current position, delete the farthest element in the next position */# include <stdio. h> # include <iostream> # include <string. h >#include <algorithm> # include <queue> # include <map> # include <set> using namespace std; const int MAXN = 100010; int Ti [MAXN]; int next [MAXN]; map <int, int> mp; struct Node {int next_id; int ti ;}; struct classcomp {bool operator () (const Node &, const Node & B) const {return. next_id <B. next_id; // Sorting from small to large }}; // do not forget the multiset <Node, classcomp> T_info; multiset <Node >:: iterator it_n; set <int> Te; set <int>: iterator it; int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt ", "w", stdout); int n, m; while (scanf ("% d", & n, & m) = 2) {for (int I = 1; I <= n; I ++) scanf ("% d", & Ti [I]); mp. clear (); // clear map for (int I = n; I> = 1; I --) // scan from the back to the front {if (mp [Ti [I]) // next [I] = mp [Ti [I]; else next [I] = n + 1; \ mp [Ti [I] = I ;} Te. clear (); T_info.clear (); for (int I = 1; I <= m; I ++) // first input the m templates in front of the set {if (! Mp [I]) mp [I] = n + 1; Node temp; temp. next_id = mp [I]; temp. ti = I; T_info.insert (temp); Te. insert (I) ;}int ans = 0; for (int I = 1; I <= n; I ++) {it = Te. find (Ti [I]); if (it! = Te. end () {Node temp; temp. next_id = I; temp. ti = Ti [I]; T_info.erase (temp); temp. next_id = next [I]; // update T_info.insert (temp);} else {ans ++; it_n = T_info.end (); it_n --; if (next [I] <(* it_n ). next_id) {Te. erase (* it_n ). ti); T_info.erase (it_n); Te. insert (Ti [I]); Node temp; temp. next_id = next [I]; temp. ti = Ti [I]; T_info.insert (temp) ;}} printf ("% d \ n", ans);} 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.