0% found this document useful (0 votes)
36 views

JCPC

The document provides information on various C++ STL containers and algorithms. It summarizes the key features and usage of containers like vector, deque, list, forward_list, pair, set, map, stack, queue, priority_queue. It also discusses concepts like recursion, two pointers, binary search, and cumulative sum. Code snippets and examples are given to demonstrate the usage of different containers, algorithms and concepts. Links to Codeforces problems and submissions applying these techniques are included.

Uploaded by

farooq tahsin
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

JCPC

The document provides information on various C++ STL containers and algorithms. It summarizes the key features and usage of containers like vector, deque, list, forward_list, pair, set, map, stack, queue, priority_queue. It also discusses concepts like recursion, two pointers, binary search, and cumulative sum. Code snippets and examples are given to demonstrate the usage of different containers, algorithms and concepts. Links to Codeforces problems and submissions applying these techniques are included.

Uploaded by

farooq tahsin
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

//cout << fixed << setprecision(12) << x;

***********************************************************************************
*************************************

<vector>

*You can add elements ONLY from back.

#include<vector>
vector<int>v(5);
vector<int>v1={1,2,3,4,5};
vector<int>v2(5,100);
v.push_back(50);
v.pop_back();
v.swap(v2);
v.front();
v.back();
v.size();
v.empty();
v.emplace(v.begin() + 1, 51);
v.erase(v.begin());
v.erase(v.begin() + 1, v.end() - 1);
v.resize(5);
v.shrink_to_fit();
sort(v.begin(), v.end());
sort(v.rbegin(), v.rend());
reverse(v.begin(), v.end());
*min_element(v.begin(), v.end());
*max_element(v.begin(), v.end());
for (auto i : v)
cout << i << " ";

https://codeforces.com/contest/998/submission/171269790

***********************************************************************************
*************************************

<deque>

// You can add elemnts from back OR front.

All functons in vector can use here.


#include<deque>
deque <int> d;
D.push_back(12);
D.push_front(9);
D.pop_back();
D.pop_front();

***********************************************************************************
*************************************

<list>

// You can add elemnts from ANY INDEX.


#include<list>
list<int>l = { 1,2,3,4,5,2,2,2 };
l.push_back(10);
l.push_front(0);
l.pop_back();
l.pop_front();
cout << l.back() << " " << l.front();
l.remove(2);
l.erase(l.begin());
auto it = l.begin();
it++;
l.erase(it, l.end());
l.insert(it, 87);
l.assign(5, 2);// 2 2 2 2 2
l.size();
l.empty();
l.resize(5);
l.clear();
list<int> l2 = { 4,2,8,1,3 };
l.merge(l2); // merge+sort+clear(l2)
l.splice(l.begin(), l2); // ‫انسخ الباراميتر التاني في المكان اللي حددتو بالباراميتر االول‬
l.unique(); // remove duplicate elements
l.remove_if([](int n) {return n % 2 == 0; });
l.sort();
l.sort(greater<int>());
l.reverse();

***********************************************************************************
*************************************

<forward_list>

// NOT IMPORTATNT (You can add elements ONLY from front).

#include<forward_list>
forward_list<int> FL={ 1,2,3 };
FL.assign({ 5,4,6 });
FL.push_front(9);
FL.pop_front();
FL.remove(4);

***********************************************************************************
*************************************

<pair>

#include<utility>
pair <int, int>P;
cin >> P.first >> P.second;
cout << P.first<<" "<<P.second;
pair<string, pair<int, bool>>PP;
PP.first = "Farooq";
PP.second.first = 123;
PP.second.second = true;
vector<pair<int, int>>VP;
VP.push_back({{ 123,321 });
cout << VP[0].first << " " << VP[0].second;
https://codeforces.com/contest/230/submission/171271549

***********************************************************************************
*************************************

<set>

// cant change the value of numbers in SET (READ ONLY)

#include<set>
set<int>S{ 2,1,0,5,2,1 }; // sort & uniqe
for (auto i : S)cout << i<<" "; //0 1 2 5
S.empty();
S.size();
S.insert(3); cout << endl; // 0 1 2 3 5
S.erase(1);
S.swap(s2);
S.clear();
S.count(5); // if S contain 5 return 1 else return 0
set<int,greater<int>>S{ 2,1,0,5,2,1 }; // 5 2 1 0
multiset<int>MS{ 5,9,3,5,6,2,5 }; // sort ONLY
for (auto i : MS)cout << i << " "; // 2 3 5 5 5 6
MS.count(5); // 3

***********************************************************************************
*************************************

<map>

#include<map>
map<int, string>M{ {12547,"FA"},{1234,"RA"},{1254,"QA"} };
M[5547] = "AH";
for (auto i : M)
cout << i.first << " " << i.second << endl; //sorted
M.count(1234); //1
M.insert({ 40,"sq" });
M.empty();
M.swap(M2);
M.clear();
M.size();
multimap<string, string>MM; // accept duplicate
MM.insert({ "Ahmad","SE" });
MM.insert({ "Ahmad","CS" });
MM.insert({ "Ahmad","CIS" });
MM.count("Ahmad");//3

https://codeforces.com/contest/785/my
https://codeforces.com/contest/499/my

***********************************************************************************
***********************************
//frequency array

map<int, int>m;
int n; cin >> n;
for (int i = 0; i < n; i++) {
int x; cin >> x;
m[x]++;
}
for (auto i : m)cout << i.first << " " << i.second << endl;

https://codeforces.com/contest/1025/my

*IMPORTANT*
https://codeforces.com/contest/227/submission/171617113

***********************************************************************************
***********************************

#include<unordered_set>
#include<unordered_map>
unordered_set<string>us;
unordered_multiset<string>uss;
unordered_map<int, int >um;
unordered_multimap<int, int >umm;*/

***********************************************************************************
***********************************

<stack>

// Last-in-first-out

#include <stack>
stack<string>s;
s.push("a");
s.push("b");
s.push("c");
s.push("d");
s.emplace("P");
s.pop();
//cout << s.top();
for (int i = 0; !s.empty(); i++) {
cout << s.top() << " ";
s.pop();
}

https://codeforces.com/contest/81/submission/172052151

***********************************************************************************
*************************************

<queue>

//first-in-first-out

#include<queue>
queue<int>q;
q.push(60);
q.push(50);
q.push(40);
cout << q.front(); // 60
cout << q.back(); // 40
q.pop();
cout << q.front(); // 50
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}

https://codeforces.com/contest/96/submission/172137303

***********************************************************************************
*************************************

<priority_queue>

#include<queue>
priority_queue<int>pq;
pq.push(50);
pq.push(70);
pq.push(80);
pq.push(60);
cout << pq.top(); // 80
pq.pop();
cout << pq.top(); // 70
while (pq.size() != 0) {
cout << pq.top() << " ";
pq.pop();
}

https://codeforces.com/contest/218/submission/172140933

***********************************************************************************
*************************************
<cumulative sum>

#include <bits/stdc++.h>
using namespace std;
int arr[105], cum[105];
int main() {
int n, m; cin >> n >> m; // 7 2
for (int i = 1; i <= n; i++) {
cin >> arr[i]; // 17 3 -1 5 2 11 -4
cum[i] = cum[i - 1] + arr[i];
cout << cum[i] << " "; // 17 20 19 24 26 37 33
}
cout << endl;
while (m--) {
int left, right; cin >> left >> right; // 2 4 // 1 5
cout << cum[right] - cum[left - 1] << endl; // 7 // 26
}
return 0;
}

https://codeforces.com/contest/18/submission/172290538
https://codeforces.com/contest/433/submission/171827988
https://codeforces.com/contest/313/submission/172292977

cumulatiive sum 2D array


https://codeforces.com/group/abwn7HbDuu/contest/215627/my
***********************************************************************************
*************************************

<Recursion>

int fact(int n) {
if (n == 0)return 1;
return n * fact(n - 1);
}
int fib(int n) {
if (n == 1 || n == 2) return 1;
return fib(n - 1) + fib(n - 2);
}

https://codeforces.com/contest/96/submission/172365215
https://codeforces.com/group/dfmvO0RN41/contest/227937/submission/172368378
https://codeforces.com/gym/100989/submission/172398983
https://codeforces.com/group/dfmvO0RN41/contest/227937/submission/172485078

***********************************************************************************
*************************************

< Two Pointers >

int n, sum; cin >> n >> sum;


int arr[n]; // sorted
for (int i = 0; i < n; i++)
cin >> arr[i]; // 2 4 5 7 8 20
int l = 0 , r = n - 1; // two pointers
while (l < r) {
if (arr[l] + arr[r] == sum) {
cout << "YES";
return 0;
}
else if (arr[l] + arr[r] > sum) r--;
else l++;
}
cout << "NO";

-----------------------------------------------------------

int n, k; cin >> n >> k; // 9 4


int a[n];
for (int i = 0; i < n; i++)
cin >> a[i]; // 5 2 4 3 -1 9 3 7 6
long long sum = 0, mx = 0;
for (int i = 0; i < k; i++)
sum += a[i]; // 14
for (int i = k; i < n; i++) {
sum -= a[i - k];
sum += a[i];
mx = max(sum, mx);
}
cout << mx;
https://codeforces.com/contest/279/my
https://codeforces.com/contest/676/submission/172504296
https://codeforces.com/contest/580/submission/172569817

***********************************************************************************
*************************************

#include<stdio.h>

cout<<"n= "<<n; ==> printf("n= %d",n);


cin>>n>>x; ==> scanf("%d%d",&n,&x);

***********************************************************************************
*************************************

< Binary Search >

https://codeforces.com/gym/101257/submission/172748001
https://codeforces.com/contest/670/problem/D2

-------------------------------------------------------

cout << lower_bound(a, a + a.size(), x) - a; // first (num >= x)


cout << upper_bound(a, a + a.size(), x) - a; // first (num > x)

***********************************************************************************
*************************************

< Ternary Search >

***********************************************************************************
*************************************

< Graph >

-Adjacency List

-Adjacency Matrix

*Graph representaion
https://codeforces.com/contest/580/submission/173086911

*DFS
https://codeforces.com/group/qAGblhphAA/contest/227610/submission/173089319

*Grid Traversing
https://codeforces.com/contest/378/submission/173234399

*BFS
https://codeforces.com/group/qAGblhphAA/contest/227610/submission/173237341
***********************************************************************************
*************************************

You might also like