unordered_map sometimes better than map:
got TLE with map on Collecting_Numbers_II.cpp but AC with unordered_map -unordered is good for a lot of requests and a lot of data
This commit is contained in:
19
CSES - CSES Problem Set/Collecting_Numbers.cpp
Normal file
19
CSES - CSES Problem Set/Collecting_Numbers.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
// Collecting Numbers
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int n; cin >> n;
|
||||
vector<int> in(n);
|
||||
for (auto &e: in) cin >> e;
|
||||
int count = 1;
|
||||
map<int, int> pos;
|
||||
for (int i{}; i < n; i++) pos[in[i]] = i;
|
||||
for (int i = 1; i < n; i++) {
|
||||
if (pos[i] > pos[i+1]) count ++;
|
||||
}
|
||||
cout << count << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user