1. Map
Key can correspond to only one ing value. It supports insertion, deletion, search (count, find), modification (delete this value first, and then insert the modified value), sorting, ing (discrete) multimap the same key value can have multiple ing
View code
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <map>
using namespace std;
map<int, int>mp;
int main( )
{
int N,M, t;
char str[1000];
while( scanf("%d%d",&N,&M) != EOF )
{
mp.clear( );
for( int i = 1; i <= N; i++)
{
scanf("%d",&t);
mp[t]++;
}
for( int i = 1; i <= M; i++)
{
scanf("%s",str);
if( str[0] == 'D' )
scanf("%d",&t),mp.erase(t);
else if( str[0] == 'F')
{
scanf("%d",&t);
if( mp.find(t) != mp.end( ))
puts("1");
else
puts("-1");
}
else if( str[0] == 'P')
{
map<int,int>::iterator iter = mp.begin( );
printf("%d",iter->first);
for( int i = 1; i < iter->second; i++)
printf(" %d",iter->first);
while( iter++ != mp.end( ))
{
for( int i = 1; i <= iter->second; i++)
printf(" %d",iter->first);
}
puts("");
}
}
}
return 0;
}
2. Set/Multiset
A set is a container. The values of the elements contained in a set are unique. This is useful when collecting a specific value of data. Elements in the set are arranged in a certain order and used as instances in the set. A collection is organized by a linked list. The insert and delete operations are faster than the vector operations, but the query or addition of elements at the end is somewhat slow. The specific implementation adopts the data structure of the Binary Tree of the red and black trees. (Baidu encyclopedia)
The element values contained by Multiset are not unique.
View code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <set>
using namespace std;
multiset<int>p;
int main( )
{
int N,M, t;
char str[1000];
while( scanf("%d%d",&N,&M) != EOF )
{
p.clear( );
for( int i = 1; i <= N; i++)
{
scanf("%d",&t);
p.insert( t );
}
for( int i = 1; i <= M; i++)
{
scanf("%s",str);
if( str[0] == 'D' )
scanf("%d",&t),p.erase(t);
else if( str[0] == 'F')
{
scanf("%d",&t);
if( p.find(t) != p.end( ))
puts("1");
else
puts("-1");
}
else if( str[0] == 'P')
{
multiset<int>::iterator iter = p.begin( );
printf("%d",*iter);
while( ++iter != p.end( ))
{
printf(" %d",*iter);
}
puts("");
}
}
}
return 0;
}
3. hnoi 2004
Powerful set and lower_bound applications
View code
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <set>
# Include <algorithm>
Using namespace STD;
# Define mod 1000000.
Set <int> P;
Typedef set <int >:: iterator it;
Const int INF = ~ 0u> 2;
// Const int INF = 0x7f7f7f7f;
/* An error is returned when the value is assigned. You can change the value to a larger or smaller value.
The reason may be that the inserted pet will be deleted when its feature value is set to this.
*/
Int main ()
{
Int N, A, B, C, sum = 0;
Scanf ("% d", & N );
P. insert (INF), P. insert (-INF );
While (n --)
{
Scanf ("% d", & A, & B );
If (P. Size () = 2)
P. insert (B), c =;
Else if (a = C)
P. insert (B );
Else
{
It x = P. lower_bound (B );
Int r = * X-B;
Int L = B-* (-- X );
If (L <= r)
Sum + = L, P. Erase (X );
Else
Sum + = r, P. Erase (++ X );
If (sum> = mod)
Sum % = MOD;
}
}
Printf ("% d \ n", sum );
Return 0;
}