Skip to content

Commit

Permalink
Tsinghua University Bootcamp: Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Macesuted committed Aug 26, 2024
1 parent cae38ea commit b62582d
Show file tree
Hide file tree
Showing 7 changed files with 531 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @file G.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-08-26
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define maxn 200005

class UnionSet {
private:
int fa[maxn], siz[maxn];
int64_t sum;

int getfa(int p) { return fa[p] == p ? p : fa[p] = getfa(fa[p]); }
int64_t calc(int64_t v) { return v * (v - 1) / 2; }

public:
void resize(int n) {
for (int i = 1; i <= n; i++) fa[i] = i, siz[i] = 1;
sum = 0;
return;
}
void merge(int x, int y) {
x = getfa(x), y = getfa(y);
if (x == y) return;
sum -= calc(siz[x]) + calc(siz[y]);
siz[x] += siz[y], fa[y] = x;
sum += calc(siz[x]);
return;
}
int64_t query(void) { return sum; }
} US;

set<int> S;
int a[maxn], pos[maxn], n, k;

void insert(int v) {
auto p = S.insert(v).first, pl = prev(p), pr = next(p);
if (*pl != 0 && *p - *pl <= k) US.merge(*p, *pl);
if (*pr != n + 1 && *pr - *p <= k) US.merge(*p, *pr);
return;
}

void solve(void) {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i], pos[i] = i;
US.resize(n);
sort(pos + 1, pos + n + 1, [&](int x, int y) { return a[x] > a[y]; });
S.insert(0), S.insert(n + 1);
int64_t lst = 0, ans = 0;
for (int i = 1; i <= n; i++) ans += a[i];
for (int i = 1; i <= n; i++) insert(pos[i]), ans += a[pos[i]] * (US.query() - lst), lst = US.query();
cout << ans << endl;
return;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

int _ = 1;
while (_--) solve();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @file K.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-08-26
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define maxn 1005
#define maxm 10005

typedef pair<int, int> pii;

bool f[maxn][maxn];
int rest[maxn][maxn], cur[maxn][maxn];
pii h[maxm];

bool solve(void) {
int n, k, m;
cin >> n >> k >> m;
if (k <= 2) return true;
if (k == 4) return m == 1;
for (int i = 1; i <= m; i++) cin >> h[i].first >> h[i].second;
sort(h + 1, h + m + 1);
f[n][n] = true;
for (int i = n; i; i--)
for (int j = n; j; j--) {
if (!f[i][j]) continue;
if (!rest[i][j]) rest[i][j] = h[++cur[i][j]].second;
if (rest[i][j] >= i) f[i][j - 1] = true, rest[i][j - 1] = rest[i][j] - i, cur[i][j - 1] = cur[i][j];
if (rest[i][j] >= j) f[i - 1][j] = true, rest[i - 1][j] = rest[i][j] - j, cur[i - 1][j] = cur[i][j];
}
for (int i = 1; i <= n; i++)
if (f[i][0] || f[0][i]) return true;
return false;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

int _ = 1;
while (_--) cout << (solve() ? "YES" : "NO") << endl;

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @file L.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-08-26
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'
#define maxn 20

int64_t a[maxn][maxn], b[maxn], Sl[1024], Sr[1024];

void solve(void) {
int n, m;
int64_t s;
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) cin >> a[i][j];
cin >> s;
int tm = m / 2, om = m - tm;
for (int S = 0; S < (1 << n); S++) {
for (int j = 0; j < m; j++) b[j] = 0;
for (int j = 0; j < (1 << tm); j++) Sl[j] = 0;
for (int j = 0; j < (1 << om); j++) Sr[j] = 0;
for (int i = 0; i < n; i++)
if (S >> i & 1)
for (int j = 0; j < m; j++) b[j] += a[i][j];
for (int j = 0; j < tm; j++) Sl[1 << j] = b[j];
for (int j = 0; j < om; j++) Sr[1 << j] = b[tm + j];
unordered_map<int64_t, int> St;
for (int T = 0; T < (1 << tm); T++) {
if (T) {
int lb = T & -T;
Sl[T] = Sl[lb] + Sl[T ^ lb];
}
St[Sl[T]] = T;
}
for (int T = 0; T < (1 << om); T++) {
if (T) {
int lb = T & -T;
Sr[T] = Sr[lb] + Sr[T ^ lb];
}
if (St.count(s - Sr[T])) {
int64_t rT = St[s - Sr[T]] | (T << tm);
cout << "YES" << endl;
vector<pair<int, int>> ans;
for (int i = 0; i < n; i++)
if (!(S >> i & 1)) ans.emplace_back(1, i + 1);
for (int j = 0; j < m; j++)
if (!(rT >> j & 1)) ans.emplace_back(2, j + 1);
cout << ans.size() << endl;
for (auto [x, y] : ans) cout << x << ' ' << y << endl;
return;
}
}
}
cout << "NO" << endl;
return;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

int _ = 1;
while (_--) solve();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @file A.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-08-26
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

#define maxn 5005

typedef pair<int, int> pii;

pii a[maxn];

int64_t dis(pii x, pii y) { return abs(x.first - y.first) + abs(x.second - y.second); }

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
vector<pii> answer;
while (true) {
bool chk = true;
for (int i = 2; i <= n; i++) chk &= (a[1] == a[i]);
if (chk) break;
int p = 2;
while (a[p] == a[1]) p++;
for (int i = p + 1; i <= n; i++)
if (a[1] != a[i] && dis(a[i], a[1]) < dis(a[p], a[1])) p = i;
if (a[1].first > a[p].first) swap(a[1], a[p]);
int tx = (a[1].first + a[p].first) / 2, ty = (a[1].second + a[p].second) / 2;
if (abs(a[1].first - tx) > abs(a[p].first - tx)) tx--;
if (abs(a[1].second - ty) < abs(a[p].second - ty)) ty += (ty < a[p].second ? +1 : -1);
int tot = min(abs(a[1].first - tx) + abs(a[1].second - ty), abs(a[p].first - tx) + abs(a[p].second - ty)) + 1;
for (int i = 1; i <= n; i++) {
int dx = abs(a[i].first - tx), dy = abs(a[i].second - ty), tim = tot;
if (tim >= dx)
a[i].first = tx, tim -= dx;
else
a[i].first += (a[i].first < tx ? +tim : -tim), tim = 0;
if (tim >= dy)
a[i].second = ty, tim -= dy;
else
a[i].second += (a[i].second < ty ? +tim : -tim), tim = 0;
}
answer.emplace_back(tx, ty);
}
cout << answer.size() << endl;
for (auto [x, y] : answer) cout << x << ' ' << y << endl;
return;
}

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

int _ = 1;
while (_--) solve();

return 0;
}
Loading

0 comments on commit b62582d

Please sign in to comment.