-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3af0a8b
commit addffba
Showing
43 changed files
with
867 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, k; cin >> n >> k; | ||
int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; | ||
int dp[405][405]; | ||
|
||
int sum = 0, mx = 0; | ||
for (int i = 1; i <= n; i++) | ||
sum += a[i], mx = max(mx, a[i]), dp[i][0] = mx * i - sum; | ||
|
||
for (int i = 0; i <= n; i++) for (int j = 1; j <= k; j++) { | ||
if (i == 0) dp[i][j] = 0; | ||
|
||
int sum = 0, mx = 0; dp[i][j] = 1e9; | ||
for (int x = i; x > 0; x--) { | ||
mx = max(mx, a[x]); sum += a[x]; | ||
dp[i][j] = min(dp[i][j], dp[x - 1][j - 1] + mx * (i - x + 1) - sum); | ||
} | ||
} | ||
|
||
cout << dp[n][k]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
int a[2][n]; for (int i = 0; i < 2; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; | ||
int ans = 0, cur = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (a[0][i] == 0) { | ||
if (cur >= 1) ans += 2 + cur; | ||
cur = 0; | ||
} else cur++; | ||
} | ||
if (cur >= 1) ans += 2 + cur; cur = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (i % 2 == 0 && a[0][i] == 1 && a[1][i] == 1) ans -= 2; | ||
if (a[1][i] == 0) { | ||
if (cur >= 1) ans += 2 + cur; | ||
cur = 0; | ||
} else cur++; | ||
} | ||
if (cur >= 1) ans += 2 + cur; cur = 0; | ||
cout << ans; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; | ||
|
||
int ans[n + 1]; fill(ans, ans + n + 1, 1e9); | ||
for (int i = 0; i < n; i++) { | ||
int l = i, r = i, cur = 0; | ||
while (l >= 0 && r < n) { | ||
cur += abs(a[l] - a[r]); | ||
ans[r - l + 1] = min(ans[r - l + 1], cur); | ||
l--; r++; | ||
} | ||
if (i == n - 1) continue; | ||
l = i, r = i + 1, cur = 0; | ||
while (l >= 0 && r < n) { | ||
cur += abs(a[l] - a[r]); | ||
ans[r - l + 1] = min(ans[r - l + 1], cur); | ||
l--; r++; | ||
} | ||
} | ||
for (int i = 1; i <= n; i++) cout << ans[i] << ' '; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
const int m = 1e9 + 7; | ||
|
||
struct dat { | ||
pair<int, int> mn, mx; int cnt; | ||
dat() : mn(1e9, -1), mx(-1e9, -1), cnt(0) {}; | ||
dat(int i, int v) : mn(v, i), mx(v, i), cnt(1) {}; | ||
}; | ||
|
||
int n, k, a[300005], fac[300005], inv_fac[300005]; | ||
dat t[1048576]; | ||
|
||
dat combine(const dat d1, const dat d2) { | ||
dat tmp; | ||
tmp.mn = min(d1.mn, d2.mn); | ||
tmp.mx = max(d1.mx, d2.mx); | ||
tmp.cnt = d1.cnt + d2.cnt; | ||
return tmp; | ||
} | ||
|
||
void build(int idx, int l, int r) { | ||
if (l == r) { t[idx] = dat(l, a[l]); return; } | ||
int m = (l + r) >> 1; | ||
build(idx * 2, l, m); | ||
build(idx * 2 + 1, m + 1, r); | ||
t[idx] = combine(t[idx * 2], t[idx * 2 + 1]); | ||
} | ||
|
||
dat query(int idx, int l, int r, int tl, int tr) { | ||
if (tl > tr) return dat(); | ||
if (l == tl && r == tr) return t[idx]; | ||
int m = (l + r) >> 1; | ||
return combine(query(idx * 2, l, m, tl, min(tr, m)), query(idx * 2 + 1, m + 1, r, max(m + 1, tl), tr)); | ||
} | ||
|
||
void del(int idx, int l, int r, int p) { | ||
if (l == r) { t[idx] = dat(); return; } | ||
int m = (l + r) >> 1; | ||
if (p <= m) del(idx * 2, l, m, p); | ||
else del(idx * 2 + 1, m + 1, r, p); | ||
t[idx] = combine(t[idx * 2], t[idx * 2 + 1]); | ||
} | ||
|
||
int exp_mod(int a, int b) { | ||
int r = 1; | ||
while (b) { | ||
if (b & 1) r = r * a % m; | ||
a = a * a % m; | ||
b /= 2; | ||
} return r; | ||
} | ||
|
||
int nck(int n, int k) { | ||
if (k < 0 || k > n) return 0; | ||
return fac[n] * inv_fac[k] % m * inv_fac[n - k] % m; | ||
} | ||
|
||
int solve(int l, int r) { | ||
dat res = query(1, 1, n, l, r); | ||
if (res.cnt == 0) return 1; | ||
if (res.mx.first + res.mn.first > k) { | ||
return solve(l, res.mx.second - 1) * solve(res.mx.second + 1, r) % m; | ||
} else { | ||
int freq = 0, orig = res.mn.first; | ||
while (res.mn.first == orig) { | ||
del(1, 1, n, res.mn.second); | ||
freq++; | ||
res = query(1, 1, n, l, r); | ||
} | ||
return solve(l, r) * nck(res.cnt + freq, freq) % m; | ||
} | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
cin >> n >> k; fac[0] = 1; | ||
for (int i = 1; i <= n; i++) { | ||
cin >> a[i]; | ||
fac[i] = fac[i - 1] * i % m; | ||
} | ||
inv_fac[n] = exp_mod(fac[n], m - 2); | ||
for (int i = n - 1; i >= 0; i--) inv_fac[i] = inv_fac[i + 1] * (i + 1) % m; | ||
build(1, 1, n); | ||
cout << solve(1, n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
pair<int, int> a[n]; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; | ||
|
||
int cnt = 0, cur = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (cur > a[i].second) cur = 0, cnt++; | ||
cur = max(cur, a[i].first); | ||
} | ||
|
||
if (cur > 0) cnt++; | ||
|
||
cout << cnt; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[editor] | ||
line_wrapping=false | ||
line_break_column=72 | ||
auto_continue_multiline=true | ||
|
||
[file_prefs] | ||
final_new_line=true | ||
ensure_convert_new_lines=false | ||
strip_trailing_spaces=false | ||
replace_tabs=false | ||
|
||
[indentation] | ||
indent_width=4 | ||
indent_type=1 | ||
indent_hard_tab_width=8 | ||
detect_indent=false | ||
detect_indent_width=false | ||
indent_mode=2 | ||
|
||
[project] | ||
name=ACMICPC | ||
base_path=C:\\Users\\shuce\\projects\\ACMICPC\\ | ||
description= | ||
|
||
[long line marker] | ||
long_line_behaviour=1 | ||
long_line_column=72 | ||
|
||
[files] | ||
current_page=5 | ||
FILE_NAME_0=0;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C30860.cpp;0;4 | ||
FILE_NAME_1=693;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C17192.cpp;0;4 | ||
FILE_NAME_2=1506;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C28477.cpp;0;4 | ||
FILE_NAME_3=690;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C28307.cpp;0;4 | ||
FILE_NAME_4=226;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C28313.cpp;0;4 | ||
FILE_NAME_5=73;C++;0;EUTF-8;1;1;0;C%3A%5CUsers%5Cshuce%5Cprojects%5CACMICPC%5C27353.cpp;0;4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, k; cin >> n >> k; | ||
int a[k]; for (int i = 0; i < k; i++) cin >> a[i]; | ||
sort(a, a + k); | ||
if (k & 1) { | ||
int p1[k], p2[k]; p1[0] = 0; p2[k - 1] = 0; | ||
for (int i = 1; i < k; i += 2) | ||
p1[i] = p1[i + 1] = p1[i - 1] + a[i] - a[i - 1]; | ||
for (int i = k - 2; i >= 0; i -= 2) | ||
p2[i] = p2[i - 1] = p2[i + 1] + a[i + 1] - a[i]; | ||
int ans = 1e9; | ||
for (int i = 0; i < k; i++) ans = min(ans, p1[i] + p2[i]); | ||
cout << ans; | ||
} else { | ||
int ans = 0; | ||
for (int i = 0; i < k; i += 2) ans += a[i + 1] - a[i]; | ||
cout << ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n, k; cin >> n >> k; | ||
int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; | ||
|
||
multiset<int> st1, st2; int s1 = 0, s2 = 0; | ||
for (int i = 0; i < k; i++) st1.insert(a[i]), s1 += a[i]; | ||
while (st1.size() - 1 > st2.size()) { | ||
int v = *--st1.end(); | ||
s1 -= v; s2 += v; | ||
st2.insert(v); | ||
st1.erase(--st1.end()); | ||
} | ||
|
||
int v = *--st1.end(); | ||
cout << (v * st1.size()) - s1 + s2 - (v * st2.size()) << ' '; | ||
|
||
for (int i = k; i < n; i++) { | ||
if (a[i - k] <= *--st1.end()) st1.erase(st1.find(a[i - k])), s1 -= a[i - k]; | ||
else st2.erase(st2.find(a[i - k])), s2 -= a[i - k]; | ||
|
||
st1.insert(a[i]), s1 += a[i]; | ||
|
||
if (!st2.empty()) { | ||
int tmp = *st2.begin(); | ||
st1.insert(tmp), s1 += tmp, s2 -= tmp, st2.erase(st2.begin()); | ||
} | ||
|
||
while (st1.size() - 1 > st2.size()) { | ||
int v = *--st1.end(); | ||
s1 -= v; s2 += v; | ||
st2.insert(v); | ||
st1.erase(--st1.end()); | ||
} | ||
|
||
int v = *--st1.end(); | ||
cout << (v * st1.size()) - s1 + s2 - (v * st2.size()) << ' '; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int n, m, depth[100005], bridge[100005]; | ||
bool visited[100005], ok = true; | ||
vector<int> g[100005]; | ||
vector<pair<int, int>> ans; | ||
|
||
void dfs(int v, int p) { | ||
visited[v] = 1; | ||
bridge[v] = depth[v] = depth[p] + 1; | ||
for (const auto it : g[v]) { | ||
if (it == p) continue; | ||
if (depth[v] < depth[it]) continue; | ||
if (visited[it]) bridge[v] = min(bridge[v], depth[it]); | ||
else dfs(it, v); | ||
bridge[v] = min(bridge[v], bridge[it]); | ||
ans.push_back({v, it}); | ||
} | ||
|
||
if (bridge[v] == depth[v] && p != 0) ok = false; | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
cin >> n >> m; | ||
for (int i = 0; i < m; i++) { | ||
int a, b; cin >> a >> b; | ||
g[a].push_back(b); | ||
g[b].push_back(a); | ||
} | ||
|
||
dfs(1, 0); | ||
if (ok && ans.size() == m) for (const auto it : ans) cout << it.first << ' ' << it.second << '\n'; | ||
else cout << "IMPOSSIBLE"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
#define int long long | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int n; cin >> n; | ||
int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; | ||
sort(a, a + n); | ||
int cur = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (a[i] - cur >= 2) { | ||
cout << cur + 1; | ||
return 0; | ||
} | ||
cur += a[i]; | ||
} | ||
cout << cur + 1; | ||
} |
Oops, something went wrong.