Applied algorithm solution
Applied algorithm solution
CONTENTS
• Introduction
• Standard Template Library (STL) in C++
APPLIED ALGORITHMS
General introduction
Data structure library
3 4
INTRODUCTION INTRODUCTION
5 6
• Given 2 integers a and b, calculate sum of them. • Given 2 integers a and b, calculate sum of them.
• Data • Data
• Line 1 consists of 2 integers a and b (0 <= a, b <= 1019) • One line consists of 2 integers a and b (0 <= a, b <= 1019)
• Result • Result
• Write a number which is them sum of a and b • Write a number which is them sum of a and b
#include <bits/stdc++.h>
using namespace std;
Stdin Stdout
int main(){
35 8
int a,b;
cin >> a >> b;
int res = a + b;
cout << res;
return 0;
}
7 8
INTRODUCTION - Example (P.01.01.01) INTRODUCTION - Example (P.01.01.01)
• Given 2 integers a and b, calculate sum of them. • Given 2 integers a and b, calculate sum of them.
• Data • Data
• One line consists of 2 integers a and b (0 <= a, b <= 1019) • One line consists of 2 integers a and b (0 <= a, b <= 1019)
• Result • Result
• Write a number which is them sum of a and b • Write a number which is them sum of a and b
#include <bits/stdc++.h> #include <bits/stdc++.h>
using namespace std; using namespace std;
int main(){ int main(){
int a,b; unsigned long long a,b;
cin >> a >> b; cin >> a >> b;
int res = a + b; unsigned long long res = a + b;
cout << res;
Overflow when a and cout << res;
return 0;
b are large numbers return 0;
} }
9 10
• Given 2 integers a and b, calculate sum of them. • Given 2 integers a and b, calculate sum of them.
• Data • Data
• One line consists of 2 integers a and b (0 <= a, b <= 1019) • One line consists of 2 integers a and b (0 <= a, b <= 1019)
#include <bits/stdc++.h>
• Result • Result
using namespace std;
• Write a number which is them sum of a and b • Write a number which is them sum of a and b
int main(){
#include <bits/stdc++.h> unsigned long long a,b, a1, b1,a2,b2;
using namespace std; cin >> a >> b;
int main(){ a1=a/10; b1=b/10;
unsigned long long a,b; a2 = a%10; b2 = b%10;
cin >> a >> b; unsigned long long c1 = a1+b1+(a2+b2)/10;
a = 10000000000000000000, b = 10000000000000000000 unsigned long long res = a + b; unsigned long long c2 = (a2+b2)%10;
res = 1553255926290448384 (still overflow)
cout << res; if (c1 > 0) cout << c1 << c2;
return 0; else cout << c2;
}
SOLVED!!
return 0;
}
11 12
Standard Template Library (STL) in C++ Standard Template Library (STL) in C++
13 14
Standard Template Library (STL) in C++ - List Standard Template Library (STL) in C++ - List
15 16
Standard Template Library (STL) in C++ - String Standard Template Library (STL) in C++ - Stack
17 18
Standard Template Library (STL) in C++ - Queue Standard Template Library (STL) in C++ - Set
19 20
Standard Template Library (STL) in C++ - Set Standard Template Library (STL) in C++ - Set
• Store elements without repeating values • Store elements without repeating values
• Operations: add, remove, search • Operations: add, remove, search
21 22
Standard Template Library (STL) in C++ - Set Standard Template Library (STL) in C++ - Set
• The set structure in C++ provides the function upper_bound(k): returns a pointer to the smallest • The set structure in C++ provides the function upper_bound(k): returns a pointer to the smallest
element that is greater than k in the set. If k is greater than or equal to the largest element, the element that is greater than k in the set. If k is greater than or equal to the largest element, the
function returns a pointer to the position after the last element of the set function returns a pointer to the position after the last element of the set
#include <bits/stdc++.h>
#include <bits/stdc++.h> upper_bound(3) = 4 lower_bound(3) = 4
using namespace std;
using namespace std; upper_bound(4) = 6 lower_bound(4) = 4
int main() {
int main() { no upper_bound of 10 no lower_bound of 11
set<int> S;
set<int> S;
for(int v = 1; v <= 5; v++) S.insert(2*v);
for(int v = 1; v <= 5; v++) S.insert(2*v); set<int>::iterator p = S.lower_bound(3);
set<int>::iterator p = S.upper_bound(3); cout << "lower_bound(3) = " << *p << endl;
cout << "upper_bound(3) = " << *p << endl; p = S.lower_bound(4);
p = S.upper_bound(4); cout << "lower_bound(4) = " << *p << endl;
cout << "upper_bound(4) = " << *p << endl; p = S.lower_bound(11);
p = S.upper_bound(10); if(p == S.end()) cout << "no lower_bound of 11" << endl;
if(p == S.end()) cout << "no upper_bound of 10" << endl; else cout << "lower_bound of 11 = " << *p << endl;
} }
23 24
Standard Template Library (STL) in C++ - Map Standard Template Library (STL) in C++ - Priority Queue
• A data structure that stores pairs of (key, value) • Store elements, retrieve the element with the largest/smallest key efficiently
• Operation: add a pair of (key, value); query the value corresponding to a given key. #include <bits/stdc++.h>
#include <bits/stdc++.h> value of key abc = 1 #define pii pair<int,int> pq pop 100
using namespace std; xyzt is mapped to value 10 using namespace std; pq pop 30
int main() { abc is mapped to value 1 int main() { pq pop 5
map<string, int> M; def is mapped to value 2 priority_queue<int> pq; pq pop 1
M["abc"] = 1; M["def"] = 2; M["xyzt"] = 10; value of 1234 = 0 pq.push(5); pq.push(1); pq.push(100); pq.push(30);
cout << "value of key " << k << " = " << M[k] << endl; int e = pq.top(); pq.pop();
for(map<string,int>::iterator p = M.begin(); p != M.end(); p++) cout << "pq pop " << e << endl;
cout << p->first << " is mapped to value " << p->second << endl; }
string k1 = "1234"; }
cout << "value of " << k1 << " = " << M[k1] << endl;
}
25 26
Standard Template Library (STL) in C++ - Priority Queue Standard Template Library (STL) in C++ - Priority Queue
• Store elements, retrieve the element with the largest/smallest key efficiently • Store elements, retrieve the element with the largest/smallest key efficiently
#include <bits/stdc++.h> #include <bits/stdc++.h>
#define pii pair<int,int> PQ pop (9,-900) #define pii pair<int,int> PQ pop (1,-10)
using namespace std; PQ pop (4,-40) using namespace std; PQ pop (4,-40)
int main() { PQ pop (1,-10) int main() { PQ pop (9,-900)
priority_queue<pii> PQ; priority_queue<pii, vector<pii>, greater<pii> > PQ;
PQ.push(make_pair(4,-40)); PQ.push(make_pair(4,-40));
PQ.push(make_pair(1,-10)); PQ.push(make_pair(1,-10));
PQ.push(make_pair(9,-900)); PQ.push(make_pair(9,-900));
while(!PQ.empty()){ while(!PQ.empty()){
pii e = PQ.top(); PQ.pop(); pii e = PQ.top(); PQ.pop();
cout << "PQ pop (" << e.first << "," << e.second << ")" << endl; cout << "PQ pop (" << e.first << "," << e.second << ")" << endl;
} }
} }
27 28
THANK YOU !
29