"Map discretization + play table" UVA 11995 I Can Guess the Data structure!map associative container: Ordered + map, find complexity O (NLOGN) topic
Give you an array of n numbers, and find the subscript value of the K-th occurrence (subscript starting from 1)
–
Talk about the idea of the problem is obviously to play the table pre-processing, the key is how to play this table 1. First we observed that V is very large, open a two-dimensional array data[v][k] must not be stored, so with map discretization (automatic ordinal numbering, avoid space waste) 2.data[v] must be a variable-length array, Storage data v appears 0, 1, 2 times ... The subscript value, think of vector< int >
In this way, the complexity of preprocessing each element O (logn), the total preprocessing time complexity O (NLOGN), each query O (logn), so the single-ratio o (n*m) complexity is greatly reduced
Comparison: data[] [] with vector< int >child[] and map< int,vector< int > >
0 Dynamic 1 Dynamic 2 dynamic
Reference Code
#include <bits/stdc++.h>using namespace STD;Const int_max =1e3+Ten;intN,m,v,k; map<int,vector<int> >mp///last two >> do not ligatures, will be mistaken for bit arithmetic >>intMain () {#ifndef Online_judgeFreopen ("Input.txt","R", stdin);#endif //Online_judge while(scanf("%d%d", &n,&m) = =2) {mp.clear (); for(inti =0; I < n; + + i) {scanf("%d", &v); Mp[v].push_back (i +1);//Complexity O (NLOGN)} for(inti =0; I < m; + + i) {scanf("%d%d", &k,&v);if(Mp[v].size () < K)puts("0");Else printf("%d\n", mp[v][k-1]); } }return 0;}
- Bold
Ctrl + B
- Italic Body
Ctrl + I
- Reference
Ctrl + Q
- Insert Link
Ctrl + L
- Inserting code
Ctrl + K
- Insert Picture
Ctrl + G
- Promote title
Ctrl + H
- Ordered list
Ctrl + O
- Unordered list
Ctrl + U
- Line
Ctrl + R
- Revoke
Ctrl + Z
- Redo
Ctrl + Y
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Map discretization + play table" UVA 11995 I Can Guess the Data structure!