Skip to content

Commit 6647146

Browse files
committed
Oct 4
1 parent ae432d1 commit 6647146

File tree

11 files changed

+318
-17
lines changed

11 files changed

+318
-17
lines changed

2019/oct4/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ICPC North America Qualifier 2019
2+
<https://naq19.kattis.com/>

2019/oct4/a-circuit.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<iostream>
2+
#include<stack>
3+
#include<string>
4+
using namespace std;
5+
6+
bool is_gate(char in) {
7+
return (in == '*' || in == '+' || in == '-');
8+
}
9+
10+
11+
bool is_binary(char in) {
12+
return (in == '*' || in == '+');
13+
}
14+
15+
/**
16+
g++ -std=c++11 a-circuit.cpp -o k
17+
*/
18+
int main() {
19+
stack<bool> s;
20+
int n, at_letter, k;
21+
at_letter = 0; k = 0;
22+
char in;
23+
string line;
24+
cin >> n;
25+
bool b[n];
26+
bool o1, o2;
27+
for(int i = 0; i < n; i++) {
28+
cin >> in;
29+
if(in == 'F') b[i] = false;
30+
else b[i] = true;
31+
}
32+
33+
cin.ignore();
34+
getline(cin, line);
35+
36+
while(k < line.size()) {
37+
in = line[k]; k++;
38+
if(in == ' ' || in == '\n')
39+
continue;
40+
41+
if(is_gate(in)) {
42+
if(is_binary(in)) {
43+
o1 = s.top(); s.pop();
44+
o2 = s.top(); s.pop();
45+
if(in == '*') { // * AND
46+
s.push(o1 && o2);
47+
} else { // + OR
48+
s.push(o1 || o2);
49+
}
50+
} else { // is singular - NOT
51+
o1 = s.top(); s.pop();
52+
s.push(!o1);
53+
}
54+
} else { // A B ... Z
55+
s.push(b[at_letter]);
56+
at_letter++;
57+
}
58+
}
59+
cout << (s.top() ? 'T' : 'F') << endl;
60+
61+
return 0;
62+
}

2019/oct4/b-diagonal-cut.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<iostream>
2+
#include<numeric>
3+
using namespace std;
4+
5+
typedef unsigned long long int mlong;
6+
7+
8+
/**
9+
g++ -std=c++17 b-diagonal-cut.cpp -o k
10+
1 <= M, N <= 10^18 leads to issues. Need adjustment
11+
*/
12+
13+
int main() {
14+
mlong M, N;
15+
cin >> M >> N;
16+
mlong g = gcd(M, N);
17+
mlong m = M / g;
18+
mlong n = N / g;
19+
mlong b;
20+
if(m == n) {
21+
b = n;
22+
} else if(m % 2 == n % 2) {
23+
b = 1;
24+
}
25+
mlong f = b * g;
26+
printf("%llu", f);
27+
cout << endl;
28+
return 0;
29+
}

2019/oct4/c-gerrymandering.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<iostream>
2+
#include<string>
3+
#include<cmath>
4+
using namespace std;
5+
6+
typedef long long int mlong;
7+
typedef long double mdouble;
8+
9+
/**
10+
g++ -std=c++11 c-gerrymandering.cpp -o k
11+
*/
12+
13+
int main() {
14+
int P, D, d;
15+
mlong a, b;
16+
cin >> P >> D;
17+
mlong vA[D];
18+
mlong vB[D];
19+
mlong wA;
20+
mlong wB;
21+
mlong wdiff = 0;
22+
mlong vtotal = 0;
23+
bool Awins;
24+
25+
for(int i = 0; i < D; i++) {
26+
vA[i] = 0;
27+
vB[i] = 0;
28+
}
29+
30+
for(int i = 0; i < P; i++) {
31+
cin >> d >> a >> b;
32+
vA[d - 1] += a;
33+
vB[d - 1] += b;
34+
vtotal += a + b;
35+
}
36+
37+
// for(int i = 0; i < D; i++) {
38+
// printf("%d %d\n", vA[i], vB[i]);
39+
// }
40+
41+
for(int i = 0; i < D; i++) {
42+
if(vA[i] > vB[i]) { // A wins
43+
Awins = true;
44+
wB = vB[i];
45+
wA = vA[i] - ((mlong) floor(((mdouble) vA[i] + vB[i]) / 2.0)) - 1;
46+
} else { // B wins
47+
Awins = false;
48+
wA = vA[i];
49+
wB = vB[i] - ((mlong) floor(((mdouble) vA[i] + vB[i]) / 2.0)) - 1;
50+
}
51+
wdiff += (wA - wB);
52+
cout << (Awins ? "A" : "B") << " " << wA << " " << wB << endl;
53+
}
54+
cout.precision(8);
55+
cout << abs((mdouble) wdiff) / ((mdouble) vtotal) << endl;
56+
57+
return 0;
58+
}
59+
60+
61+
62+

2019/oct4/d-missing.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
5+
typedef unsigned long long int mlong;
6+
7+
8+
/**
9+
g++ -std=c++11 d-missing.cpp -o k
10+
4
11+
3
12+
4
13+
5
14+
8
15+
*/
16+
17+
int main() {
18+
int n, c, d;
19+
vector<int> v;
20+
cin >> n;
21+
int last;
22+
bool skipped = false;
23+
for(int i = 0; i < n; i++) {
24+
cin >> c;
25+
if(i == 0) {
26+
// check if c == 1
27+
for(int j = 1; j < c; j++)
28+
v.push_back(j);
29+
} else {
30+
for(int j = last + 1; j < c; j++)
31+
v.push_back(j);
32+
}
33+
last = c;
34+
}
35+
if(v.empty()) {
36+
cout << "good job" << endl;
37+
return 0;
38+
}
39+
for(int i : v) {
40+
cout << i << endl;
41+
}
42+
return 0;
43+
}
44+

2019/oct4/i-slow-leak.cpp

Whitespace-only changes.

2019/oct4/k-summer.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<iostream>
2+
#include<string>
3+
#include<unordered_set>
4+
using namespace std;
5+
6+
/**
7+
g++ -std=c++11 k-summer.cpp -o k
8+
*/
9+
int main() {
10+
unordered_set<char> s = unordered_set<char>(); // charaters visited at each i
11+
//unordered_set<char> s; // charaters visited at each i
12+
string in;
13+
cin >> in;
14+
int count = 0;
15+
for(int i = 0; i < in.size(); i++) {
16+
s = unordered_set<char>();
17+
for(int j = i + 1; j < in.size(); j++) {
18+
if(in[i] == in[j])
19+
break;
20+
else {
21+
if(s.count(in[j]) > 0) {
22+
// don't count this one
23+
} else {
24+
count++;
25+
s.insert(in[j]);
26+
// print the good itinerary
27+
// for(int k = i; k <= j; k++) {
28+
// printf("%c", in[k]);
29+
// }
30+
// cout << endl;
31+
}
32+
}
33+
}
34+
}
35+
36+
cout << count << endl;
37+
return 0;
38+
}
39+

2019/oct4/m-zipline.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include<iostream>
2+
#include<algorithm>
3+
#include<cmath>
4+
using namespace std;
5+
6+
typedef unsigned long long int mlong;
7+
8+
double pow2(double x) {
9+
return pow(x, 2.0);
10+
}
11+
12+
double f(double x, double w, double g, double h) {
13+
return sqrt(pow2(g) + pow2(x)) + sqrt(pow2(h) + pow2(w - x));
14+
}
15+
16+
double df(double x, double w, double g, double h) {
17+
return (x / sqrt(pow2(g) + pow2(x))) + ((x - w) / sqrt(pow2(h) + pow2(w - x)));
18+
}
19+
20+
double bsearch(double w, double g, double h) {
21+
double left = 0;
22+
double right = w;
23+
double x, dfx;
24+
int i = 0;
25+
while(abs(left - right) > pow(10.0, -6) / 1.10) {
26+
//cout << "iter " << i << " " << abs(left - right) << endl;
27+
i++;
28+
if(i > 1000) break;
29+
x = (left + right) / 2;
30+
dfx = df(x, w, g, h);
31+
if(dfx == 0.0)
32+
break;
33+
else if(dfx < 0.0)
34+
left = x;
35+
else // (x > 0.0)
36+
right = x;
37+
}
38+
return f(x, w, g, h);
39+
}
40+
41+
/**
42+
g++ -std=c++17 m-zipline.cpp -o k
43+
*/
44+
double getmin(double w, double g, double h) {
45+
double x = abs(g - h);
46+
return sqrt(pow2(x) + pow2(w));
47+
}
48+
49+
int main() {
50+
int n, w, g, h, r;
51+
cin >> n;
52+
double l[n];
53+
double L[n];
54+
55+
for(int i = 0; i < n; i++) {
56+
cin >> w >> g >> h >> r;
57+
// compute minimum
58+
l[i] = getmin((double) w, (double) g, (double) h);
59+
g = g - r;
60+
h = h - r;
61+
// compute maximum
62+
L[i] = bsearch((double) w, (double) g, (double) h);
63+
}
64+
65+
cout.precision(17);
66+
for(int i = 0; i < n; i++) {
67+
cout << l[i] << " " << L[i] << endl;
68+
}
69+
70+
return 0;
71+
}
72+

2019/sept21/g-graduation3.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,9 @@ g++ -std=c++11 g-graduation3.cpp -o k
213213
11 3
214214
0 1 1 2 2 2 2 3 3 3 3
215215
216+
8 2
217+
0 1 2 3 0 5 5 5
218+
216219
8 3
217220
2 0 2 0 4 4 2 5 6
218221

2019/sept21/g-graduation4.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <iostream>
22
#include <set>
33
#include <string>
4-
#include <stack>
54
#include <queue>
65
#include <vector>
76
using namespace std;

0 commit comments

Comments
 (0)