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;
|
||||||
|
}
|
||||||
39
CSES - CSES Problem Set/Collecting_Numbers_II.cpp
Normal file
39
CSES - CSES Problem Set/Collecting_Numbers_II.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
// Collecting Numbers II
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n; cin >> n;
|
||||||
|
int m; cin >> m;
|
||||||
|
vector<int> in(n);
|
||||||
|
for (auto &e: in) cin >> e;
|
||||||
|
int count = 1;
|
||||||
|
unordered_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;
|
||||||
|
for (int i{}; i < m; i++) {
|
||||||
|
int a, b; cin >> a >> b;
|
||||||
|
|
||||||
|
|
||||||
|
if (in[a - 1] > 1 && pos[in[a - 1] - 1] > a - 1 && pos[in[a - 1] - 1] <= b - 1) count--;
|
||||||
|
if (in[a - 1] > 1 && pos[in[a - 1] - 1] < a - 1 && pos[in[a - 1] - 1] >= b - 1) count++;
|
||||||
|
if (in[a - 1] < n && pos[in[a - 1] + 1] > a - 1 && pos[in[a - 1] + 1] <= b - 1) count++; //
|
||||||
|
if (in[a - 1] < n && pos[in[a - 1] + 1] < a - 1 && pos[in[a - 1] + 1] >= b - 1) count--;
|
||||||
|
|
||||||
|
if (in[b - 1] > 1 && pos[in[b - 1] - 1] > a - 1 && pos[in[b - 1] - 1] < b - 1) count++;
|
||||||
|
if (in[b - 1] > 1 && pos[in[b - 1] - 1] < a - 1 && pos[in[b - 1] - 1] > b - 1) count--;
|
||||||
|
if (in[b - 1] < n && pos[in[b - 1] + 1] > a - 1 && pos[in[b - 1] + 1] < b - 1) count--;
|
||||||
|
if (in[b - 1] < n && pos[in[b - 1] + 1] < a - 1 && pos[in[b - 1] + 1] > b - 1) count++;
|
||||||
|
|
||||||
|
pos[in[a - 1]] = b - 1;
|
||||||
|
pos[in[b - 1]] = a - 1;
|
||||||
|
swap(in[a-1], in[b-1]);
|
||||||
|
cout << count << endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
27
CSES - CSES Problem Set/Missing_Coin_Sum.cpp
Normal file
27
CSES - CSES Problem Set/Missing_Coin_Sum.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// Missing Coin Sum
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n; cin >> n;
|
||||||
|
vector<ll> coins(n);
|
||||||
|
for (auto &e: coins) cin >> e;
|
||||||
|
std::sort(coins.begin(), coins.end());
|
||||||
|
|
||||||
|
ll sum = 0;
|
||||||
|
|
||||||
|
for (int i{}; i < n; i++) {
|
||||||
|
if (coins[i] - 1 > sum) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
sum += coins[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << sum + 1 << endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
24
CSES - CSES Problem Set/Stick_Lengths.cpp
Normal file
24
CSES - CSES Problem Set/Stick_Lengths.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// Stick Lengths
|
||||||
|
|
||||||
|
#include<bits/stdc++.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int n; cin >> n;
|
||||||
|
vector<ll> in(n);
|
||||||
|
for (auto &e: in) cin >> e;
|
||||||
|
std::sort(in.begin(), in.end());
|
||||||
|
ll target = in[n/2];
|
||||||
|
ll target2 = in[n/2 + 1];
|
||||||
|
ll count = 0, count2 = 0;
|
||||||
|
for (auto e: in) {
|
||||||
|
count += abs(target - e);
|
||||||
|
count2 += abs(target2 - e);
|
||||||
|
}
|
||||||
|
cout << min(count, count2) << endl;
|
||||||
|
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user