Skip to content

Commit

Permalink
add codes
Browse files Browse the repository at this point in the history
  • Loading branch information
apple authored and apple committed Mar 30, 2019
0 parents commit c22c620
Show file tree
Hide file tree
Showing 54 changed files with 4,714 additions and 0 deletions.
58 changes: 58 additions & 0 deletions 16diagonals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
g = [[-1]*5 for i in range(5)]
def poss(c, i, j):
if c == '\\':
if i>0 and g[i-1][j] == '/':
return False
if j>0 and g[i][j-1] == '/':
return False
if i > 0 and j > 0 and g[i-1][j-1] == '\\':
return False
if i < 4 and j < 4 and g[i+1][j+1] == '\\':
return False
else:
if i>0 and g[i-1][j] == '\\':
return False
if j>0 and g[i][j-1] == '\\':
return False
if i > 0 and j > 0 and g[i-1][j-1] == '/':
return False
if i < 4 and j < 4 and g[i+1][j+1] == '/':
return False
return True
mx = 0

def printG():
for row in g:
print(row)

def kill(i, j, sol, size):
global mx, g
# mx = max(mx, len(sol))
if len(sol) == size:
print(g)
return
if i >= 5 or j >= 5 or (i, j) in sol:
return
print (i, j)
# printG()
c = g[i][j]
if poss('/', i, j):
sol.append((i, j))
g[i][j] = '/'
kill(i+1, j, sol, size)
kill(i, j+1, sol, size)
# g[i][j] = c
sol.pop(-1)
if poss('\\', i, j):
sol.append((i, j))
g[i][j] = '\\'
kill(i+1, j, sol, size)
kill(i, j+1, sol, size)
# g[i][j] = c
sol.pop(-1)

kill(i+1, j, sol, size)
kill(i, j+1, sol, size)
g[i][j] = c
kill(0, 0, [], 16)
# print(mx)
88 changes: 88 additions & 0 deletions 2ndBestMST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int nax = (int)1e3 + 5;
int par[nax][23], mx[nax][23], level[nax];
int LG;
vector<pair<int, int> > g[nax];

void dfs(int s, int p, int w) {
par[s][0] = p, mx[s][0] = w;
for (int lg=1; lg<LG; lg++) {
if (par[s][lg-1] != 0) {
par[s][lg] = par[par[s][lg-1]][lg-1];
mx[s][lg] = max(mx[s][lg], max(mx[par[s][lg-1]][lg-1], mx[s][lg-1]));
// cout << (1<<lg) << "th parent of " << s << " is " << par[s][lg] << "\n";
// cout << "max from " << s << " to " << par[s][lg] << " is " << mx[s][lg] << "\n";
}
}

for (auto& x: g[s]) {
int v = x.first, w_ = x.second;
if (p != v) {
level[v] = level[s] + 1;
dfs(v, s, w_);
}
}
}

int lca(int u, int v) {
if (level[u] < level[v]) swap(u, v);
// cout << level[u] << " " << level[v] << "\n";
int diff = level[u] - level[v];
for (int i=LG; i>=0; i--) {
if (diff & (1<<i)) {
u = par[u][i];
}
}
if(u == v) return u;
for (int i=LG; i>=0; i--) {
if (par[u][i] != par[v][i]) {
u = par[u][i], v = par[v][i];
}
}
return par[u][0];
}

// u -> from, v -> to
int max_query(int u, int v) {
if (level[u] < level[v]) swap(u, v);
int diff = level[u] - level[v];
int ans = 0;
for (int i=LG; i>=0; i--) {
if (diff & (1<<i)) {
ans = max(ans, mx[u][i]);
u = par[u][i];
}
}
if(u == v) return ans;
for (int i=LG; i>=0; i--) {
if (par[u][i] != par[v][i]) {
ans = max(ans, max(mx[u][i], mx[v][i]));
u = par[u][i], v = par[v][i];
}
}
return max(ans, mx[u][0]);
}

int main() {
int n, m, u, v, w;
cin >> n >> m;
for (int i=0; i<m; i++) {
cin >> u >> v >> w;
g[u].push_back(make_pair(v, w));
g[v].push_back(make_pair(u, w));
}
while ((1 << LG) < n) LG += 1;
dfs(1, 0, 0);
cin >> u >> v;
while (u != -1) {
int _lca = lca(u, v);
int ans = max(max_query(u, _lca), max_query(v, _lca));
cout << _lca << " max: " << ans << "\n";

cin >> u >> v;
}
return 0;
}
58 changes: 58 additions & 0 deletions 2seals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<iostream>
#include<vector>
using namespace std;

struct Seal {
int x, y;
Seal(int a, int b): x(a), y(b) {}
int area() {
return x*y;
}
};

vector<Seal> arr;
int n, a, b, ans;

bool canFit(int i) {
return (max(arr[i].x, arr[i].y) <= max(a, b));
}

void cal(int i, int j, int a, int b) {
if ((arr[i].x + arr[j].x) <= a and max(arr[i].y, arr[j].y)<=b) {
ans = max(ans, arr[i].area()+arr[j].area());
}
else if ((arr[i].x + arr[j].y) <= a and max(arr[i].y, arr[j].x)<=b) {
ans = max(ans, arr[i].area()+arr[j].area());
}
else if ((arr[i].y + arr[j].x) <= a and max(arr[i].x, arr[j].y)<=b) {
ans = max(ans, arr[i].area()+arr[j].area());
}
else if ((arr[i].y + arr[j].y) <= a and max(arr[i].x, arr[j].x)<=b) {
ans = max(ans, arr[i].area()+arr[j].area());
}
}

int main() {
int x, y, i=0;
cin >> n >> a >> b;
while (i++<n) {
cin >> x >> y;
arr.push_back(Seal(x, y));
}
// sort(arr.begin(), arr.end(), [](Seal& x, Seal& y) {
// return x.area() > y.area();
// });
for (i=0; i<n; i++) {
if (!canFit(i)) {
continue;
}
for (int j=i+1; j<n; j++) {
if (!canFit(j)) continue;
// cout << i << " " << j << endl;
cal(i, j, a, b);
cal(i, j, b, a);
}
}
cout << ans << endl;
return 0;
}
101 changes: 101 additions & 0 deletions A-large-practice.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
100
42
11
1
2018
8443257735395719
3208453465779946
2043439400806565
5323780492592791
4335359103044849
3187971487657685
965135100884463
2
1645105264890959
1651013940130118
1107629925327913
8
9
1555555555555555
7
3679611869180437
10
7938060657258174
4825975801190870
3127662647418319
2993661506096990
5033611206310619
6522973834713685
7985186550336178
3347562352608270
1326339744282004
7480603363132169
8240988447020423
1337371646092875
5142840810688781
4204566031065241
8315103468096997
1550540757242824
6
5
3301511805182441
2034261063973180
3
1624607553992255
496751776148768
3878990487719733
233930138507428
189692857078347
4052077684071508
1958109005670493
10000000000000000
6971957867112306
8888888888888889
5283062679867123
8440651276745840
1375527603528762
8715584141883134
8163450696797725
6788732883967888
9999999999999999
5347363169457821
4298029790558041
5805064716657129
1806791933758186
7272660783617992
3660610777438848
5843361730579456
6080334421419632
5155470161652371
4
5209026359028
117807724110664
8125372526658496
2790825644477351
95
3503811308886195
1936894310448265
6458483610398552
9600164625148179
1253603953337480
2796277510156695
6329603104128405
5899328494745306
4229736524117628
3436726949125577
564317895518012
902535784477749
4269664682284824
2471816536645070
2328169568197412
2025267255919963
1048822921539168
9756513335710382
9205454041025047
2072819705362884
4618304723087462
2133096072293059
4805821693525465
4241638601957121
9360530591816075
8922861411369607
Loading

0 comments on commit c22c620

Please sign in to comment.