Skip to content

Commit d43fa88

Browse files
committed
Weekly Contest 136
1 parent 84964b1 commit d43fa88

5 files changed

+154
-12
lines changed

escape-a-large-maze.cc

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
11
// Escape a Large Maze
2+
#define ALL(x) (x).begin(), (x).end()
3+
24
class Solution {
35
public:
46
bool isEscapePossible(vector<vector<int>>& bs, vector<int>& src, vector<int>& dst) {
5-
const long N = 1000001;
6-
unordered_set<long> v;
7-
for (auto &b: bs)
8-
v.insert(b[0]*N+b[1]);
9-
vector<pair<int,int>> q{{src[0], src[1]}};
10-
v.insert(src[0]*N+src[1]);
7+
vector<int> xs, ys;
8+
for (auto &b: bs) {
9+
xs.push_back(b[0]);
10+
if (b[0]+1 < 1000000) xs.push_back(b[0]+1);
11+
ys.push_back(b[1]);
12+
if (b[1]+1 < 1000000) ys.push_back(b[1]+1);
13+
}
14+
xs.push_back(0); xs.push_back(999999); xs.push_back(dst[0]);
15+
sort(ALL(xs)); xs.erase(unique(ALL(xs)), xs.end());
16+
ys.push_back(0); ys.push_back(999999); ys.push_back(dst[1]);
17+
sort(ALL(ys)); ys.erase(unique(ALL(ys)), ys.end());
18+
19+
int n = xs.size(), m = ys.size(), x, y;
20+
vector<vector<char>> v(n, vector<char>(m));
21+
for (auto &b: bs) {
22+
x = lower_bound(ALL(xs), b[0])-xs.begin();
23+
y = lower_bound(ALL(ys), b[1])-ys.begin();
24+
v[x][y] = 1;
25+
}
26+
27+
int sx = lower_bound(ALL(xs), src[0])-xs.begin();
28+
int sy = lower_bound(ALL(ys), src[1])-ys.begin();
29+
int tx = lower_bound(ALL(xs), dst[0])-xs.begin();
30+
int ty = lower_bound(ALL(ys), dst[1])-ys.begin();
31+
vector<pair<int,int>> q;
32+
q.emplace_back(sx, sy);
33+
v[sx][sy] = 1;
1134
for (int i=0; i<q.size(); i++) {
12-
int x, y;
1335
tie(x, y) = q[i];
14-
if (i == 200*200 || x == dst[0] && y == dst[1]) return true;
15-
if (x && v.insert((x-1)*N+y).second) q.emplace_back(x-1, y);
16-
if (x+1<N-1 && v.insert((x+1)*N+y).second) q.emplace_back(x+1, y);
17-
if (y && v.insert(x*N+y-1).second) q.emplace_back(x, y-1);
18-
if (y+1<N-1 && v.insert(x*N+y+1).second) q.emplace_back(x, y+1);
36+
if (x == tx && y == ty) return true;
37+
if (x && !v[x-1][y]++) q.emplace_back(x-1, y);
38+
if (x+1<n && !v[x+1][y]++) q.emplace_back(x+1, y);
39+
if (y && !v[x][y-1]++) q.emplace_back(x, y-1);
40+
if (y+1<m && !v[x][y+1]++) q.emplace_back(x, y+1);
1941
}
2042
return false;
2143
}

flower-planting-with-no-adjacent.cc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Flower Planting With No Adjacent
2+
class Solution {
3+
public:
4+
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
5+
vector<int> g[N], c(N);
6+
for (auto &p: paths) {
7+
g[p[0]-1].push_back(p[1]-1);
8+
g[p[1]-1].push_back(p[0]-1);
9+
}
10+
for (int i = 0; i < N; i++)
11+
if (!c[i]) {
12+
vector<int> q;
13+
int u = i;
14+
for(;;) {
15+
int t[6] = {};
16+
for (int v: g[u])
17+
if (c[v])
18+
t[c[v]] = 1;
19+
else
20+
c[v] = 5, q.push_back(v);
21+
for (int k = 1; ; k++)
22+
if (!t[k]) {
23+
c[u] = k;
24+
break;
25+
}
26+
if (q.empty()) break;
27+
u = q.back();
28+
q.pop_back();
29+
}
30+
}
31+
return c;
32+
}
33+
};

longest-duplicate-substring.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Longest Duplicate Substring
2+
#define FOR(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (a); i < (b); i++)
3+
#define REP(i, n) FOR(i, 0, n)
4+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
5+
6+
class Solution {
7+
public:
8+
string longestDupSubstring(string S) {
9+
const char *a = S.c_str();
10+
int n = S.size(), m = 256;
11+
int *sa = new int[n], *r = new int[n], *h = new int[max(n, 256)], *x = new int[n];
12+
fill_n(h, m, 0);
13+
copy_n(a, n, r);
14+
REP(i, n) h[r[i]]++;
15+
FOR(i, 1, m) h[i] += h[i-1];
16+
ROF(i, 0, n) sa[--h[r[i]]] = i;
17+
int k = 1;
18+
for (; ; k <<= 1) {
19+
iota(x, x+k, n-k);
20+
int j = k;
21+
REP(i, n) if (sa[i] >= k) x[j++] = sa[i]-k;
22+
fill_n(h, m, 0);
23+
REP(i, n) h[r[x[i]]]++;
24+
FOR(i, 1, m) h[i] += h[i-1];
25+
ROF(i, 0, n) sa[--h[r[x[i]]]] = x[i];
26+
fill_n(h, m, 0);
27+
m = 1;
28+
h[sa[0]] = 0;
29+
FOR(i, 1, n) {
30+
if (r[sa[i]] != r[sa[i-1]] || max(sa[i], sa[i-1]) >= n-k || r[sa[i]+k] != r[sa[i-1]+k]) m++;
31+
h[sa[i]] = m-1;
32+
}
33+
copy_n(h, n, r);
34+
if (m == n) break;
35+
}
36+
k = 0;
37+
h[0] = 0;
38+
REP(i, n)
39+
if (r[i]) {
40+
for (int j = sa[r[i]-1]; max(i, j)+k < n && a[i+k] == a[j+k]; k++);
41+
h[r[i]] = k;
42+
k && k--;
43+
}
44+
int ans = -1, ansx;
45+
REP(i, n)
46+
if (h[i] > ans)
47+
ans = h[ansx = i];
48+
string ret = S.substr(sa[ansx], ans);
49+
delete[] sa;
50+
delete[] r;
51+
delete[] h;
52+
delete[] x;
53+
return ret;
54+
}
55+
};

partition-array-for-maximum-sum.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Partition Array for Maximum Sum
2+
#define REP1(i, n) for (remove_cv<remove_reference<decltype(n)>::type>::type i = 1; i <= (n); i++)
3+
#define ROF(i, a, b) for (remove_cv<remove_reference<decltype(b)>::type>::type i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int maxSumAfterPartitioning(vector<int>& A, int K) {
8+
int n = A.size();
9+
vector<int> s(n+1);
10+
REP1(i, n) {
11+
int mx = 0;
12+
ROF(j, max(0, i-K), i) {
13+
mx = max(mx, A[j]);
14+
s[i] = max(s[i], s[j]+(i-j)*mx);
15+
}
16+
}
17+
return s[n];
18+
}
19+
};

robot-bounded-in-circle.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Robot Bounded In Circle
2+
class Solution {
3+
public:
4+
bool isRobotBounded(string instructions) {
5+
int x[2]={}, d=3;
6+
for (int i = 4; i--; )
7+
for (char c: instructions)
8+
if (c == 'L') d--;
9+
else if (c == 'R') d++;
10+
else x[d&1] += (d&2)-1;
11+
return !(x[0]|x[1]);
12+
}
13+
};

0 commit comments

Comments
 (0)