"Multiset" Hdu 5349 MZL ' s simple problem

Source: Internet
Author: User

"Multiset" Hdu 5349 MZL ' s simple problem topic link: hdu 5349 MZL ' s simple problem topic

n operations, inserting elements, deleting the smallest elements, querying the largest elements, and outputting.


The use of C++stl's multiset

set--multivariate sets (elements are not repeatable), multiset--multiple sets of repeatable elements
A multivariate set (multisets) is similar to a set (sets), except that it supports duplicate objects. (Refer to set container for specific usage)
The set and Multiset are internally implemented with a balanced binary tree:

As can be seen from the internal data structure, its middle order traversal is an ordered sequence, the first element is the minimum value, the last element is the maximum value, and all the STL operation Complexity is O (LOGN)

List of action functions:

Begin () returns the iterator that points to the first element
Clear () Clears all elements
COUNT () returns the number of elements that point to a value
Empty () returns True if the collection is null
End () returns an iterator pointing to the last element
Equal_range () returns two iterators in the collection with the upper and lower bounds equal to the given value
Erase () Delete elements in the collection
Find () returns an iterator that points to the element being found
Get_allocator () returns the allocator of a multivariate set
Insert () Inserts an element into the collection
Key_comp () returns a function for comparing values between elements
Lower_bound () returns an iterator that points to the first element greater than (or equal to) a value
Max_size () returns the maximum limit of the elements that the collection can hold
Rbegin () returns a reverse iterator that points to the last element in a multivariate collection
Rend () returns a reverse iterator that points to the first element in a multivariate collection
Size () Number of elements in a multivariate collection
Swap () Swap two multivariate set variables
Upper_bound () returns an iterator that is greater than a value element
Value_comp () returns a function to compare the values between elements

Talk about reverse iterators

A reverse iterator is an iterator that traverses a container in reverse. In other words, the container is traversed from the last element to the first element. The inverse iterator will reverse the meaning of the increment (and decrement): For a reverse iterator, the + + operation accesses the previous element, while the-operation accesses the next element.

//反向迭代器的声明multiset<int>::reverse_iterator It;//区别一般迭代器multiset<int>::iterator it;

The "plot" takes the vector container's V.begin (), V.end () and V.rbegin (), V.rend () as an example, to see where they point is clear!

Talk about the idea.

Skilled multiset Insert (), Erase (), Rbegin () can be used, note that rbegin () returns a reverse iterator, to be declared well in advance; erase (iterator) the incoming parameter is an iterator;

Reference Code
/*====================================*|* STL's multiset *|\*====================================*/ /*author:hacker_vision*/#include <bits/stdc++.h>#define CLR (k,v) memset (k,v,sizeof (k) )using namespace  STD;Const int_max =2e4+Ten;intN,x,op;//op operation number; multiset<int>Ms multiset<int>:: Iterator it; multiset<int>:: Reverse_iterator It;intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge    while(scanf("%d", &n) = =1) {ms.clear (); for(inti =0; I < n; + + i) {scanf("%d", &op);if(op==1){scanf("%d", &x);        Ms.insert (x); }Else if(op==2) {it = Ms.begin ();//it points to the smallest element in Multiset (first)            if(Ms.size ()) ms.erase (it);//.erase (itetator it) parameter count iterator}Else{It = Ms.rbegin ();//it pointing to the largest element in Multiset (last)            if(Ms.size ())printf("%d\n", *it);Else puts("0"); }    }  }return 0;}
    • Bold Ctrl + B
    • Italic Body Ctrl + I
    • ReferenceCtrl + Q
    • Insert LinkCtrl + L
    • Inserting codeCtrl + K
    • Insert PictureCtrl + G
    • Promote titleCtrl + H
    • Ordered listCtrl + O
    • Unordered listCtrl + U
    • LineCtrl + R
    • RevokeCtrl + Z
    • RedoCtrl + Y

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

"Multiset" Hdu 5349 MZL ' s simple problem

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.