"Map discretization + play table" UVA 11995 I Can Guess the Data structure!

Source: Internet
Author: User

"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
    • 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.

"Map discretization + play table" UVA 11995 I Can Guess the Data structure!

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.