Skip to content

Commit 42a41ef

Browse files
2 parents 00fcd1a + 51985c1 commit 42a41ef

27 files changed

+731
-1
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
struct Workshops {
5+
int start_time;
6+
int duration;
7+
int end_time;
8+
};
9+
10+
struct Available_Workshops {
11+
int n;
12+
vector <Workshops> w;
13+
};
14+
15+
Available_Workshops *initialize(int *start_time, int *duration, int n) {
16+
Available_Workshops *tmp = new(Available_Workshops);
17+
Workshops ws;
18+
19+
tmp -> n = n;
20+
for (int i=0;i<n;i++) {
21+
ws.start_time = start_time[i];
22+
ws.duration = duration[i];
23+
ws.end_time = start_time[i]+duration[i];
24+
tmp -> w.push_back(ws);
25+
}
26+
27+
return tmp;
28+
}
29+
30+
bool compare(Workshops w1, Workshops w2) {
31+
return (w1.end_time < w2.end_time);
32+
}
33+
34+
int CalculateMaxWorkshops(Available_Workshops* ptr) {
35+
// sort workshops by end_time
36+
sort(ptr->w.begin(),ptr->w.end(),compare);
37+
38+
// interval scheduling
39+
int last_processed_time=-1;
40+
int maxWorkshops=0;
41+
for (int i=0;i<ptr->n;i++) {
42+
if (ptr->w[i].start_time >= last_processed_time) {
43+
last_processed_time=ptr->w[i].end_time;
44+
maxWorkshops++;
45+
}
46+
}
47+
48+
return maxWorkshops;
49+
}
50+
int main(int argc, char *argv[]) {
51+
int n; // number of workshops
52+
cin >> n;
53+
// create arrays of unknown size n
54+
int* start_time = new int[n];
55+
int* duration = new int[n];
56+
57+
for(int i=0; i < n; i++){
58+
cin >> start_time[i];
59+
}
60+
for(int i = 0; i < n; i++){
61+
cin >> duration[i];
62+
}
63+
64+
Available_Workshops * ptr;
65+
ptr = initialize(start_time,duration, n);
66+
cout << CalculateMaxWorkshops(ptr) << endl;
67+
return 0;
68+
}
69+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
using namespace std;
3+
enum class Fruit { apple, orange, pear };
4+
enum class Color { red, green, orange };
5+
6+
template <typename T> struct Traits;
7+
template <>
8+
struct Traits<Fruit>{
9+
static string name(int index){
10+
switch(index){
11+
case 0:return "apple";
12+
case 1: return "orange" ;
13+
case 2: return "pear";
14+
}
15+
return "unknown";
16+
}
17+
};
18+
template <>
19+
struct Traits<Color>{
20+
static string name(int index){
21+
switch(index){
22+
case 0:return "red";
23+
case 1: return "green" ;
24+
case 2: return "orange";
25+
}
26+
return "unknown";
27+
}
28+
};
29+
30+
int main()
31+
{
32+
int t = 0; std::cin >> t;
33+
34+
for (int i=0; i!=t; ++i) {
35+
int index1; std::cin >> index1;
36+
int index2; std::cin >> index2;
37+
cout << Traits<Color>::name(index1) << " ";
38+
cout << Traits<Fruit>::name(index2) << "\n";
39+
}
40+
}
41+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
#include <algorithm>
6+
#include <cassert>
7+
using namespace std;
8+
9+
template <class T>
10+
class AddElements {
11+
T element;
12+
public:
13+
AddElements(T val) : element(val){}
14+
T add(T val) {
15+
return element + val;
16+
}
17+
T concatenate(T val) {
18+
return element + val;
19+
}
20+
};
21+
22+
int main () {
23+
int n,i;
24+
cin >> n;
25+
for(i=0;i<n;i++) {
26+
string type;
27+
cin >> type;
28+
if(type=="float") {
29+
double element1,element2;
30+
cin >> element1 >> element2;
31+
AddElements<double> myfloat (element1);
32+
cout << myfloat.add(element2) << endl;
33+
}
34+
else if(type == "int") {
35+
int element1, element2;
36+
cin >> element1 >> element2;
37+
AddElements<int> myint (element1);
38+
cout << myint.add(element2) << endl;
39+
}
40+
else if(type == "string") {
41+
string element1, element2;
42+
cin >> element1 >> element2;
43+
AddElements<string> mystring (element1);
44+
cout << mystring.concatenate(element2) << endl;
45+
}
46+
}
47+
return 0;
48+
}
49+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
#include <algorithm>
6+
using namespace std;
7+
class Matrix{
8+
public:
9+
vector<vector<int>> a;
10+
Matrix& operator +(const Matrix &y) {
11+
for(int m = 0; m < y.a.size(); m++)
12+
for(int n = 0; n < y.a[0].size(); n ++) {
13+
a[m][n] = a[m][n] + y.a[m][n];
14+
}
15+
return *this;
16+
}
17+
};
18+
19+
int main () {
20+
int cases,k;
21+
cin >> cases;
22+
for(k=0;k<cases;k++) {
23+
Matrix x;
24+
Matrix y;
25+
Matrix result;
26+
int n,m,i,j;
27+
cin >> n >> m;
28+
for(i=0;i<n;i++) {
29+
vector<int> b;
30+
int num;
31+
for(j=0;j<m;j++) {
32+
cin >> num;
33+
b.push_back(num);
34+
}
35+
x.a.push_back(b);
36+
}
37+
for(i=0;i<n;i++) {
38+
vector<int> b;
39+
int num;
40+
for(j=0;j<m;j++) {
41+
cin >> num;
42+
b.push_back(num);
43+
}
44+
y.a.push_back(b);
45+
}
46+
result = x+y;
47+
for(i=0;i<n;i++) {
48+
for(j=0;j<m;j++) {
49+
cout << result.a[i][j] << " ";
50+
}
51+
cout << endl;
52+
}
53+
}
54+
return 0;
55+
}
56+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Operator Overloading
2+
3+
#include<iostream>
4+
5+
using namespace std;
6+
7+
class Complex
8+
{
9+
public:
10+
int a,b;
11+
void input(string s)
12+
{
13+
int v1=0;
14+
int i=0;
15+
while(s[i]!='+')
16+
{
17+
v1=v1*10+s[i]-'0';
18+
i++;
19+
}
20+
while(s[i]==' ' || s[i]=='+'||s[i]=='i')
21+
{
22+
i++;
23+
}
24+
int v2=0;
25+
while(i<s.length())
26+
{
27+
v2=v2*10+s[i]-'0';
28+
i++;
29+
}
30+
a=v1;
31+
b=v2;
32+
}
33+
};
34+
35+
//Overload operators + and << for the class complex
36+
//+ should add two complex numbers as (a+ib) + (c+id) = (a+c) + i(b+d)
37+
//<< should print a complex number in the format "a+ib"
38+
39+
Complex operator + (Complex x, Complex y) {
40+
x.a += y.a;
41+
x.b += y.b;
42+
return x;
43+
}
44+
45+
ostream& operator << (ostream& out, Complex x) {
46+
out << x.a << "+i" << x.b;
47+
return out;
48+
}
49+
50+
int main()
51+
{
52+
Complex x,y;
53+
string s1,s2;
54+
cin>>s1;
55+
cin>>s2;
56+
x.input(s1);
57+
y.input(s2);
58+
Complex z=x+y;
59+
cout<<z<<endl;
60+
}
61+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#define toStr(x) #x
2+
#define io(s) cin >> s
3+
#define foreach(list, index) for(int index = 0; index < list.size(); index++)
4+
#define FUNCTION(name, op) void name(int &x, int y){ if(!(x op y)) x = y; }
5+
#define INF 1e9
6+
7+
#include <iostream>
8+
#include <vector>
9+
using namespace std;
10+
11+
#if !defined toStr || !defined io || !defined FUNCTION || !defined INF
12+
#error Missing preprocessor definitions
13+
#endif
14+
15+
FUNCTION(minimum, <)
16+
FUNCTION(maximum, >)
17+
18+
int main(){
19+
int n; cin >> n;
20+
vector<int> v(n);
21+
foreach(v, i) {
22+
io(v)[i];
23+
}
24+
int mn = INF;
25+
int mx = -INF;
26+
foreach(v, i) {
27+
minimum(mn, v[i]);
28+
maximum(mx, v[i]);
29+
}
30+
int ans = mx - mn;
31+
cout << toStr(Result =) <<' '<< ans;
32+
return 0;
33+
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <deque>
3+
using namespace std;
4+
5+
void printKMax(int arr[], int n, int k){
6+
deque<int> d;
7+
for(int i=0;i<k;i++)
8+
{
9+
while(!d.empty() && arr[i]>=arr[d.back()]){
10+
d.pop_back();
11+
}
12+
d.push_back(i);
13+
}
14+
for(int i=k;i<n;i++)
15+
{
16+
cout<<arr[d[0]]<<" ";
17+
while(!d.empty() && d[0]<=i-k)
18+
{
19+
d.pop_front();
20+
}
21+
while(!d.empty() && arr[i]>=arr[d.back()])
22+
{
23+
d.pop_back();
24+
}
25+
d.push_back(i);
26+
}
27+
cout<<arr[d[0]]<<" \n";
28+
}
29+
30+
int main(){
31+
ios_base::sync_with_stdio(false);
32+
cin.tie(0);
33+
int t;
34+
cin >> t;
35+
while(t>0) {
36+
int n,k;
37+
cin >> n >> k;
38+
int i;
39+
int arr[n];
40+
for(i=0;i<n;i++)
41+
cin >> arr[i];
42+
printKMax(arr, n, k);
43+
t--;
44+
}
45+
return 0;
46+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <cmath>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <iostream>
5+
#include <algorithm>
6+
using namespace std;
7+
8+
9+
int main() {
10+
int size, a;
11+
cin >> size;
12+
vector <int> v;
13+
14+
for(int i = 0; i < size; i ++) {
15+
cin >> a;
16+
v.push_back(a);
17+
}
18+
19+
int q;
20+
cin >> q;
21+
22+
for(int i = 0; i < q; i ++) {
23+
int n;
24+
cin >> n;
25+
vector<int> :: iterator low = lower_bound(v.begin(), v.end(), n);
26+
if (v[low - v.begin()] == n)
27+
cout << "Yes" << " " << low - v.begin() + 1 << endl;
28+
else
29+
cout << "No" << " " << low - v.begin() + 1 << endl;
30+
}
31+
return 0;
32+
}
33+

0 commit comments

Comments
 (0)