Skip to content

Commit 5382651

Browse files
committed
misc
1 parent eff3b62 commit 5382651

23 files changed

+597
-0
lines changed

apply-operations-to-an-array.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Apply Operations to an Array
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
vector<int> applyOperations(vector<int>& a) {
7+
int n = a.size();
8+
REP(i, n-1)
9+
if (a[i] == a[i+1])
10+
a[i]*=2, a[i+1]=0;
11+
int j = 0;
12+
REP(i, n)
13+
if (a[i])
14+
a[j++] = a[i];
15+
fill(a.begin()+j, a.end(), 0);
16+
return a;
17+
}
18+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Divide Intervals Into Minimum Number of Groups
2+
#define ALL(x) (x).begin(), (x).end()
3+
using pii = pair<int, int>;
4+
5+
class Solution {
6+
public:
7+
int minGroups(vector<vector<int>>& a) {
8+
vector<pii> b;
9+
int s = 0, c = 0;
10+
for (auto &x : a) {
11+
b.emplace_back(x[0], 1);
12+
b.emplace_back(x[1]+1, -1);
13+
}
14+
sort(ALL(b));
15+
for (auto [x, d] : b) {
16+
c += d;
17+
s = max(s, c);
18+
}
19+
return s;
20+
}
21+
};

find-all-good-indices.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Find All Good Indices
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
vector<int> goodIndices(vector<int>& a, int k) {
8+
int n = a.size(), s = 1;
9+
vector<int> b(n), r;
10+
ROF(i, 0, n-1) {
11+
b[i] = s;
12+
if (a[i] <= a[i+1]) s++;
13+
else s = 1;
14+
}
15+
s = 1;
16+
FOR(i, 1, n) {
17+
if (min(b[i], s) >= k)
18+
r.push_back(i);
19+
if (a[i-1] >= a[i]) s++;
20+
else s = 1;
21+
}
22+
return r;
23+
}
24+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Find The Original Array of Prefix Xor
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
4+
class Solution {
5+
public:
6+
vector<int> findArray(vector<int>& pref) {
7+
int n = pref.size();
8+
vector<int> a(n);
9+
a[0] = pref[0];
10+
FOR(i, 1, n)
11+
a[i] = pref[i-1] ^ pref[i];
12+
return a;
13+
}
14+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Length of the Longest Alphabetical Continuous Substring
2+
class Solution {
3+
public:
4+
int longestContinuousSubstring(string s) {
5+
int n = s.size(), r = 0;
6+
for (int j = 0, i = 0; i < n; ) {
7+
for (; j<n && s[i]==s[j]-(j-i); j++);
8+
r = max(r, j-i);
9+
i = j;
10+
}
11+
return min(r, 26);
12+
}
13+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Longest Subarray With Maximum Bitwise AND
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int longestSubarray(vector<int>& a) {
7+
int n = a.size(), x = *max_element(ALL(a)), c = 0, r = 0;
8+
for (int v: a)
9+
if (v != x)
10+
c = 0;
11+
else
12+
r = max(r, ++c);
13+
return r;
14+
}
15+
};

maximum-deletions-on-a-string.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Maximum Deletions on a String
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
#define ROF(i, a, b) for (long i = (b); --i >= (a); )
4+
5+
class Solution {
6+
public:
7+
int deleteString(string a) {
8+
int n = a.size();
9+
vector<int> dp(n+1);
10+
ROF(i, 0, n) {
11+
dp[i] = 1;
12+
int mx = (int(i)+n)/2+1;
13+
FOR(j, i+1, mx) {
14+
if (dp[j] < dp[i]) continue;
15+
int k = 0;
16+
for (; i+k < j && a[i+k] == a[j+k]; k++);
17+
if (i+k == j)
18+
dp[i] = dp[j]+1;
19+
}
20+
}
21+
return dp[0];
22+
}
23+
};

maximum-sum-of-an-hourglass.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Maximum Sum of an Hourglass
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
class Solution {
5+
public:
6+
int maxSum(vector<vector<int>>& a) {
7+
int m = a.size(), n = a[0].size(), s = 0;
8+
REP(i, m-2)
9+
REP(j, n-2) {
10+
s = max(s, a[i][j]+a[i][j+1]+a[i][j+2] +
11+
a[i+1][j+1]+
12+
a[i+2][j]+a[i+2][j+1]+a[i+2][j+2]);
13+
}
14+
return s;
15+
}
16+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Maximum Sum of Distinct Subarrays With Length K
2+
#define FOR(i, a, b) for (long i = (a); i < (b); i++)
3+
#define REP(i, n) for (long i = 0; i < (n); i++)
4+
5+
class Solution {
6+
public:
7+
long long maximumSubarraySum(vector<int>& a, int k) {
8+
long n = a.size(), s = 0, sum = 0;
9+
unordered_map<int, int> mm;
10+
vector<int> c(n+1);
11+
REP(i, k-1) {
12+
int &t = mm[a[i]];
13+
c[t]--;
14+
c[++t]++;
15+
sum += a[i];
16+
}
17+
FOR(i, k-1, n) {
18+
int &t = mm[a[i]];
19+
c[t]--;
20+
c[++t]++;
21+
sum += a[i];
22+
if (c[1] == k)
23+
s = max(s, sum);
24+
int &t1 = mm[a[i-k+1]];
25+
c[t1]--;
26+
c[--t1]++;
27+
sum -= a[i-k+1];
28+
}
29+
return s;
30+
}
31+
};

minimize-xor.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Minimize Xor
2+
class Solution {
3+
public:
4+
int minimizeXor(int a, int b) {
5+
int c = 0, t;
6+
b = __builtin_popcount(b);
7+
for (; a && b; a ^= t, b--)
8+
c |= t = 1 << 31-__builtin_clz(a);
9+
for (; b; b--)
10+
c |= c+1 & ~c;
11+
return c;
12+
}
13+
};

minimum-total-distance-traveled.cc

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Minimum Total Distance Traveled
2+
#define REP(i, n) for (long i = 0; i < (n); i++)
3+
4+
namespace {
5+
const long N = 100, V = N*2+2;
6+
struct Edge { int v, c, w, next; } e[(N+N+N*N) << 1];
7+
long h[V], hsrc;
8+
int head[V], q[V], allo;
9+
bool vis[V];
10+
11+
void insert(int u, int v, int c, int w) {
12+
e[allo] = {v, c, w, head[u]};
13+
head[u] = allo++;
14+
e[allo] = {u, 0, -w, head[v]};
15+
head[v] = allo++;
16+
}
17+
18+
bool relabel(int n, int src, int sink) {
19+
int hd = 0, tl = 1;
20+
q[0] = sink;
21+
fill_n(vis, n, false);
22+
fill_n(h, n, LONG_MAX/2);
23+
h[sink] = 0;
24+
vis[sink] = true;
25+
while (hd != tl) {
26+
int u = q[hd++], v;
27+
long t;
28+
if (hd == n) hd = 0;
29+
vis[u] = false;
30+
for (int i = head[u]; i >= 0; i = e[i].next)
31+
if (e[i^1].c && (t = h[u]-e[i].w) < h[v = e[i].v]) {
32+
h[v] = t;
33+
if (! vis[v]) {
34+
vis[v] = true;
35+
if (t <= h[hd != tl ? q[hd] : src])
36+
hd == 0 && (hd = n), q[--hd] = v;
37+
else
38+
q[tl++] = v, tl == n && (tl = 0);
39+
}
40+
}
41+
}
42+
REP(u, n)
43+
for (int i = head[u]; i >= 0; i = e[i].next)
44+
e[i].w += h[e[i].v]-h[u];
45+
hsrc += h[src];
46+
return h[src] < LONG_MAX/2;
47+
}
48+
49+
int augment(int sink, int u, int f) {
50+
if (u == sink) return f;
51+
vis[u] = true;
52+
int old = f;
53+
for (int i = head[u]; i >= 0; i = e[i].next)
54+
if (e[i].c && ! vis[e[i].v] && ! e[i].w) {
55+
int ff = augment(sink, e[i].v, min(f, e[i].c));
56+
e[i].c -= ff;
57+
e[i^1].c += ff;
58+
if (! (f -= ff)) break;
59+
}
60+
return old-f;
61+
}
62+
}
63+
64+
class Solution {
65+
public:
66+
long long minimumTotalDistance(vector<int>& a, vector<vector<int>>& b) {
67+
const int m = a.size(), n = b.size(), src = m+n, sink = src+1;
68+
allo = 0;
69+
fill_n(head, sink+1, -1);
70+
REP(i, m)
71+
insert(src, i, 1, 0);
72+
REP(i, n)
73+
insert(m+i, sink, b[i][1], 0);
74+
REP(i, m)
75+
REP(j, n)
76+
insert(i, m+j, 1, abs(a[i]-b[j][0]));
77+
fill_n(h, sink+1, 0);
78+
hsrc = 0;
79+
long w, cost = 0;
80+
while (relabel(sink+1, src, sink))
81+
while (fill_n(vis, sink+1, false), w = augment(sink, src, INT_MAX))
82+
cost += w*hsrc;
83+
return cost;
84+
};
85+
};
86+
87+
/// minimum-cost flow: successive shortest path algorithm
88+
89+
#define ALL(x) (x).begin(), (x).end()
90+
#define REP(i, n) for (long i = 0; i < (n); i++)
91+
92+
namespace {
93+
const int N = 100, V = N*2+2, E = N+N+N*N;
94+
struct Edge { int v, c, w, next; } e[E << 1];
95+
long h[V];
96+
int q[V], head[V], pre[V], allo;
97+
bool in[V];
98+
99+
void insert(int u, int v, int c, int w) {
100+
e[allo] = {v, c, w, head[u]};
101+
head[u] = allo++;
102+
e[allo] = {u, 0, -w, head[v]};
103+
head[v] = allo++;
104+
}
105+
106+
bool labelCorrecting(int n, int src, int sink) {
107+
int *hd = q, *tl = q+1;
108+
fill_n(in, n, false);
109+
fill_n(h, n, LONG_MAX);
110+
h[q[0] = src] = 0;
111+
while (hd != tl) {
112+
int u = *hd++;
113+
if (hd == q+n) hd = q;
114+
in[u] = false;
115+
for (int i = head[u]; i >= 0; i = e[i].next)
116+
if (e[i].c > 0 && h[u]+e[i].w < h[e[i].v]) {
117+
h[e[i].v] = h[u]+e[i].w;
118+
pre[e[i].v] = i;
119+
if (! in[e[i].v]) {
120+
*tl++ = e[i].v;
121+
if (tl == q+n) tl = q;
122+
in[e[i].v] = true;
123+
}
124+
}
125+
}
126+
return h[sink] < LONG_MAX;
127+
}
128+
129+
long minCost(int n, int src, int sink) {
130+
long cost = 0, flow = 0;
131+
while (labelCorrecting(n, src, sink)) {
132+
int f = INT_MAX;
133+
for (int v = sink; v != src; v = e[pre[v]^1].v)
134+
f = min(f, e[pre[v]].c);
135+
for (int v = sink; v != src; v = e[pre[v]^1].v) {
136+
e[pre[v]].c -= f;
137+
e[pre[v]^1].c += f;
138+
}
139+
cost += f*h[sink];
140+
flow += f;
141+
}
142+
return cost;
143+
}
144+
}
145+
146+
class Solution {
147+
public:
148+
long long minimumTotalDistance(vector<int>& a, vector<vector<int>>& b) {
149+
const int m = a.size(), n = b.size(), src = m+n, sink = src+1;
150+
allo = 0;
151+
fill_n(head, sink+1, -1);
152+
REP(i, m)
153+
insert(src, i, 1, 0);
154+
REP(i, n)
155+
insert(m+i, sink, b[i][1], 0);
156+
REP(i, m)
157+
REP(j, n)
158+
insert(i, m+j, 1, abs(a[i]-b[j][0]));
159+
return minCost(sink+1, src, sink);
160+
};
161+
};

most-frequent-even-element.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Most Frequent Even Element
2+
#define ALL(x) (x).begin(), (x).end()
3+
4+
class Solution {
5+
public:
6+
int mostFrequentEven(vector<int>& a) {
7+
int n = a.size(), s = -1, c = 0;
8+
sort(ALL(a));
9+
for (int j = 0, i = 0; i < n; ) {
10+
for (; j < n && a[i]==a[j]; j++);
11+
if (j-i > c && a[i]%2==0)
12+
c = j-i, s = a[i];
13+
i = j;
14+
}
15+
return s;
16+
}
17+
};

number-of-common-factors.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Number of Common Factors
2+
class Solution {
3+
public:
4+
int commonFactors(int a, int b) {
5+
int s = 0;
6+
a = gcd(a, b);
7+
for (int i = 1; i*i <= a; i++)
8+
if (a%i == 0)
9+
s++, s += i*i != a;
10+
return s;
11+
}
12+
};

0 commit comments

Comments
 (0)