Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit b9b653f

Browse files
committed
Computer Assignment 2
1 parent 44a041a commit b9b653f

12 files changed

+556
-1
lines changed

.DS_Store

0 Bytes
Binary file not shown.

CA1/.DS_Store

0 Bytes
Binary file not shown.

CA1/Archive.zip

420 KB
Binary file not shown.

CA1/Question1.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def read_input() :
2121
def find_gcd_max(n,k,bags,biggest_bag) :
2222
gcd_max = 1
2323
add_to_bags = [0]*n
24-
for gcd in range(2,biggest_bag+1) :
24+
for gcd in range(2,biggest_bag+k+1) :
2525
for idx,bag in enumerate(bags) :
2626
remainder = bag%gcd
2727
x = 0

CA2/.DS_Store

6 KB
Binary file not shown.

CA2/.vscode/launch.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "g++-10 - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}/${fileBasenameNoExtension}",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "lldb",
18+
"preLaunchTask": "C/C++: g++-10 build active file"
19+
}
20+
]
21+
}

CA2/.vscode/settings.json

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"files.associations": {
3+
"initializer_list": "cpp",
4+
"vector": "cpp",
5+
"iostream": "cpp",
6+
"cmath": "cpp",
7+
"array": "cpp",
8+
"atomic": "cpp",
9+
"bit": "cpp",
10+
"*.tcc": "cpp",
11+
"cctype": "cpp",
12+
"clocale": "cpp",
13+
"compare": "cpp",
14+
"complex": "cpp",
15+
"concepts": "cpp",
16+
"cstdarg": "cpp",
17+
"cstddef": "cpp",
18+
"cstdint": "cpp",
19+
"cstdio": "cpp",
20+
"cstdlib": "cpp",
21+
"cwchar": "cpp",
22+
"cwctype": "cpp",
23+
"deque": "cpp",
24+
"unordered_map": "cpp",
25+
"exception": "cpp",
26+
"algorithm": "cpp",
27+
"functional": "cpp",
28+
"iterator": "cpp",
29+
"memory": "cpp",
30+
"memory_resource": "cpp",
31+
"numeric": "cpp",
32+
"optional": "cpp",
33+
"random": "cpp",
34+
"string": "cpp",
35+
"string_view": "cpp",
36+
"system_error": "cpp",
37+
"tuple": "cpp",
38+
"type_traits": "cpp",
39+
"utility": "cpp",
40+
"iosfwd": "cpp",
41+
"istream": "cpp",
42+
"limits": "cpp",
43+
"new": "cpp",
44+
"ostream": "cpp",
45+
"ranges": "cpp",
46+
"sstream": "cpp",
47+
"stdexcept": "cpp",
48+
"streambuf": "cpp",
49+
"typeinfo": "cpp"
50+
}
51+
}

CA2/.vscode/tasks.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "cppbuild",
5+
"label": "C/C++: g++-10 build active file",
6+
"command": "/usr/local/bin/g++-10",
7+
"args": [
8+
"-g",
9+
"${file}",
10+
"-o",
11+
"${fileDirname}/${fileBasenameNoExtension}"
12+
],
13+
"options": {
14+
"cwd": "${workspaceFolder}"
15+
},
16+
"problemMatcher": [
17+
"$gcc"
18+
],
19+
"group": {
20+
"kind": "build",
21+
"isDefault": true
22+
},
23+
"detail": "Task generated by Debugger."
24+
}
25+
],
26+
"version": "2.0.0"
27+
}

CA2/Q1-v2.cpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include<iostream>
2+
#include <utility>
3+
#include <vector>
4+
#include <cstdlib>
5+
using namespace std;
6+
7+
struct MaxHeap
8+
{
9+
vector<int> data;
10+
MaxHeap()
11+
{
12+
data.push_back(0);
13+
}
14+
void insert(int value)
15+
{
16+
data.push_back(value);
17+
int current = data.size()-1;
18+
while(current != 1)
19+
{
20+
if(data[current] > data[current >> 1])
21+
{
22+
swap(data[current], data[current >> 1]);
23+
current >>= 1;
24+
}
25+
else break;
26+
}
27+
}
28+
void pop()
29+
{
30+
if(data.size() == 1) return;
31+
swap(data[1], data[data.size()-1]);
32+
data.pop_back();
33+
int current = 1;
34+
while(current < data.size())
35+
{
36+
int largest = current;
37+
if((current << 1) < data.size() && data[current << 1] > data[largest])
38+
{
39+
largest = current << 1;
40+
}
41+
if((current << 1) + 1 < data.size() && data[(current << 1) + 1] > data[largest])
42+
{
43+
largest = (current << 1) + 1;
44+
}
45+
if(largest != current)
46+
{
47+
swap(data[current], data[largest]);
48+
current = largest;
49+
}
50+
else break;
51+
}
52+
}
53+
int top()
54+
{
55+
return data[1];
56+
}
57+
bool empty()
58+
{
59+
return data.size() <= 1;
60+
}
61+
};
62+
63+
class War
64+
{
65+
public:
66+
War()
67+
{
68+
MaxHeap attack_priorities = MaxHeap();
69+
min_moves = 0;
70+
71+
}
72+
73+
void read_input()
74+
{
75+
cin >> n;
76+
cin >> z;
77+
cin >> k;
78+
for(int i = 0;i < n;i++)
79+
{
80+
int power;
81+
cin >> power;
82+
attack_priorities.insert(power);
83+
}
84+
}
85+
86+
void start()
87+
{
88+
while(z > 0 && min_moves < k)
89+
{
90+
int biggest_power = attack_priorities.top();
91+
attack_priorities.pop();
92+
z -= biggest_power;
93+
attack_priorities.insert(biggest_power/2);
94+
min_moves++;
95+
}
96+
97+
if(z <= 0 && min_moves <= k)
98+
cout << min_moves << endl;
99+
else
100+
cout << -1 << endl;
101+
}
102+
private:
103+
MaxHeap attack_priorities;
104+
long int n;
105+
long int z;
106+
long int k;
107+
long int min_moves;
108+
};
109+
int main()
110+
{
111+
War war = War();
112+
war.read_input();
113+
war.start();
114+
return 0;
115+
}

CA2/Q2-v3.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Link:
2+
def __init__( self, start, end ):
3+
self.start = start
4+
self.end = end
5+
6+
7+
def count_subarrys(arr,l) :
8+
count = {}
9+
links = [None] * l
10+
11+
for i in sorted(range(0,l),key = lambda i: int(arr[i])):
12+
link_previous = links[i - 1] if i > 0 else None
13+
link_next = links[i + 1] if i < (l - 1) else None
14+
15+
start = link_previous.start if link_previous else i
16+
end = link_next.end if link_next else i
17+
18+
links[i] = Link( start, end )
19+
20+
if link_previous:
21+
links[link_previous.start].end = end
22+
23+
if link_next:
24+
links[link_next.end].start = start
25+
number = int(arr[i])
26+
if not number in count.keys() :
27+
count[number] = (1 + i - start) * (1 + end - i)
28+
else :
29+
count[number] += (1 + i - start) * (1 + end - i)
30+
31+
return count
32+
33+
34+
def cin() :
35+
string = input().strip()
36+
while string == "" :
37+
string = input().strip()
38+
return string
39+
40+
def main() :
41+
n,q = input().split()
42+
n = int(n)
43+
q = int(q)
44+
numbers = input().split()
45+
req = []
46+
while q > 0 :
47+
q -= 1
48+
req.append(int(input()))
49+
# count = count_subarrys(numbers,n)
50+
# for i in req :
51+
# if not i in count.keys() :
52+
# print(0)
53+
# else :
54+
# print(count[i])
55+
56+
main()

0 commit comments

Comments
 (0)