Skip to content

Commit c452bad

Browse files
committed
add: Notes & UVA src
1 parent 7ae2916 commit c452bad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3050
-0
lines changed
Binary file not shown.
Binary file not shown.

Notes/combinatorics/catalan.pdf

166 KB
Binary file not shown.
1.55 MB
Binary file not shown.

UVA/10003.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <iostream>
2+
#include <cstring>
3+
using namespace std;
4+
5+
int n, x, a[55], b[55], p[55], dp[55][55], h[55][55];
6+
7+
int optimize (int l, int r)
8+
{
9+
int tmp=1e9, sum=p[r]-p[l-1];
10+
//Knuth's optimization
11+
for (int k=h[l][r-1]; k<=h[l+1][r]; ++k)
12+
if (dp[l][k-1]+dp[k][r]+sum<=tmp)
13+
{
14+
tmp=dp[l][k-1]+dp[k][r]+sum;
15+
h[l][r]=k;
16+
}
17+
dp[l][r]=tmp;
18+
return dp[l][r];
19+
}
20+
21+
int main()
22+
{
23+
ios_base::sync_with_stdio(false); cin.tie(NULL);
24+
while (cin >> x)
25+
{
26+
if (x<1) return 0;
27+
memset(dp, 0, sizeof dp);
28+
memset(h, 0, sizeof h);
29+
cin >> n;
30+
for (int i=1; i<=n; ++i)
31+
cin >> b[i];
32+
33+
a[1]=b[1];
34+
for (int i=2; i<=n+1; ++i)
35+
if (i>n)
36+
a[i]=x-b[i-1];
37+
else
38+
a[i]=b[i]-b[i-1];
39+
++n;
40+
41+
for (int i=1; i<=n; ++i)
42+
p[i]=p[i-1]+a[i];
43+
44+
for (int i=1; i<=n; ++i)
45+
dp[i][i]=0,
46+
h[i][i]=i;
47+
48+
for (int d=1; d<=n-1; ++d)
49+
for (int i=1; i<=n-d; ++i)
50+
dp[i][i+d]=optimize(i, i+d);
51+
52+
cout << "The minimum cutting is " << dp[1][n] << ".\n";
53+
}
54+
return 0;
55+
}

UVA/10054.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <list>
5+
using namespace std;
6+
const int N = 65;
7+
8+
int n, deg[N];
9+
list<pair<int, int> > euler;
10+
vector<pair<int, bool> > g[N];
11+
12+
void euler_cycle(list<pair<int, int> >::iterator it, int u) {
13+
for (auto &i : g[u])
14+
if (i.second) {
15+
int v = i.first;
16+
i.second = false;
17+
for (auto &edge : g[v])
18+
if (edge.first == u && edge.second) {
19+
edge.second = false;
20+
break;
21+
}
22+
euler_cycle(euler.insert(it, {v, u}), v);
23+
}
24+
}
25+
26+
int main() {
27+
ios::sync_with_stdio(0);
28+
cin.tie(NULL);
29+
//freopen("input.in", "r", stdin);
30+
//freopen("output.out", "w", stdout);
31+
32+
int tc;
33+
cin >> tc;
34+
for (int c = 1; c <= tc; ++c) {
35+
if (c != 1) cout << '\n';
36+
cout << "Case #" << c << '\n';
37+
for (int i = 0; i < N; ++i)
38+
g[i].clear(), deg[i] = 0;
39+
40+
cin >> n;
41+
for (int i = 0, u, v; i < n; ++i) {
42+
cin >> u >> v;
43+
g[u].emplace_back(v, true);
44+
g[v].emplace_back(u, true);
45+
++deg[u];
46+
++deg[v];
47+
}
48+
49+
bool good = true;
50+
int start = -1;
51+
for (int i = 1; i <= 50; ++i)
52+
if (deg[i] % 2) {
53+
good = false;
54+
break;
55+
} else if (deg[i] && start == -1) {
56+
start = i;
57+
}
58+
59+
if (!good) {
60+
cout << "some beads may be lost\n";
61+
} else {
62+
euler.clear();
63+
euler_cycle(euler.begin(), start);
64+
for (const auto &i : euler)
65+
cout << i.first << ' ' << i.second << '\n';
66+
}
67+
}
68+
}

UVA/10054_stack.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <vector>
4+
#include <stack>
5+
using namespace std;
6+
const int N = 100;
7+
8+
int n, deg[N];
9+
vector<pair<int, bool> > g[N];
10+
11+
void eulerCycle(int s) {
12+
vector<int> res;
13+
stack<int> st;
14+
st.push(s);
15+
while (!st.empty()) {
16+
int u = st.top();
17+
bool flag = false;
18+
for (auto &i : g[u])
19+
if (i.second) {
20+
flag = true;
21+
i.second = false;
22+
int v = i.first;
23+
for (auto &edge : g[v])
24+
if (edge.first == u && edge.second) {
25+
edge.second = false;
26+
break;
27+
}
28+
st.push(v);
29+
break;
30+
}
31+
if (!flag) {
32+
res.emplace_back(u);
33+
st.pop();
34+
}
35+
}
36+
for (int i = 0; i + 1 < (int)res.size(); ++i)
37+
cout << res[i] << ' ' << res[i + 1] << '\n';
38+
}
39+
40+
int main() {
41+
ios::sync_with_stdio(0);
42+
cin.tie(NULL);
43+
//freopen("input.in", "r", stdin);
44+
//freopen("output.out", "w", stdout);
45+
46+
int tc;
47+
cin >> tc;
48+
for (int c = 1; c <= tc; ++c) {
49+
if (c != 1) cout << '\n';
50+
cout << "Case #" << c << '\n';
51+
for (int i = 1; i <= 50; ++i)
52+
deg[i] = 0, g[i].clear();
53+
54+
cin >> n;
55+
for (int i = 0, u, v; i < n; ++i) {
56+
cin >> u >> v;
57+
g[u].emplace_back(v, true);
58+
g[v].emplace_back(u, true);
59+
++deg[u];
60+
++deg[v];
61+
}
62+
63+
bool good = true;
64+
int start = -1;
65+
for (int u = 1; u <= 50; ++u)
66+
if (deg[u] & 1) {
67+
good = false;
68+
break;
69+
} else if (deg[u] && start == -1) {
70+
start = u;
71+
}
72+
73+
if (!good) {
74+
cout << "some beads may be lost\n";
75+
continue;
76+
}
77+
eulerCycle(start);
78+
}
79+
}

UVA/10065.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <iomanip>
6+
using namespace std;
7+
const double eps = 1e-9;
8+
9+
struct point
10+
{
11+
double x, y;
12+
point() { x = 0, y = 0; }
13+
point(double xx, double yy) : x(xx), y(yy) {}
14+
};
15+
struct vec
16+
{
17+
double x, y;
18+
vec() { x = 0, y = 0; }
19+
vec(double xx, double yy) : x(xx), y(yy) {}
20+
};
21+
vec tovec (const point &a, const point &b) { return vec(b.x - a.x, b.y - a.y); }
22+
double cross (const vec &a, const vec &b) { return (a.x * b.y - b.x * a.y); }
23+
double dist (const point &a, const point &b) { return hypot(a.x - b.x, a.y - b.y); }
24+
bool collinear (const point &O, const point &a, const point &b) { return fabs(cross(tovec(O, a), tovec(O, b))) < eps; }
25+
bool ccw (const point &O, const point &a, const point &b) { return cross(tovec(O, a), tovec(O, b)) > 0; }
26+
27+
int n, C = 0;
28+
point pivot;
29+
vector<point> p, CVH;
30+
31+
bool grahamScan (const point &L, const point &R)
32+
{
33+
if (collinear(pivot, L, R))
34+
return dist(pivot, L) - dist(pivot, R) < eps;
35+
return cross(tovec(pivot, L), tovec(pivot, R)) > 0;
36+
}
37+
38+
vector<point> findCVH (vector<point> P)
39+
{
40+
if (n <= 3)
41+
{
42+
if (!(P[0].x == P[n - 1].x && P[0].y == P[n - 1].y)) P.push_back(P[0]);
43+
return P;
44+
}
45+
int P0 = 0;
46+
for (int i = 1; i < n; ++i)
47+
if (P[i].y < P[P0].y || (P[i].y == P[P0].y && P[i].x > P[P0].x)) P0 = i;
48+
49+
point tmp = P[0]; P[0] = P[P0]; P[P0] = tmp;
50+
51+
pivot = P[0];
52+
sort(P.begin() + 1, P.end(), grahamScan);
53+
54+
vector<point> S;
55+
S.push_back(P[n - 1]);
56+
S.push_back(P[0]);
57+
S.push_back(P[1]);
58+
int i = 2;
59+
while (i < n)
60+
{
61+
int top = (int)S.size() - 1;
62+
if (ccw(S[top - 1], S[top], P[i])) S.push_back(P[i++]);
63+
else S.pop_back();
64+
}
65+
return S;
66+
}
67+
68+
double areaPoly (const vector<point> &Poly)
69+
{
70+
double S = 0.0;
71+
point Origin = point(0, 0);
72+
for (int i = 0; i < (int)Poly.size() - 1; ++i)
73+
S += cross(tovec(Origin, Poly[i]), tovec(Origin, Poly[i + 1]));
74+
S = fabs(S / 2.0);
75+
return S;
76+
}
77+
78+
int main()
79+
{
80+
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
81+
82+
while (cin >> n)
83+
{
84+
if (n < 1) return 0;
85+
p.clear();
86+
for (int i = 0; i < n; ++i)
87+
{
88+
double x, y; cin >> x >> y;
89+
p.push_back(point(x, y));
90+
}
91+
CVH = findCVH(p);
92+
p.push_back(p[0]);
93+
double res = (1.0 - areaPoly(p) / areaPoly(CVH)) * 100.0;
94+
cout << "Tile #" << ++C << '\n' << "Wasted Space = ";
95+
cout << fixed << setprecision(2) << res << " %\n"; cout << '\n';
96+
}
97+
}

0 commit comments

Comments
 (0)