Skip to content

Commit b435029

Browse files
authored
HW #2
1 parent 3fff178 commit b435029

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

Kattis Automatic Trading.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by Alon on 29/05/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
using namespace std;
12+
typedef long long ll;
13+
14+
15+
16+
int main() { // automatictrading
17+
string s;
18+
cin >> s;
19+
int q, a, b, res;
20+
cin >> q;
21+
while (q--) {
22+
cin >> a >> b;
23+
res = 0;
24+
while (b+res < s.size() && s[a+res] == s[b+res]) ++res;
25+
cout << res << endl;
26+
}
27+
return 0;
28+
}

Kattis Avoiding the Apocalypse.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//
2+
// Created by Alon on 13/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
#include <cassert>
12+
#include <climits>
13+
#include <cmath>
14+
#include <iostream>
15+
#include <algorithm>
16+
#include <vector>
17+
#include <queue>
18+
#include <set>
19+
#include <map>
20+
21+
using namespace std;
22+
23+
typedef pair<int,int> ii;
24+
typedef pair<int,ii> iii;
25+
typedef vector<ii> vii;
26+
typedef vector<vii> vvii;
27+
typedef vector<int> vi;
28+
typedef vector<vi> vvi;
29+
typedef set<int> si;
30+
typedef vector<si> vsi;
31+
typedef vector<map<int, int>> vmii;
32+
33+
const int INF = 1e9;
34+
using namespace std;
35+
typedef long long ll;
36+
37+
/********** Max Flow **********/
38+
typedef pair<int, int> pii;
39+
40+
int augment(vector<map<int, int>>& res, int s, int t, const vi& p, int minEdge) {
41+
// traverse the path from s to t according to p.
42+
// change the residuals on this path according to the min edge weight along this path.
43+
// return the amount of flow that was added.
44+
if (t == s) {
45+
return minEdge;
46+
} else if (p[t] != -1) {
47+
int f = augment(res, s, p[t], p, min(minEdge, res[p[t]][t]));
48+
res[p[t]][t] -= f;
49+
res[t][p[t]] += f;
50+
return f;
51+
}
52+
return 0;
53+
}
54+
55+
// input: number of nodes (n), all nodes are between 0 and n-1,
56+
// edges v1->v2 of the form (weight,(v1,v2)), source (s) and target (t).
57+
// output: max flow from s to t over the edges.
58+
// time: O(VE^2) and O(EF).
59+
int EdmondsKarp (int n, vector<iii>& edges, int s, int t, vmii& res) {
60+
// initialise adjacenty list and residuals adjacency matrix
61+
62+
// vvi res(n,vi(n,0));
63+
vvi adj(n);
64+
for (iii e : edges) {
65+
res[e.second.first][e.second.second] += e.first;
66+
adj[e.second.first].push_back(e.second.second);
67+
adj[e.second.second].push_back(e.second.first);
68+
}
69+
// while we can add flow
70+
int addedFlow, maxFlow = 0;
71+
do {
72+
// save to p the BFS tree from s to t using only edges with residuals
73+
//vi dist(res.size(), INF);
74+
vi dist(n, INF);
75+
dist[s] = 0;
76+
queue<int> q;
77+
q.push(s);
78+
// vi p(res.size(), -1);
79+
vi p(n, -1);
80+
while (!q.empty()) {
81+
int u = q.front(); q.pop();
82+
if (u == t) break;
83+
for (int v : adj[u]) if (res[u][v] > 0 && dist[v] == INF) {
84+
dist[v] = dist[u] + 1;
85+
q.push(v);
86+
p[v] = u;
87+
}
88+
}
89+
// add flow on the path between s to t according to p
90+
addedFlow = augment(res, s, t, p, INF);
91+
maxFlow += addedFlow;
92+
} while (addedFlow > 0);
93+
return maxFlow;
94+
}
95+
96+
97+
98+
int locations, start_location, number_of_people, number_of_steps, number_of_medics, number_of_roads;
99+
#define NODE(specific_location, time) (locations * (time) + specific_location)
100+
#define START_NODE 0
101+
#define END_NODE (locations * number_of_steps + 1)
102+
#define NUMBER_OF_NODES (locations * number_of_steps + 2)
103+
#define INF_PEOPLE (1000 * 1000)
104+
105+
int main() {
106+
int cases;
107+
cin >> cases;
108+
while (cases--) {
109+
vector<iii> edges;
110+
cin >> locations >> start_location >> number_of_people >> number_of_steps >> number_of_medics;
111+
number_of_steps++;
112+
// limit the flow
113+
edges.push_back({number_of_people, {START_NODE, NODE(start_location, 0)}});
114+
115+
for (int i = 0; i < number_of_medics; ++i) { // all medics go to the sink
116+
int m;
117+
cin >> m;
118+
edges.push_back({INF_PEOPLE, {NODE(m, number_of_steps - 1), END_NODE}});
119+
}
120+
121+
cin >> number_of_roads;
122+
for (int i = 0; i < number_of_roads; ++i) { //possible edges from input
123+
int from, to, max_people, stall_time;
124+
cin >> from >> to >> max_people >> stall_time;
125+
for (int time = 0; time + stall_time < number_of_steps; ++time) {
126+
edges.push_back({max_people, {NODE(from, time), NODE(to, time + stall_time)}});
127+
}
128+
}
129+
130+
for (int location = 1; location <= locations; ++location) { // waiting edges
131+
for (int time = 1; time < number_of_steps; ++time) {
132+
edges.push_back({INF_PEOPLE, {NODE(location, time - 1), NODE(location, time)}});
133+
}
134+
}
135+
136+
vector<map<int, int>> adj(NUMBER_OF_NODES);
137+
cout << EdmondsKarp(NUMBER_OF_NODES, edges, START_NODE, END_NODE, adj) << endl;
138+
}
139+
return 0;
140+
}
141+
142+

Kattis Canonical Coin Systems.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Created by Alon on 02/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
#include <cassert>
12+
#include <limits.h>
13+
#include <cmath>
14+
15+
using namespace std;
16+
typedef long long ll;
17+
18+
19+
int main() {
20+
int n, new_c;
21+
cin >> n;
22+
assert(n > 1);
23+
vector<int> coins(n);
24+
for (int i = n-1; i >= 0; --i) cin >> coins[i]; // sorted from big to small
25+
vector<int> num_of_coins(coins[0] + coins[1], INT_MAX);
26+
/// real minimum
27+
for (int c = 0; c < n; ++c) {
28+
int coin = coins[c];
29+
num_of_coins[coin] = 1;
30+
for (int i = 0; i + coin < num_of_coins.size(); ++i) {
31+
if (num_of_coins[i+coin] > num_of_coins[i]) {
32+
num_of_coins[i+coin] = num_of_coins[i] + 1;
33+
}
34+
}
35+
}
36+
/// greedy search
37+
for (int i = 1; i < num_of_coins.size(); ++i) {
38+
int temp = i, num = 0;
39+
for (int coin : coins) {
40+
num += floor(temp / coin);
41+
temp %= coin;
42+
}
43+
if (num_of_coins[i] < num) {
44+
cout << "non-canonical" << endl;
45+
return 0;
46+
}
47+
48+
}
49+
cout << "canonical" << endl;
50+
return 0;
51+
}
52+

Kattis Walrus Weights.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Created by Alon on 02/06/2020.
3+
//
4+
5+
#include <iostream>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <fstream>
9+
#include <string>
10+
#include <vector>
11+
using namespace std;
12+
typedef long long ll;
13+
14+
15+
16+
#define POSSIBLE_SIZE 2001
17+
bool possible[POSSIBLE_SIZE] = { false }; // init to false
18+
int main() {
19+
int n, cur;
20+
cin >> n;
21+
while (n--) {
22+
cin >> cur;
23+
for (int i = POSSIBLE_SIZE - cur - 1; i > 0; --i) {
24+
if (possible[i]) possible[i+cur] = true;
25+
}
26+
possible[cur] = true;
27+
}
28+
for (int i = 0; i < 1000; ++i) {
29+
if (possible[1000 + i]) {
30+
cout << 1000 + i << endl;
31+
break;
32+
}
33+
if (possible[1000 - i]) {
34+
cout << 1000 - i << endl;
35+
break;
36+
}
37+
}
38+
return 0;
39+
}
40+

0 commit comments

Comments
 (0)