Tutorial
Tutorial is loading...
Code
#include <iostream>
using namespace std;
void solve() {
int t, a, b, x, y;
cin >> t >> a >> b >> x >> y;
auto solve = [&](int t, int a, int b, int x, int y) {
int cur = 0;
cur += max((t - a + x) / x, 0);
t -= max((t - a + x) / x, 0) * x;
cur += max((t - b + y) / y, 0);
return cur;
};
cout << max(solve(t, a, b, x, y), solve(t, b, a, y, x)) << endl;
}
signed main() {
int q = 1;
cin >> q;
while (q --> 0)
solve();
return 0;
}
Tutorial
Tutorial is loading...
Code
def solve():
w, h, a, b = map(int, input().split())
x1, y1, x2, y2 = map(int, input().split())
if x1 == x2:
if abs(y1 - y2) % b == 0:
return "Yes"
else:
return "No"
if y1 == y2:
if abs(x1 - x2) % a == 0:
return "Yes"
else:
return "No"
if (x1 - x2) % a == 0 or (y1 - y2) % b == 0:
return "Yes"
return "No"
t = int(input())
for _ in range(t):
print(solve())
Tutorial
Tutorial is loading...
Code
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_map>
#include <random>
#include <chrono>
#include <cassert>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <queue>
#include <unordered_set>
#include <fstream>
#include <random>
using namespace std;
using ll = long long;
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 500;
int sum[MAXN + 1][MAXN + 1];
int n, m, k;
int check(int i, int mx) {
return min(max(i, 0), mx);
}
int pref(int i, int j) {
return sum[check(i, n)][check(j, m)];
}
void solve() {
cin >> n >> m >> k; k--;
vector<string> mine(n);
int all_gold = 0;
for (int i = 0; i < n; i++) {
cin >> mine[i];
for (int j = 0; j < m; j++) {
all_gold += (mine[i][j] == 'g');
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + (mine[i][j] == 'g');
}
}
int ans = all_gold;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mine[i][j] == '.') {
int a = i - k, b = i + k + 1, c = j - k, d = j + k + 1;
ans = min(ans, pref(b, d) - pref(a, d) - pref(b, c) + pref(a, c));
}
}
}
cout << all_gold - ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
}
Tutorial
Tutorial is loading...
Code
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) ((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const char en = '\n';
const int INF = 1e9 + 7;
const ll INFLL = 1e18;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifdef LOCAL
#include "debug.h"
#define numtest(x) cerr << "Test #" << (x) << ": " << endl;
#else
#define debug(...) 42
#define numtest(x) 42
#endif
int merge(const vector<int> &a, const vector<int> &b) {
int n = sz(a);
int res = 0;
for (int c = 0, i = 0, j = 0; c < n; ++c) {
if (a[i] > b[j]) {
++res;
++i;
} else if (a[i] < b[j]) {
++j;
} else {
assert(0);
}
}
return res;
}
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
vector<int> pref_min(n), suf_max(n);
pref_min[0] = 0;
for (int i = 1; i < n; ++i) {
pref_min[i] = pref_min[i - 1];
if (a[i] < a[pref_min[i - 1]]) {
pref_min[i] = i;
}
}
suf_max[n - 1] = n - 1;
for (int i = n - 2; i >= 0; --i) {
suf_max[i] = suf_max[i + 1];
if (a[i] > a[suf_max[i + 1]]) {
suf_max[i] = i;
}
}
int cur = merge(a, b);
int l = cur, r = n;
while (r - l > 1) {
int m = l + (r - l) / 2;
swap(a[pref_min[m - 1]], a[suf_max[m]]);
if (merge(a, b) >= m) {
l = m;
} else {
r = m;
}
swap(a[pref_min[m - 1]], a[suf_max[m]]);
}
cout << l << en;
}
int32_t main() {
int tests = 1;
#ifdef LOCAL
freopen("input.txt", "r", stdin);
tests = 1;
#else
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
cin >> tests;
for (int testcase = 1; testcase <= tests; ++testcase) {
solve();
}
return 0;
}
Tutorial
Tutorial is loading...
Code
//#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
//#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const int INFi = 2e9;
const ll INF = 2e18;
void solve() {
int n, m, x, y; cin >> n >> m >> x >> y;
x--;
y--;
vvi g(n);
rep(_, n - 1) {
int u, v; cin >> u >> v;
u--;
v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<vi> block;
vi path;
auto dfs = [&] (auto &&self, int v, int p, int t) -> bool {
path.push_back(v);
if (v == t) return true;
for(auto &u : g[v]) {
if (u == p) continue;
if (self(self, u, v, t)) return true;
}
path.pop_back();
return false;
};
rep(i, m) {
int a, b; cin >> a >> b;
a--;
b--;
dfs(dfs, a, -1, b);
assert(!path.empty());
if (block.size() < path.size()) block.resize(path.size());
rep(j, path.size()) block[j].push_back(path[j]);
path.clear();
}
vector<bool> ok(n, false);
vi q;
q.push_back(x);
vector<bool> cur(n, false);
vi was(n, -1);
for(int t = 0;;++t) {
if (t < block.size()) for(auto &u : block[t]) cur[u] = true;
vi nxt;
for(auto &v : q) {
if (ok[v] || cur[v] || was[v] == t) continue;
was[v] = t;
bool nei = 0;
for(auto &u : g[v]) nei |= ok[u];
if (t && !nei) continue;
nxt.push_back(v);
}
q.clear();
if (t < block.size()) {
for(auto &u : block[t]) {
cur[u] = false;
if (ok[u]) {
ok[u] = false;
}
q.push_back(u);
}
}
for(auto &v : nxt) {
assert(!ok[v]);
ok[v] = true;
for(auto &u : g[v]) {
if (!ok[u]) {
q.push_back(u);
}
}
}
if (ok[y]) {
cout << t + 1 << '\n';
return;
}
if (t > (int)block.size() && q.empty()) {
cout << "-1\n";
return;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
cin >> t;
rep(_, t) {
solve();
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include "bits/stdc++.h"
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
vector<int> want(n);
auto make = [&] (int i, int x, int y) {
if (!want[i]) {
if (a[i] != x) {
swap(a[i], b[i]);
}
want[i] = 1;
}
};
const int N = 2 * n + 22;
vector<vector<pair<int, int>>> g(N);
for (int i = 0; i < n; i++) {
g[a[i]].push_back({b[i], i});
g[b[i]].push_back({a[i], i});
}
vector<int> used(N);
auto dfs = [&] (auto&& dfs, int v, int h) -> void {
used[v] = h;
for (auto& [u, i] : g[v]) {
if (used[u] <= 0) {
make(i, v, u);
dfs(dfs, u, h + 1);
} else if (used[u] < used[v]) {
make(i, v, u);
}
}
};
for (int i = 0; i < N; i++) {
if (used[i] == 0 && int(g[i].size()) == 1) {
dfs(dfs, i, 1);
}
}
int rt;
auto find = [&] (auto&& find, int v, int pr) -> void {
used[v] = -1;
for (auto& [u, i] : g[v]) {
if (used[u] == 0) {
find(find, u, i);
} else if (i != pr) {
rt = u;
}
}
};
for (int i = 0; i < N; i++) {
if (used[i] == 0 && !g[i].empty()) {
rt = -1;
find(find, i, -1);
dfs(dfs, rt, 1);
}
}
cout << set<int>(a.begin(), a.end()).size() + set<int>(b.begin(), b.end()).size() << '\n';
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << '\n';
for (int i = 0; i < n; i++) {
cout << b[i] << " ";
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
solve();
}
}








