backup
This commit is contained in:
parent
653d1c1d30
commit
97477a73db
|
@ -0,0 +1,2 @@
|
|||
s = input()
|
||||
print(s[:-1] + '4')
|
|
@ -0,0 +1,5 @@
|
|||
n = int(input())
|
||||
for i in range(n + 1):
|
||||
for j in range(n - i + 1):
|
||||
for k in range(n - i - j + 1):
|
||||
print(i, j, k)
|
|
@ -0,0 +1,160 @@
|
|||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998'244'353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) (((x) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << x << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* algorithms */
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
int n = s.length(), m = t.length();
|
||||
vector<int> next; next.push_back(-1);
|
||||
int j = -1, i = 0;
|
||||
while (i < m)
|
||||
if (j == -1 || t[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (i != m && t[i] == t[j]) next.push_back(next[j]);
|
||||
else next.push_back(j);
|
||||
} else j = next[j];
|
||||
vector<int> res;
|
||||
i = 0, j = 0;
|
||||
while (i < n && j < m)
|
||||
if (j == -1 || s[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (j == m) res.push_back(i - j), j = next[j];
|
||||
} else j = next[j];
|
||||
return res;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
void solve() {
|
||||
read(string, s);
|
||||
s.push_back('B');
|
||||
int n = s.length();
|
||||
int curr = 0;
|
||||
int sum = 0;
|
||||
int mn = INT_MAX;
|
||||
int no_b = 1;
|
||||
int res = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (s[i] == 'A') {
|
||||
++curr;
|
||||
} else {
|
||||
if (s[i - 1] == 'B') {
|
||||
res += sum;
|
||||
sum = 0;
|
||||
mn = INT_MAX;
|
||||
no_b = 0;
|
||||
} else {
|
||||
sum += curr;
|
||||
mn = min(mn, curr);
|
||||
}
|
||||
curr = 0;
|
||||
}
|
||||
}
|
||||
if (sum) {
|
||||
if (no_b) {
|
||||
res += sum - mn;
|
||||
} else {
|
||||
res += sum;
|
||||
}
|
||||
}
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, t);
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,173 @@
|
|||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998'244'353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << x << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* algorithms */
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
int n = s.length(), m = t.length();
|
||||
vector<int> next; next.push_back(-1);
|
||||
int j = -1, i = 0;
|
||||
while (i < m)
|
||||
if (j == -1 || t[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (i != m && t[i] == t[j]) next.push_back(next[j]);
|
||||
else next.push_back(j);
|
||||
} else j = next[j];
|
||||
vector<int> res;
|
||||
i = 0, j = 0;
|
||||
while (i < n && j < m)
|
||||
if (j == -1 || s[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (j == m) res.push_back(i - j), j = next[j];
|
||||
} else j = next[j];
|
||||
return res;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
void solve() {
|
||||
read(int, n, a, b);
|
||||
adj(ch, n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
read(int, u, v);
|
||||
edge(ch, u, v);
|
||||
}
|
||||
vector<int> vis(n + 1);
|
||||
// bfs => distance from M to each vertex
|
||||
deque<pair<int, int>> dq;
|
||||
vector<int> dis_m(n + 1, INF);
|
||||
dq.emplace_back(a, 0);
|
||||
while (dq.size()) {
|
||||
popfront(dq, v, d);
|
||||
continue_or(vis[v], 1);
|
||||
dis_m[v] = d;
|
||||
for (auto&& u : ch[v]) {
|
||||
dq.emplace_back(u, d + 1);
|
||||
}
|
||||
}
|
||||
fill(vis.begin(), vis.end(), 0);
|
||||
// bfs => distance from V to each vertex
|
||||
vector<int> dis_v(n + 1, INF);
|
||||
dq.emplace_back(b, 0);
|
||||
while (dq.size()) {
|
||||
popfront(dq, v, d);
|
||||
continue_or(vis[v], 1);
|
||||
dis_v[v] = d;
|
||||
for (auto&& u : ch[v]) {
|
||||
dq.emplace_back(u, d + 1);
|
||||
}
|
||||
}
|
||||
// dfs => find qualified cycle
|
||||
fill(vis.begin(), vis.end(), 0);
|
||||
function<bool(int, int)> dfs = [&] (int v, int pa) -> bool {
|
||||
if_or(vis[v], 1) {
|
||||
if (dis_m[v] > dis_v[v]) {
|
||||
}
|
||||
return dis_m[v] > dis_v[v];
|
||||
}
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u != pa && dfs(u, v)) return 1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
if (dfs(b, 0)) {
|
||||
cout << "YES\n";
|
||||
} else {
|
||||
cout << "NO\n";
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, t);
|
||||
while(t--) solve();
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998244353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << x << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
int n = s.length(), m = t.length();
|
||||
vector<int> next; next.push_back(-1);
|
||||
int j = -1, i = 0;
|
||||
while (i < m)
|
||||
if (j == -1 || t[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (i != m && t[i] == t[j]) next.push_back(next[j]);
|
||||
else next.push_back(j);
|
||||
} else j = next[j];
|
||||
vector<int> res;
|
||||
i = 0, j = 0;
|
||||
while (i < n && j < m)
|
||||
if (j == -1 || s[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (j == m) res.push_back(i - j), j = next[j];
|
||||
} else j = next[j];
|
||||
return res;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
read(int, n);
|
||||
readvec(int, a, n);
|
||||
counter(a, ct, greater<>);
|
||||
unordered_map<int, int> res;
|
||||
int init = ct.begin()->first + 1;
|
||||
int prev = init;
|
||||
for (auto&& [i, _] : ct) {
|
||||
if (i < prev - 1) {
|
||||
init = i + 1;
|
||||
}
|
||||
prev = i;
|
||||
}
|
||||
for (auto&& [i, t] : ct) {
|
||||
if (i > init) continue;
|
||||
int mn = init * int(t - 1) + i;
|
||||
for (auto&& [j, prev] : res) {
|
||||
mn = min(mn, prev + j * int(t - 1) + i);
|
||||
}
|
||||
res[i] = mn;
|
||||
// debug(i), debug(res[i]);
|
||||
}
|
||||
cout << res[0] << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, t);
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998244353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << x << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
int n = s.length(), m = t.length();
|
||||
vector<int> next; next.push_back(-1);
|
||||
int j = -1, i = 0;
|
||||
while (i < m)
|
||||
if (j == -1 || t[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (i != m && t[i] == t[j]) next.push_back(next[j]);
|
||||
else next.push_back(j);
|
||||
} else j = next[j];
|
||||
vector<int> res;
|
||||
i = 0, j = 0;
|
||||
while (i < n && j < m)
|
||||
if (j == -1 || s[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (j == m) res.push_back(i - j), j = next[j];
|
||||
} else j = next[j];
|
||||
return res;
|
||||
}
|
||||
|
||||
void solve() {
|
||||
read(int, n, k);
|
||||
read(string, s);
|
||||
readvec(int, l, k);
|
||||
readvec(int, r, k);
|
||||
read(int, q);
|
||||
readvec(int, x, q);
|
||||
for (int i = 0; i < k; ++i) {
|
||||
--l[i], --r[i];
|
||||
}
|
||||
for (int i = 0; i < q; ++i) {
|
||||
--x[i];
|
||||
}
|
||||
vector<int> pos(n);
|
||||
{
|
||||
int curr = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (i > r[curr]) ++curr;
|
||||
pos[i] = curr;
|
||||
}
|
||||
}
|
||||
vector<vector<int>> mark(k, vector<int>(n));
|
||||
for (auto&& i : x) {
|
||||
int ipos = pos[i];
|
||||
int offset = min(i, l[ipos] + r[ipos] - i) - l[ipos];
|
||||
mark[ipos][offset] ^= 1;
|
||||
}
|
||||
for (int i = 0; i < k; ++i) {
|
||||
int left = l[i], right = r[i];
|
||||
vector<char> c(right - left + 1);
|
||||
int mid = right - left + 2 >> 1;
|
||||
int rev = 0;
|
||||
for (int j = 0; j < mid; ++j) {
|
||||
rev ^= mark[i][j];
|
||||
if (rev) {
|
||||
debug(j), debug(right - j), debug(c.size());
|
||||
c[j] = s[right - j];
|
||||
c[right - left - j] = s[left + j];
|
||||
} else {
|
||||
c[j] = s[left + j];
|
||||
c[right - left - j] = s[right - j];
|
||||
}
|
||||
}
|
||||
for (auto&& x : c) cout << x;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, t);
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
#include<bits/stdc++.h>
|
||||
#include <initializer_list>
|
||||
using namespace std;
|
||||
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __decompose_s(a, x) auto x = a;
|
||||
#define __decompose_n(a, ...) auto [__VA_ARGS__] = a;
|
||||
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998244353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
#define untie ios_base::sync_with_stdio(0), cin.tie(NULL)
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) type __VA_ARGS__; __read(__VA_ARGS__);
|
||||
#define readvec(type, a, n) vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];
|
||||
#define putvec(a) for (auto&& x : a) cout << x << ' '; cout << endl;
|
||||
#define debug(x) cerr << #x" = " << x << endl;
|
||||
#define debugvec(a) cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;
|
||||
|
||||
#define pa(a) __typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);
|
||||
#define sa(a) __typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};
|
||||
|
||||
#define poptop(q, ...) auto [__VA_ARGS__] = q.top(); q.pop();
|
||||
#define popback(q, ...) auto [__VA_ARGS__] = q.back(); q.pop_back();
|
||||
#define popfront(q, ...) auto [__VA_ARGS__] = q.front();q.pop_front();
|
||||
|
||||
#define adj(ch, n) vector<vector<int>> ch((n) + 1);
|
||||
#define edge(ch, u, v) ch[u].push_back(v), ch[v].push_back(u);
|
||||
#define Edge(ch, u, v) ch[u].push_back(v);
|
||||
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
int n = s.length(), m = t.length();
|
||||
vector<int> next; next.push_back(-1);
|
||||
int j = -1, i = 0;
|
||||
while (i < m)
|
||||
if (j == -1 || t[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (i != m && t[i] == t[j]) next.push_back(next[j]);
|
||||
else next.push_back(j);
|
||||
} else j = next[j];
|
||||
vector<int> res;
|
||||
i = 0, j = 0;
|
||||
while (i < n && j < m)
|
||||
if (j == -1 || s[i] == t[j]) {
|
||||
++i, ++j;
|
||||
if (j == m) res.push_back(i - j), j = next[j];
|
||||
} else j = next[j];
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, n);
|
||||
readvec(ull, a, n);
|
||||
ull s1, s2, s3, s4, pa;
|
||||
|
||||
s1 = s2 = 0, pa = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
pa ^= a[i];
|
||||
s1 = (s1 + (i + 1) * pa) % PRIME;
|
||||
s2 = (s2 + pa) % PRIME;
|
||||
}
|
||||
s3 = s4 = 0, pa = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
s3 = (s3 + i * pa) % PRIME;
|
||||
s4 = (s4 + pa) % PRIME;
|
||||
pa ^= a[i];
|
||||
}
|
||||
ull res = n * s1 - n * (n - 1) / 2 * s2 + n * s3 - n * (n + 1) / 2 * s4;
|
||||
cout << (res % PRIME);
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __decompose_s(a, x) auto x = a;
|
||||
#define __decompose_n(a, ...) auto [__VA_ARGS__] = a;
|
||||
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
#define untie ios_base::sync_with_stdio(0), cin.tie(NULL)
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) type __VA_ARGS__; __read(__VA_ARGS__);
|
||||
#define readvec(type, a, n) vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];
|
||||
#define putvec(a) for (auto&& x : a) cout << x << ' '; cout << endl;
|
||||
#define debug(x) cerr << #x" = " << x << endl;
|
||||
#define debugvec(a) cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;
|
||||
|
||||
#define pa(a) __typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);
|
||||
#define sa(a) __typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};
|
||||
|
||||
#define poptop(q, ...) auto [__VA_ARGS__] = q.top(); q.pop();
|
||||
#define popback(q, ...) auto [__VA_ARGS__] = q.back(); q.pop_back();
|
||||
#define popfront(q, ...) auto [__VA_ARGS__] = q.front();q.pop_front();
|
||||
|
||||
#define adj(ch, n) vector<vector<int>> ch((n) + 1);
|
||||
#define edge(ch, u, v) ch[u].push_back(v), ch[v].push_back(u);
|
||||
#define Edge(ch, u, v) ch[u].push_back(v);
|
||||
|
||||
|
||||
void solve() {
|
||||
read(int, n, k);
|
||||
vector<int> marks(n + 1);
|
||||
for (int i = 0; i < k; ++i) {
|
||||
read(int, v);
|
||||
marks[v] = 1;
|
||||
}
|
||||
adj(ch, n);
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
read(int, u, v);
|
||||
edge(ch, u, v);
|
||||
}
|
||||
function<tuple<int, int, int>(int, int)> dfs = [&] (int v, int pa) {
|
||||
int res = 0;
|
||||
int max_w = marks[v] == 0 ? 0 : -1;
|
||||
int max_r = marks[v] == 1 ? 0 : -1;
|
||||
for (auto&& u : ch[v]) {
|
||||
if (u == pa) continue;
|
||||
auto [sub_ans, w, r] = dfs(u, v);
|
||||
if (w != -1 && max_r != -1) res = min(res, w + 1 + max_r);
|
||||
if (r != -1 && max_w != -1) res = min(res, r + 1 + max_w);
|
||||
if (w != -1) max_w = max(max_w, w + 1);
|
||||
if (r != -1) max_r = max(max_r, r + 1);
|
||||
res = min(res, sub_ans);
|
||||
}
|
||||
return make_tuple(res, max_w, max_r);
|
||||
};
|
||||
cout << get<0>(dfs(1, 0)) << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
read(int, t);
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = int64_t;
|
||||
#define msd_power(x) (63LL - __builtin_clz(x))
|
||||
void solve() {
|
||||
int n; cin >> n;
|
||||
vector<ll> a(n);
|
||||
for (int i = 0; i < n; ++i) cin >> a[i];
|
||||
ll res = 0;
|
||||
ll last_power = 0;
|
||||
for (int i = 1; i < n; ++i) {
|
||||
if (msd_power(a[i]) >= msd_power(a[i-1]) + last_power) {
|
||||
ll prev = a[i-1] << last_power;
|
||||
if (prev > a[i]) {
|
||||
last_power = 1;
|
||||
} else {
|
||||
last_power = 0;
|
||||
}
|
||||
} else {
|
||||
ll diff = 0;
|
||||
if (msd_power(a[i]) <= msd_power(a[i-1])) {
|
||||
ll curr = a[i] << (msd_power(a[i-1]) - msd_power(a[i]));
|
||||
if (curr < a[i-1]) diff = 1;
|
||||
} else {
|
||||
ll prev = a[i-1] << (msd_power(a[i]) - msd_power(a[i-1]));
|
||||
if (prev > a[i]) diff = 1;
|
||||
}
|
||||
last_power = msd_power(a[i-1]) + last_power - msd_power(a[i]) + diff;
|
||||
}
|
||||
// cerr << last_power << ' ';
|
||||
res += last_power;
|
||||
}
|
||||
// cerr << endl;
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
void solve() {
|
||||
int n, m; cin >> n >> m;
|
||||
vector<pair<int, int>> seg(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int l, r; cin >> l >> r;
|
||||
seg[i] = {l, r};
|
||||
}
|
||||
sort(seg.begin(), seg.end(), [](const pair<int, int>& x, const pair<int, int>& y) {
|
||||
if (x.first == y.first) {
|
||||
return x.second > y.second;
|
||||
}
|
||||
return x.first < y.first;
|
||||
});
|
||||
int res = 0;
|
||||
set<pair<int, int>> lefts;
|
||||
set<pair<int, int>> rights;
|
||||
int prev = 0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
auto&& [l, r] = seg[i];
|
||||
if (l == 1 || l == prev) continue;
|
||||
prev = l;
|
||||
auto rp = rights.begin();
|
||||
while (rights.size() && rp->first < l) {
|
||||
lefts.erase({seg[rp->second].first, rp->second});
|
||||
rp = rights.erase(rp);
|
||||
}
|
||||
lefts.insert({l, i});
|
||||
rights.insert({r, i});
|
||||
res = max(res, int(lefts.size()));
|
||||
cerr << lefts.size() << ' ' << res << endl;
|
||||
}
|
||||
lefts.clear(), rights.clear(), prev = 0;
|
||||
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = int64_t;
|
||||
|
||||
void solve() {
|
||||
int n, c; cin >> n >> c;
|
||||
vector<ll> a(n);
|
||||
for (int i = 0; i < n; ++i) cin >> a[i];
|
||||
vector<int> idx(n - 1);
|
||||
iota(idx.begin(), idx.end(), 1);
|
||||
sort(idx.begin(), idx.end(), [&](int i, int j) {
|
||||
if (a[i] == a[j]) return i < j;
|
||||
return a[i] > a[j];
|
||||
});
|
||||
ll sum = a[0];
|
||||
for (auto&& i : idx) {
|
||||
sum += a[i];
|
||||
if (sum < (i + 1) * c) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
cout << "Yes\n";
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while (t--) solve();
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let mut pa = vec![0;n+1];
|
||||
let mut sa = vec![0;n+1];
|
||||
pa[0] = 0 - n as i64 - 1;
|
||||
for i in 1..=n {
|
||||
pa[i] = pa[i-1].max(a[i-1] - i as i64 - 1);
|
||||
}
|
||||
for i in (0..n).rev() {
|
||||
sa[i] = sa[i+1].max(a[i] + i as i64);
|
||||
}
|
||||
let mut res = i64::MAX;
|
||||
for i in 0..n {
|
||||
res = res.min(a[i].max((pa[i] + n as i64 + 1).max(sa[i+1])));
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let mut s: i64 = a.iter().sum();
|
||||
let mut curr_seg = 0;
|
||||
let mut curr_sum = 0;
|
||||
let mut res = 0;
|
||||
for x in a {
|
||||
curr_sum += x;
|
||||
s -= x;
|
||||
if s >= 0 {
|
||||
curr_seg += 1;
|
||||
res += curr_seg * curr_sum;
|
||||
curr_sum = 0;
|
||||
}
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nq: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, q) = (nq[0], nq[1]);
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
for _ in 0..q {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut k: i64 = buf.trim().parse::<i64>().unwrap();
|
||||
let mut a = a.to_owned();
|
||||
let mut pow = 59;
|
||||
let mut res = 0;
|
||||
while pow >= 0 {
|
||||
let target = 1 << pow;
|
||||
let mut need = 0;
|
||||
let mut f = true;
|
||||
for i in 0..n {
|
||||
let curr = 0.max(target - a[i]);
|
||||
if curr > k - need {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
need += 0.max(target - a[i]);
|
||||
}
|
||||
if f {
|
||||
for i in 0..n {
|
||||
a[i] = a[i].max(target) - target;
|
||||
}
|
||||
k -= need;
|
||||
res += target;
|
||||
} else {
|
||||
for i in 0..n {
|
||||
if a[i] >= target {
|
||||
a[i] -= target;
|
||||
}
|
||||
}
|
||||
}
|
||||
pow -= 1;
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
void solve() {
|
||||
int n, m; cin >> n >> m;
|
||||
cout << max(n, m) << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while(t--) solve();
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
constexpr int MAXN = 1e5 + 10;
|
||||
|
||||
vector<int> ch[MAXN];
|
||||
|
||||
void solve() {
|
||||
int n; cin >> n;
|
||||
fill(ch, ch + n + 1, vector<int>());
|
||||
for (int i = 0; i < n - 1; ++i) {
|
||||
int u, v; cin >> u >> v;
|
||||
ch[u].push_back(v);
|
||||
ch[v].push_back(u);
|
||||
}
|
||||
int cnt = 0;
|
||||
function<void(int, int)> dfs = [&](int v, int pa) {
|
||||
int f = 1;
|
||||
for (auto&&u : ch[v]) {
|
||||
if (u != pa) {
|
||||
f = 0;
|
||||
dfs(u, v);
|
||||
}
|
||||
}
|
||||
if (f) ++cnt;
|
||||
};
|
||||
if (n == 2) {
|
||||
cout << 1 << endl;
|
||||
} else {
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
if (ch[i].size() > 1) {
|
||||
dfs(i, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
cout << (cnt + 1 >> 1) << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while(t--) solve();
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ull = uint64_t;
|
||||
|
||||
constexpr int MAXN = 1e5 + 10;
|
||||
constexpr ull mdl = 1e9 + 7;
|
||||
|
||||
ull table[MAXN + 1];
|
||||
|
||||
void prep() {
|
||||
ull l1 = 1, l2 = 0;
|
||||
for (int i = 0; i < MAXN + 1; ++i) {
|
||||
table[i] = (l1 + l2) % mdl;
|
||||
l1 = l2;
|
||||
l2 = table[i];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
prep();
|
||||
string s; cin >> s;
|
||||
s.push_back(0);
|
||||
ull res = 1;
|
||||
int acc = 0;
|
||||
char prev = 0;
|
||||
for (auto&& c : s) {
|
||||
if (c == 'w' || c == 'm') {
|
||||
goto zero;
|
||||
}
|
||||
if (c != prev) {
|
||||
if (prev == 'u' || prev == 'n') {
|
||||
res = (res * (table[acc] % mdl)) % mdl;
|
||||
}
|
||||
acc = 1;
|
||||
prev = c;
|
||||
} else {
|
||||
++acc;
|
||||
}
|
||||
}
|
||||
cout << res;
|
||||
return 0;
|
||||
zero: cout << '0';
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ull = uint64_t;
|
||||
int main() {
|
||||
ull l1 = 1, l2 = 1;
|
||||
cout << "1, 1";
|
||||
for (int i = 2; i <= 1e5 + 1; ++i) {
|
||||
ull t = l1 + l2;
|
||||
cout << ", " << t;
|
||||
l1 = l2;
|
||||
l2 = t;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut n: i64 = buf.trim().parse::<i64>().unwrap();
|
||||
let mut res = 1;
|
||||
while n != 0 {
|
||||
let digit = n % 10;
|
||||
n /= 10;
|
||||
res *= (digit + 2) * (digit + 1) >> 1;
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
fn judge(x: Vec<Vec<i32>>) -> bool {
|
||||
let a = x.iter().fold(true, |b, p|{
|
||||
b && p[0] <= 0
|
||||
});
|
||||
let b = x.iter().fold(true, |b, p|{
|
||||
b && p[1] <= 0
|
||||
});
|
||||
let c = x.iter().fold(true, |b, p|{
|
||||
b && p[0] >= 0
|
||||
});
|
||||
let d = x.iter().fold(true, |b, p|{
|
||||
b && p[1] >= 0
|
||||
});
|
||||
a || b || c || d
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
let mut co = Vec::with_capacity(n);
|
||||
for _ in 0..n {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tmp: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
co.push(tmp);
|
||||
}
|
||||
if judge(co) {
|
||||
println!("YES");
|
||||
} else {
|
||||
println!("NO");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut a: Vec<u64> = buf.trim().split(' ').map(|x|x.parse::<u64>().unwrap()).collect::<Vec<u64>>();
|
||||
let mut mdl = 2u64;
|
||||
loop {
|
||||
let mut one = false;
|
||||
let mut zero = false;
|
||||
for x in &mut a {
|
||||
if *x & 1 == 0 {
|
||||
zero = true;
|
||||
} else {
|
||||
one = true;
|
||||
}
|
||||
*x >>= 1;
|
||||
}
|
||||
if zero && one {
|
||||
println!("{mdl}");
|
||||
break;
|
||||
}
|
||||
mdl <<= 1;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
use std::collections::BTreeSet;
|
||||
use std::ops::Bound::*;
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut l: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let r: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut rst = BTreeSet::new();
|
||||
r.into_iter().for_each(|x|{ rst.insert(x); });
|
||||
let mut c: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
l.sort_unstable();
|
||||
let mut ranges = vec![];
|
||||
for &x in l.iter().rev() {
|
||||
let cr = *rst.range((Excluded(x), Unbounded)).next().unwrap();
|
||||
rst.remove(&cr);
|
||||
ranges.push(cr - x);
|
||||
}
|
||||
ranges.sort_unstable_by_key(|x|-x);
|
||||
c.sort_unstable();
|
||||
let res = ranges.into_iter().zip(c.into_iter()).fold(0i64, |s, p|s + p.0 * p.1);
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let m: usize = buf.trim().parse::<usize>().unwrap();
|
||||
let mut cnt = vec![0;30];
|
||||
for _ in 0..m {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let query: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
let (t, mut v) = (query[0], query[1]);
|
||||
if t == 1 {
|
||||
cnt[query[1] as usize] += 1;
|
||||
} else {
|
||||
let mut f = true;
|
||||
let mut ccnt = 0;
|
||||
let mut i = 0;
|
||||
while v != 0 {
|
||||
ccnt = (ccnt >> 1) + cnt[i];
|
||||
let curr_bit = v & 1;
|
||||
v >>= 1;
|
||||
if curr_bit == 1 {
|
||||
if ccnt == 0 {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
ccnt -= 1;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
if f {
|
||||
println!("YES");
|
||||
} else {
|
||||
println!("NO");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: String = buf.trim().parse::<String>().unwrap();
|
||||
let a = t.into_bytes();
|
||||
let mut mp = HashMap::<u8, usize>::new();
|
||||
let mut cnt = 0;
|
||||
for x in a {
|
||||
*mp.entry(x).or_default() += 1;
|
||||
if matches!(mp.get(&x), Some(&t) if t + 64 == x as usize) {
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
println!("{cnt}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, k) = (t[0], t[1]);
|
||||
for i in 1..=k {
|
||||
print!("{i} ");
|
||||
}
|
||||
for i in (k+1..=n).rev() {
|
||||
print!("{i} ");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nk: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, k) = (nk[0], nk[1]);
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let b: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let mut res = 0;
|
||||
let mut a_sum = 0;
|
||||
let mut b_max = 0;
|
||||
for i in 0..n.min(k) {
|
||||
a_sum += a[i];
|
||||
b_max = b_max.max(b[i]);
|
||||
res = res.max(a_sum + b_max * (k - i - 1) as i64);
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let b: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let c: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let mut dp = vec![vec![i64::MIN;8];n+1];
|
||||
for i in 1..=n {
|
||||
dp[i][0b001] = dp[i-1][0b001].max(c[i-1]);
|
||||
dp[i][0b010] = dp[i-1][0b010].max(b[i-1]);
|
||||
dp[i][0b100] = dp[i-1][0b100].max(a[i-1]);
|
||||
dp[i][0b011] = dp[i-1][0b011].max(dp[i-1][0b001] + b[i-1]).max(dp[i-1][0b010] + c[i-1]);
|
||||
dp[i][0b101] = dp[i-1][0b101].max(dp[i-1][0b001] + a[i-1]).max(dp[i-1][0b100] + c[i-1]);
|
||||
dp[i][0b110] = dp[i-1][0b110].max(dp[i-1][0b010] + a[i-1]).max(dp[i-1][0b100] + b[i-1]);
|
||||
dp[i][0b111] = dp[i-1][0b111].max(dp[i-1][0b110] + c[i-1]).max(dp[i-1][0b101] + b[i-1]).max(dp[i-1][0b011] + a[i-1]);
|
||||
}
|
||||
println!("{}", dp[n][0b111]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
use std::collections::HashSet;
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let mut x = vec![0;n+1];
|
||||
let mut y = vec![0;n+1];
|
||||
for i in 1..=n {
|
||||
x[i] = x[i-1];
|
||||
y[i] = y[i-1];
|
||||
if i & 1 == 0 {
|
||||
x[i] += a[i-1];
|
||||
} else {
|
||||
y[i] += a[i-1];
|
||||
}
|
||||
}
|
||||
let mut oc = HashSet::new();
|
||||
let mut f = true;
|
||||
for i in 0..=n {
|
||||
if oc.contains(&(x[i] - y[i])) {
|
||||
println!("YES");
|
||||
f = false;
|
||||
break;
|
||||
} else {
|
||||
oc.insert(x[i] - y[i]);
|
||||
}
|
||||
}
|
||||
if f {
|
||||
println!("NO");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
use std::collections::{BTreeSet, HashMap};
|
||||
|
||||
struct BIT(usize, Vec<i64>);
|
||||
|
||||
impl BIT {
|
||||
fn new(n: usize) -> Self {
|
||||
Self(n, vec![0;n+1])
|
||||
}
|
||||
fn query(&self, mut i: usize) -> i64 {
|
||||
let lowbit = |x: i64|x&-x;
|
||||
let mut res = 0;
|
||||
while i > 0 {
|
||||
res += self.1[i];
|
||||
i -= lowbit(i as i64) as usize;
|
||||
}
|
||||
res
|
||||
}
|
||||
fn insert(&mut self, mut i: usize) {
|
||||
let lowbit = |x: i64| x & -x;
|
||||
while i <= self.0 {
|
||||
self.1[i] += 1;
|
||||
i += lowbit(i as i64) as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n : usize = buf.trim().parse::<usize>().unwrap();
|
||||
let mut a = vec![];
|
||||
let mut numbers = BTreeSet::new();
|
||||
for _ in 0..n {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tmp: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
a.push((tmp[0], tmp[1]));
|
||||
numbers.insert(tmp[0]);
|
||||
numbers.insert(tmp[1]);
|
||||
}
|
||||
let mut mp = HashMap::new();
|
||||
let mut tot = 0;
|
||||
for p in numbers {
|
||||
tot += 1;
|
||||
mp.insert(p, tot);
|
||||
}
|
||||
a.sort_unstable();
|
||||
let mut st = BIT::new(tot);
|
||||
let mut res = 0;
|
||||
for x in a.iter().rev() {
|
||||
let td = *mp.get(&x.1).unwrap();
|
||||
let cnt = st.query(td);
|
||||
res += cnt;
|
||||
st.insert(td);
|
||||
}
|
||||
println!("{res}");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect();
|
||||
let mut odd = 0;
|
||||
let mut even = 0;
|
||||
let mut sum = 0;
|
||||
for x in a {
|
||||
sum += x;
|
||||
if x & 1 == 1 {
|
||||
odd += 1;
|
||||
} else {
|
||||
even += 1;
|
||||
}
|
||||
if odd + even == 1 {
|
||||
print!("{x} ");
|
||||
} else if odd + even == 2 {
|
||||
if odd == 1 && even == 1 {
|
||||
print!("{} ", sum - 1);
|
||||
} else {
|
||||
print!("{} ", sum);
|
||||
}
|
||||
} else {
|
||||
let mut res = sum;
|
||||
res -= odd / 3;
|
||||
if odd % 3 == 1 {
|
||||
print!("{} ", res - 1);
|
||||
} else {
|
||||
print!("{} ", res);
|
||||
}
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/**
|
||||
* 如果有负数,那么就不需要变,因为现在已经是最小的了
|
||||
* 如果没负数,至多需要把随便一个数变成0即可。
|
||||
*/
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
let mut f = true;
|
||||
let mut sgn = 1;
|
||||
for x in a {
|
||||
sgn *= x.signum();
|
||||
if x == 0 {
|
||||
println!("0");
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if f {
|
||||
if sgn == 1 {
|
||||
println!("1\n1 0");
|
||||
} else {
|
||||
println!("0");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
/**
|
||||
* dp[i] 表示进行i次删除首字母操作后得到的结果
|
||||
*/
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a = buf.trim().parse::<String>().unwrap().into_bytes();
|
||||
let mut res = 0;
|
||||
let mut mp = HashMap::new();
|
||||
let mut dp = vec![0;n+1];
|
||||
for i in (0..n).rev() {
|
||||
dp[i] = mp.get(&a[i]).unwrap_or(&n) - i;
|
||||
res += dp[i];
|
||||
mp.insert(a[i], i);
|
||||
}
|
||||
println!("{}", res);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
fn solve(a: Vec<i64>, v: Vec<usize>, start_date: usize, limit: i64) -> (usize, i64) {
|
||||
let n = a.len();
|
||||
let k = v.len();
|
||||
let mut delta_delta = vec![0i64;n+1];
|
||||
for i in 0..k {
|
||||
delta_delta[0] += 1;
|
||||
delta_delta[v[i]] -= 1;
|
||||
}
|
||||
let mut delta = vec![0;n];
|
||||
let mut t = 0;
|
||||
for i in 0..n {
|
||||
t += delta_delta[i];
|
||||
delta[i] = t;
|
||||
}
|
||||
let mut idx = n + 1;
|
||||
let mut raw = 0;
|
||||
for i in 0..n {
|
||||
if a[i] <= i as i64 + 1 && idx == n + 1 {
|
||||
idx = i;
|
||||
}
|
||||
if a[i] == i as i64 + 1 {
|
||||
raw += 1;
|
||||
}
|
||||
}
|
||||
if idx == n + 1 || delta[idx] == 0 {
|
||||
return (1, raw);
|
||||
}
|
||||
let full = (idx as i64 + 1 - a[idx]) / delta[idx];
|
||||
let mut res = full * k as i64;
|
||||
let mut rs = idx as i64 + 1 - (a[idx] + full * delta[idx]);
|
||||
eprintln!("idx={idx}, full={full}, res={res}, rs={rs}");
|
||||
let mut cf = vec![0;n+1];
|
||||
for t in 0..k {
|
||||
if rs == 0 {
|
||||
if res + 1 > limit {
|
||||
return if raw != 0 {
|
||||
(1, raw)
|
||||
} else {
|
||||
(usize::MAX, 0)
|
||||
};
|
||||
}
|
||||
let mut cnt = 0;
|
||||
let mut acc = 0;
|
||||
for i in 0..n {
|
||||
acc += cf[i];
|
||||
if a[i] + delta[i] * full + acc == i as i64 + 1 {
|
||||
cnt += 1;
|
||||
}
|
||||
}
|
||||
return (res as usize + 1, cnt);
|
||||
}
|
||||
res += 1;
|
||||
cf[0] += 1;
|
||||
cf[v[(start_date+t)%k]] -= 1;
|
||||
if v[(start_date+t)%k] > idx {
|
||||
rs -= 1;
|
||||
}
|
||||
}
|
||||
panic!()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tmp: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let d = tmp[2];
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let v: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (next_date, init_score) = solve(a, v, 0, d as i64);
|
||||
eprintln!("{next_date} {init_score}");
|
||||
if init_score == 0 || next_date > d {
|
||||
println!("{}", (d - 1) / 2);
|
||||
} else {
|
||||
println!("{}", init_score + (d - next_date) as i64 / 2);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
from sortedcontainers import SortedList
|
||||
|
||||
def solve():
|
||||
n, k = [int(x) for x in input().strip().split(' ')]
|
||||
p = [int(x) for x in input().strip().split(' ')]
|
||||
q = [int(x) for x in input().strip().split(' ')]
|
||||
assert n == len(p) and k == len(q)
|
||||
st = SortedList()
|
||||
res = 0
|
||||
for i in range(n - 1, -1, -1):
|
||||
res += st.bisect_left(p[i])
|
||||
res +=
|
|
@ -0,0 +1,33 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
let f = t == 10000;
|
||||
for k in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
let mut others = vec![];
|
||||
let mut last = i32::MAX;
|
||||
for &x in &a {
|
||||
if x > last {
|
||||
others.push(x);
|
||||
} else {
|
||||
last = x;
|
||||
}
|
||||
}
|
||||
let mut res = 0;
|
||||
if !others.is_empty() {
|
||||
for i in 0..others.len()-1 {
|
||||
res += if others[i] < others[i+1] {1} else {0};
|
||||
}
|
||||
}
|
||||
if !f {
|
||||
println!("{res}");
|
||||
} else if k == 470 {
|
||||
println!("{:?}", &a);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
struct DancingLinks<T: Copy + Eq> {
|
||||
cnt: usize,
|
||||
left: Vec<usize>,
|
||||
right: Vec<usize>,
|
||||
up: Vec<usize>,
|
||||
down: Vec<usize>,
|
||||
row_head: Vec<usize>,
|
||||
col_head: Vec<usize>,
|
||||
val: Vec<T>,
|
||||
row_info: Vec<usize>,
|
||||
col_info: Vec<usize>,
|
||||
}
|
||||
|
||||
impl<T: Copy + Eq> DancingLinks<T> {
|
||||
fn new(max_n: usize) -> Self {
|
||||
let mut ins = Self {
|
||||
cnt: 0,
|
||||
left: vec![],
|
||||
right: vec![],
|
||||
up: vec![],
|
||||
down: vec![],
|
||||
row_head: vec![0;max_n],
|
||||
col_head: vec![0;max_n],
|
||||
val: vec![],
|
||||
row_info: vec![],
|
||||
col_info: vec![],
|
||||
};
|
||||
ins.left.push(0);
|
||||
ins.right.push(0);
|
||||
ins.up.push(0);
|
||||
ins.down.push(0);
|
||||
ins.row_info.push(0);
|
||||
ins.col_info.push(0);
|
||||
ins
|
||||
}
|
||||
fn get_item(&self, idx: usize) -> T {
|
||||
self.val[idx-1]
|
||||
}
|
||||
fn get(&self, i: usize, j: usize) -> Option<T> {
|
||||
let mut curr = self.row_head[i];
|
||||
while curr != 0 {
|
||||
if self.col_info[curr] == j {
|
||||
return Some(self.get_item(curr));
|
||||
}
|
||||
curr = self.right[curr];
|
||||
}
|
||||
curr = self.row_head[i];
|
||||
while curr != 0 {
|
||||
if self.col_info[curr] == j {
|
||||
return Some(self.get_item(curr));
|
||||
}
|
||||
curr = self.left[curr];
|
||||
}
|
||||
None
|
||||
}
|
||||
fn insert(&mut self, i: usize, j: usize, val: T) {
|
||||
self.cnt += 1;
|
||||
self.val.push(val);
|
||||
self.left.push(0);
|
||||
self.right.push(self.row_head[i]);
|
||||
self.left[self.row_head[i]] = self.cnt;
|
||||
self.row_head[i] = self.cnt;
|
||||
self.up.push(0);
|
||||
self.down.push(self.col_head[j]);
|
||||
self.up[self.col_head[j]] = self.cnt;
|
||||
self.col_head[j] = self.cnt;
|
||||
self.row_info.push(i);
|
||||
self.col_info.push(j);
|
||||
}
|
||||
fn remove_row(&mut self, i: usize) {
|
||||
let mut curr = self.row_head[i];
|
||||
while curr != 0 {
|
||||
self.down[self.up[curr]] = self.down[curr];
|
||||
self.up[self.down[curr]] = self.up[curr];
|
||||
curr = self.right[curr];
|
||||
}
|
||||
self.row_head[i] = 0;
|
||||
}
|
||||
fn remove_col(&mut self, j: usize) {
|
||||
let mut curr = self.col_head[j];
|
||||
while curr != 0 {
|
||||
self.right[self.left[curr]] = self.right[curr];
|
||||
self.left[self.right[curr]] = self.left[curr];
|
||||
curr = self.right[curr];
|
||||
}
|
||||
self.col_head[j] = 0;
|
||||
}
|
||||
fn dance_remove(&mut self, i: usize, plain: T) {
|
||||
let mut curr = self.row_head[i];
|
||||
while curr != 0 {
|
||||
if self.val[curr] != plain {
|
||||
let mut curr = self.col_head[self.col_info[curr]];
|
||||
while curr != 0 {
|
||||
if self.row_info[curr] != i && self.val[curr] != plain {
|
||||
self.remove_row(self.row_info[curr]);
|
||||
}
|
||||
curr = self.down[curr];
|
||||
}
|
||||
}
|
||||
self.remove_col(self.col_info[curr]);
|
||||
curr = self.right[curr];
|
||||
}
|
||||
self.remove_row(i);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut mat = DancingLinks::new(100);
|
||||
mat.insert(0,0,10086);
|
||||
mat.insert(0,1,234);
|
||||
mat.insert(1,1,10010);
|
||||
mat.insert(12,25,12345);
|
||||
assert_eq!(mat.get(0,0), Some(10086));
|
||||
assert_eq!(mat.get(1,0), None);
|
||||
assert_eq!(mat.get(12, 25), Some(12345));
|
||||
mat.dance_remove(0,0);
|
||||
assert_eq!(mat.get(0,0), None);
|
||||
assert_eq!(mat.get(1,0), None);
|
||||
assert_eq!(mat.get(1,1), None);
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
struct Solution;
|
||||
|
||||
struct Sol;
|
||||
|
||||
impl Sol {
|
||||
fn solve(mut x: i64, limit: i64, mut s: i64) -> i64 {
|
||||
let mut pow = vec![1i64;17];
|
||||
for i in 1..17 {
|
||||
pow[i] = pow[i-1] * 10;
|
||||
}
|
||||
let mut cnt = vec![0;17];
|
||||
let m = s;
|
||||
let mut s_len = 0;
|
||||
while s != 0 {
|
||||
s_len += 1;
|
||||
s /= 10;
|
||||
}
|
||||
let pass = m <= x % pow[s_len];
|
||||
let mut x_len = 0;
|
||||
let mut x_digit = vec![];
|
||||
x_digit.push(0);
|
||||
while x != 0 {
|
||||
x_len += 1;
|
||||
x_digit.push(x % 10);
|
||||
x /= 10;
|
||||
}
|
||||
if s_len > x_len {
|
||||
return 0;
|
||||
}
|
||||
for i in 1..=x_len {
|
||||
if i <= s_len {
|
||||
cnt[i] = 1;
|
||||
} else {
|
||||
cnt[i] = cnt[i-1] * (limit + 1);
|
||||
}
|
||||
}
|
||||
let mut res = 0;
|
||||
let mut f = true;
|
||||
for i in (s_len+1..=x_len).rev() {
|
||||
res += cnt[i-1] * (limit.min(x_digit[i] - 1) + 1);
|
||||
if limit < x_digit[i] {
|
||||
f = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if f && pass {
|
||||
res += 1;
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn number_of_powerful_int(start: i64, finish: i64, limit: i32, s: String) -> i64 {
|
||||
let upper = Sol::solve(finish , limit as i64, s.parse().unwrap());
|
||||
let lower = Sol::solve(start - 1, limit as i64, s.parse().unwrap());
|
||||
upper - lower
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", Solution::number_of_powerful_int(1, 1000000000000000, 4, "1".to_string()));
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn max_height(mut cuboids: Vec<Vec<i32>>) -> i32 {
|
||||
for x in cuboids.iter_mut() {
|
||||
x.sort_unstable_by_key(|x|-x);
|
||||
}
|
||||
cuboids.sort_unstable_by(|x, y| {
|
||||
if x[0] == y[0] {
|
||||
if x[1] == y[1] {
|
||||
return y[2].cmp(&x[2]);
|
||||
}
|
||||
return y[1].cmp(&x[1]);
|
||||
}
|
||||
return y[0].cmp(&x[0]);
|
||||
});
|
||||
let n = cuboids.len();
|
||||
let if_fit = |x: &Vec<i32>, y: &Vec<i32>| {
|
||||
x[1] <= y[1] && x[2] <= y[2]
|
||||
};
|
||||
let mut dp = vec![0;n];
|
||||
let mut res = 0;
|
||||
for i in 0..n {
|
||||
for j in 0..i {
|
||||
if if_fit(&cuboids[i], &cuboids[j]) {
|
||||
dp[i] = dp[i].max(dp[j]);
|
||||
}
|
||||
}
|
||||
dp[i] += cuboids[i][0];
|
||||
res = res.max(dp[i]);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
// 中位数贪心,枚举结果中的中间那个1的位置。
|
||||
// 预处理一下前缀和和后缀和,这样在枚举的过程中就可以用二分查找,实现O(n\log n)复杂度。
|
||||
// nums = [1 0 0 1 0 1]
|
||||
// pa = [1 1 1 2 2 3]
|
||||
// pasum= [0 0 0 3 3 8]
|
||||
|
||||
|
||||
struct Solution;
|
||||
|
||||
#[inline]
|
||||
fn sum(start: i32, length: i32) -> i64 {
|
||||
(2 * start as i64 + length as i64 - 1) * length as i64 >> 1
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn min_moves(nums: Vec<i32>, k: i32) -> i32 {
|
||||
if k == 1 {
|
||||
return 0;
|
||||
}
|
||||
let n = nums.len();
|
||||
let mut pa = vec![0;n+1];
|
||||
let mut pa_sum = vec![0;n+1];
|
||||
for i in 1..=n {
|
||||
pa[i] = pa[i-1] + nums[i-1];
|
||||
if nums[i-1] == 1 {
|
||||
pa_sum[i] = pa_sum[i-1] + i as i64 - 1;
|
||||
} else {
|
||||
pa_sum[i] = pa_sum[i-1];
|
||||
}
|
||||
}
|
||||
let part = k >> 1;
|
||||
let mut res = i64::MAX;
|
||||
for (i,&x) in nums.iter().enumerate() {
|
||||
let i = i as i32;
|
||||
if x == 0 { continue; }
|
||||
let lsr = if k & 1 == 0 {i} else {i-1};
|
||||
let rsl = i + 1;
|
||||
if lsr < 0 || rsl >= n as i32 {continue;}
|
||||
if let Ok(li) = pa[0..=lsr as usize+1].binary_search_by_key(&-part, |t|t-pa[lsr as usize+1]) {
|
||||
if let Ok(ri) = pa[rsl as usize+1..=n].binary_search_by_key(&part, |t|t-pa[rsl as usize]) {
|
||||
let right_sum = pa_sum[ri+rsl as usize + 1] - pa_sum[rsl as usize] - sum(rsl, part);
|
||||
let left_sum = sum(lsr - part + 1, part) - pa_sum[lsr as usize + 1] + pa_sum[li];
|
||||
res = res.min(left_sum + right_sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
res as i32
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nums: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let k: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::min_moves(nums, k));
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
struct Solution;
|
||||
|
||||
struct Sol {
|
||||
jobs: Vec<i32>,
|
||||
k: i32,
|
||||
n: usize,
|
||||
tm: Vec<i32>,
|
||||
}
|
||||
|
||||
impl Sol {
|
||||
fn init(&mut self, i: usize, curr_st: usize, curr_tm: i32) {
|
||||
if i == self.n {
|
||||
self.tm[curr_st] = curr_tm;
|
||||
return;
|
||||
}
|
||||
self.init(i + 1, curr_st, curr_tm);
|
||||
self.init(i + 1, curr_st | 1 << i, curr_tm + self.jobs[i]);
|
||||
}
|
||||
fn dfs(&mut self, i: usize, cnt: i32, curr_st: usize, curr_tm: i32, tm_res: i32) -> bool {
|
||||
// eprintln!("{} {} {:#b} {}", i, cnt, curr_st, curr_tm);
|
||||
if cnt == self.k {
|
||||
return curr_st + 1 == 1 << self.n;
|
||||
}
|
||||
if i == self.tm.len() {
|
||||
return false;
|
||||
}
|
||||
self.dfs(i + 1, cnt, curr_st, curr_tm, tm_res) ||
|
||||
if curr_st & i == 0 && self.tm[i] <= tm_res {
|
||||
self.dfs(i + 1, cnt + 1, curr_st | i, curr_tm.max(self.tm[i]), tm_res)
|
||||
} else {false}
|
||||
}
|
||||
fn solve(&mut self) -> i32 {
|
||||
self.init(0, 0, 0);
|
||||
let mut l = *self.jobs.iter().min().unwrap();;
|
||||
let mut r = self.jobs.iter().sum::<i32>();
|
||||
while l < r {
|
||||
let mid = l + r >> 1;
|
||||
if self.dfs(0, 0, 0, 0, mid) {
|
||||
r = mid;
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
l
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn minimum_time_required(jobs: Vec<i32>, k: i32) -> i32 {
|
||||
let n = jobs.len();
|
||||
let mut sol = Sol {jobs, k, n, tm: vec![0;1<<n]};
|
||||
sol.solve()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let jobs: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let k: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::minimum_time_required(jobs, k));
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn is_valid(s: String) -> bool {
|
||||
let s = s.into_bytes();
|
||||
let mut st = vec![];
|
||||
for x in s {
|
||||
if matches!(st.last(), Some(y) if x - y == 2 || x - y == 1) {
|
||||
st.pop();
|
||||
} else {
|
||||
st.push(x);
|
||||
}
|
||||
}
|
||||
st.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
/// 状压dp
|
||||
/// 状态c表示c的每一位1对应的饼干包都已经被分发
|
||||
/// dp[i][j] 表示分给前 i 个人,状态为 j 时每个人获得的最大饼干数量。
|
||||
/// 新的状态中,考虑是否有新的人出现。如果有新的人出现,那么枚举他分到了哪些饼干
|
||||
/// dp[i][j] = min(max(dp[i-1][j^t],cookies_sum[t])),其中t|j==j
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn distribute_cookies(cookies: Vec<i32>, k: i32) -> i32 {
|
||||
let k = k as usize;
|
||||
let n = cookies.len();
|
||||
let mut cookies_sum = vec![0;1<<n];
|
||||
let mut dp = vec![vec![i32::MAX;1<<n];k+1];
|
||||
for j in 1..1<<n {
|
||||
for i in 0..n {
|
||||
if (j >> i) & 1 == 1 {
|
||||
cookies_sum[j] += cookies[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
dp[0][0] = 0;
|
||||
for i in 1..=k {
|
||||
dp[i][0] = 0;
|
||||
for j in 1..1<<n {
|
||||
let mut t = j;
|
||||
loop {
|
||||
dp[i][j] = dp[i][j].min(dp[i-1][j^t].max(cookies_sum[t]));
|
||||
if t == 0 {
|
||||
break;
|
||||
}
|
||||
t = (t - 1) & j;
|
||||
}
|
||||
}
|
||||
}
|
||||
dp[k][(1<<n)-1]
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let cookies: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let k: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::distribute_cookies(cookies, k));
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn product_except_self(a: Vec<i32>) -> Vec<i32> {
|
||||
let n = a.len();
|
||||
let mut pa = vec![1;n+1];
|
||||
for i in 1..=n {
|
||||
pa[i] = pa[i-1] * a[i-1];
|
||||
}
|
||||
let mut sa = vec![1;n+1];
|
||||
for i in (0..n).rev() {
|
||||
sa[i] = sa[i+1] * a[i];
|
||||
}
|
||||
let mut res = vec![];
|
||||
for i in 0..n {
|
||||
res.push(sa[i+1]*pa[i]);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
println!("{:?}", Solution::product_except_self(a));
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn maximum_or(nums: Vec<i32>, k: i32) -> i64 {
|
||||
let n = nums.len();
|
||||
let k = k as i64;
|
||||
let mut pa = vec![0;n+1];
|
||||
for i in 1..=n {
|
||||
pa[i] = pa[i-1] | nums[i-1] as i64;
|
||||
}
|
||||
let mut sa = vec![0;n+1];
|
||||
for i in (0..n).rev() {
|
||||
sa[i] = sa[i+1] | nums[i] as i64;
|
||||
}
|
||||
(0..n).fold(0, |res, i|res.max((nums[i] as i64) <<k | pa[i] | sa[i+1]))
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nums: Vec<i32> = buf.trim().split(' ').map(|x|x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let k: i32 = buf.trim().parse::<i32>().unwrap();
|
||||
println!("{}", Solution::maximum_or(nums, k));
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_length(s: String) -> i32 {
|
||||
let mut s = s.into_bytes();
|
||||
let mut n = s.len();
|
||||
let mut f = true;
|
||||
while f && n != 0 {
|
||||
f = false;
|
||||
for i in 0..n-1 {
|
||||
if s[i] == 65 && s[i+1] == 66 || s[i] == 67 && s[i+1] == 68 {
|
||||
s.remove(i);
|
||||
s.remove(i);
|
||||
n -= 2;
|
||||
f = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
n as i32
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let s: String = buf.trim().parse::<String>().unwrap();
|
||||
println!("{}", Solution::min_length(s));
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
struct Solution;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
struct Trie {
|
||||
mark: bool,
|
||||
nxt: HashMap<u8, Box<Trie>>,
|
||||
}
|
||||
|
||||
impl Trie {
|
||||
fn new() -> Self {
|
||||
Self {mark: false, nxt: HashMap::new()}
|
||||
}
|
||||
fn insert(&mut self, s: Vec<u8>) {
|
||||
let mut ptr = self;
|
||||
for x in s.into_iter().rev() {
|
||||
ptr = ptr.nxt.entry(x).or_insert(Box::new(Trie{mark: false, nxt: HashMap::new()}));
|
||||
}
|
||||
ptr.mark = true;
|
||||
}
|
||||
fn query(&self, s: &Vec<u8>) -> Vec<usize> {
|
||||
let mut ptr = self;
|
||||
let mut res = vec![];
|
||||
for (i, &x) in s.iter().rev().enumerate() {
|
||||
if ptr.mark {
|
||||
res.push(i);
|
||||
}
|
||||
if let Some(p) = ptr.nxt.get(&x) {
|
||||
ptr = p.as_ref();
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
if ptr.mark {
|
||||
res.push(s.len());
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl Solution {
|
||||
pub fn min_extra_char(s: String, dictionary: Vec<String>) -> i32 {
|
||||
let s = s.into_bytes();
|
||||
let n = s.len();
|
||||
let mut trie = Trie::new();
|
||||
for word in dictionary {
|
||||
trie.insert(word.into_bytes());
|
||||
}
|
||||
// eprintln!("{:?}", trie.query(&("leetcode".to_string().into_bytes())));
|
||||
// eprintln!("{:?}", trie.query(&("code".to_string().into_bytes())));
|
||||
let mut dp = vec![0;n+1];
|
||||
let mut query_string = vec![];
|
||||
for i in 1..=n {
|
||||
query_string.push(s[i-1]);
|
||||
let matchings = trie.query(&query_string);
|
||||
dp[i] = (0..i).fold(usize::MAX, |c, j|c.min(i - j + dp[j]));
|
||||
dp[i] = dp[i].min(matchings.into_iter().fold(usize::MAX, |c,j|c.min(dp[i-j])));
|
||||
}
|
||||
dp[n] as i32
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let s: String = buf.trim().parse::<String>().unwrap();
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let dictionary: Vec<String> = buf.trim().split(' ').map(|x|x.parse::<String>().unwrap()).collect::<Vec<String>>();
|
||||
println!("{}", Solution::min_extra_char(s, dictionary));
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
// Definition for singly-linked list.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
struct Solution;
|
||||
|
||||
|
||||
impl Solution {
|
||||
pub fn insert_greatest_common_divisors(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||
let gcd = |mut x: i32, mut y: i32| {
|
||||
while y != 0 {
|
||||
(x, y) = (y, x % y);
|
||||
}
|
||||
x
|
||||
};
|
||||
let mut t = head.as_mut().unwrap();
|
||||
while t.next != None {
|
||||
let next = t.next.take().unwrap();
|
||||
let mut mid = Box::new(ListNode::new(gcd(t.val, next.val)));
|
||||
mid.next = Some(next);
|
||||
t.next = Some(mid);
|
||||
t = t.next.as_mut().unwrap().next.as_mut().unwrap();
|
||||
}
|
||||
head
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn longest_valid_parentheses(s: String) -> i32 {
|
||||
let mut st = vec![];
|
||||
let s = s.into_bytes();
|
||||
let mut res = 0;
|
||||
for (i, &x) in s.iter().enumerate() {
|
||||
if x == 41 && matches!(st.last(), Some(j) if s[j - 1] == 40) {
|
||||
st.pop();
|
||||
res = res.max(i + 1 - st.last().unwrap_or(&0));
|
||||
} else {
|
||||
st.push(i + 1);
|
||||
}
|
||||
}
|
||||
res as i32
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
|
@ -0,0 +1,55 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
#define mod(x, y) (((x) % (y) + (y)) % (y))
|
||||
using ll = int64_t;
|
||||
constexpr ll prime = 1e9 + 7;
|
||||
constexpr ll b1 = 825;
|
||||
constexpr ll b2 = 87825;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
string longestPalindrome(string s) {
|
||||
int n = s.length();
|
||||
vector<ll> pa1(n + 1), pa2(n + 1);
|
||||
ll hash1 = 0, hash2 = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
hash1 = (hash1 * b1 + s[i - 1]) % prime;
|
||||
hash2 = (hash2 * b2 + s[i - 1]) % prime;
|
||||
pa1[i] = hash1;
|
||||
pa2[i] = hash2;
|
||||
}
|
||||
vector<ll> sa1(n + 1), sa2(n + 1);
|
||||
hash1 = 0, hash2 = 0;
|
||||
for (int i = n - 1; i >= 0; --i) {
|
||||
hash1 = (hash1 * b1 + s[i]) % prime;
|
||||
hash2 = (hash2 * b2 + s[i]) % prime;
|
||||
sa1[i] = hash1;
|
||||
sa2[i] = hash2;
|
||||
}
|
||||
vector<ll> p1(n + 1), p2(n + 1);
|
||||
p1[0] = 1, p2[0] = 1;
|
||||
for (int i = 1; i < n; ++i) {
|
||||
p1[i] = (p1[i - 1] * b1) % prime;
|
||||
p2[i] = (p2[i - 1] * b2) % prime;
|
||||
}
|
||||
function<bool(int, int)> check_palindrome = [&] (int l, int r) {
|
||||
int p = l + r >> 1, q = l + r + 1 >> 1;
|
||||
return mod((pa1[p + 1] - pa1[l] * p1[p + 1 - l]), prime) == mod((sa1[q] - sa1[r + 1] * p1[r + 1 - q]), prime) &&
|
||||
mod((pa2[p + 1] - pa2[l] * p2[p + 1 - l]), prime) == mod((sa2[q] - sa2[r + 1] * p2[r + 1 - q]), prime);
|
||||
};
|
||||
int l = 0;
|
||||
pair<int, int> res;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
l = max(0, l - 1);
|
||||
while (!check_palindrome(l, i)) ++l;
|
||||
res = max(res, {i - l + 1, l});
|
||||
}
|
||||
return s.substr(res.second, res.first);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
string s; cin >> s;
|
||||
cout << Solution().longestPalindrome(s);
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
// Definition for singly-linked list.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn delete_duplicates(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
|
||||
let mut c = head.as_mut();
|
||||
while let Some(curr) = c {
|
||||
while let Some(next) = curr.next.as_mut() {
|
||||
if next.val == curr.val {
|
||||
curr.next = next.next.take();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
c = curr.next.as_mut();
|
||||
}
|
||||
head
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
struct Item {
|
||||
time: i64,
|
||||
value: i64,
|
||||
cost: f64
|
||||
}
|
||||
|
||||
impl Item {
|
||||
fn new(time: i64, value: i64) -> Self {
|
||||
Item { time, value, cost : value as f64 / time as f64 }
|
||||
}
|
||||
}
|
||||
|
||||
struct Sol {
|
||||
a: Vec<Item>,
|
||||
tt: i64,
|
||||
m: usize,
|
||||
res: i64
|
||||
}
|
||||
|
||||
impl Sol {
|
||||
fn estimate(&self, mut curr_t: i64, next_idx: usize) -> f64 {
|
||||
let mut res = 0.;
|
||||
for i in next_idx..self.m {
|
||||
if self.a[i].time + curr_t > self.tt {
|
||||
return res + (self.tt - curr_t) as f64 * self.a[i].cost;
|
||||
}
|
||||
curr_t += self.a[i].time;
|
||||
res += self.a[i].value as f64;
|
||||
}
|
||||
res
|
||||
}
|
||||
fn dfs(&mut self, curr_t: i64, idx: usize, curr_v: i64) {
|
||||
self.res = self.res.max(curr_v);
|
||||
if idx == self.m {
|
||||
return;
|
||||
}
|
||||
if curr_t + self.a[idx].time <= self.tt {
|
||||
self.dfs(curr_t + self.a[idx].time, idx + 1, curr_v + self.a[idx].value);
|
||||
}
|
||||
if curr_v + self.estimate(curr_t, idx + 1).ceil() as i64 > self.res {
|
||||
self.dfs(curr_t, idx + 1, curr_v);
|
||||
}
|
||||
}
|
||||
fn solve(&mut self) -> i64 {
|
||||
self.dfs(0, 0, 0);
|
||||
self.res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tm: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let (tt, m) = (tm[0], tm[1]);
|
||||
let mut a = Vec::<Item>::with_capacity(m as usize);
|
||||
for _ in 0..m {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let mut iter = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap());
|
||||
a.push(Item::new(iter.next().unwrap(), iter.next().unwrap()));
|
||||
}
|
||||
a.sort_unstable_by(|x,y|y.cost.total_cmp(&x.cost));
|
||||
let mut sol = Sol { a, tt, m: m as usize, res: 0 };
|
||||
println!("{}", sol.solve());
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = int64_t;
|
||||
|
||||
ll solve(vector<vector<ll>> boxes) {
|
||||
int m = boxes.size();
|
||||
int n = m * 3;
|
||||
for (int i = 0; i < m; ++i) {
|
||||
boxes.push_back({boxes[i][0], boxes[i][2], boxes[i][1]});
|
||||
boxes.push_back({boxes[i][1], boxes[i][2], boxes[i][0]});
|
||||
}
|
||||
vector<vector<int>> ch(n);
|
||||
vector<int> ind(n, 0);
|
||||
function<bool(const vector<ll>&, const vector<ll>&)> is_fit = [](const vector<ll>& x, const vector<ll>& y) {
|
||||
return x[0] < y[0] && x[1] < y[1] || x[0] < y[1] && x[1] < y[0];
|
||||
};
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n ;++j) {
|
||||
if (is_fit(boxes[j], boxes[i])) {
|
||||
ch[i].push_back(j);
|
||||
++ind[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
ll res = 0;
|
||||
deque<int> dq;
|
||||
vector<ll> dp(n, 0);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
if (!ind[i]) dq.push_back(i);
|
||||
}
|
||||
while (!dq.empty()) {
|
||||
int i = dq.front();
|
||||
dq.pop_front();
|
||||
dp[i] += boxes[i][2];
|
||||
res = max(res, dp[i]);
|
||||
for (auto&& j : ch[i]) {
|
||||
dp[j] = max(dp[j], dp[i]);
|
||||
--ind[j];
|
||||
if (!ind[j]) dq.push_back(j);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int i = 0;
|
||||
while (1) {
|
||||
++i;
|
||||
int n;
|
||||
cin >> n;
|
||||
if (!n) break;
|
||||
vector<vector<ll>> boxes(n);
|
||||
for (int i = 0;i < n; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
int t; cin >> t; boxes[i].push_back(t);
|
||||
}
|
||||
}
|
||||
cout << "Case " << i << ": maximum height = " << solve(move(boxes)) << endl;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
fn solve(mut boxes: Vec<Vec<i64>>) -> i64 {
|
||||
let m = boxes.len();
|
||||
let n = m * 3;
|
||||
for i in 0..m {
|
||||
let curr = boxes[i].to_owned();
|
||||
boxes.push(vec![curr[0], curr[2], curr[1]]);
|
||||
boxes.push(vec![curr[1], curr[2], curr[0]]);
|
||||
}
|
||||
let mut ch = vec![vec![];n];
|
||||
let mut ind = vec![0;n];
|
||||
let is_fit = |x: &Vec<i64>, y: &Vec<i64>| {
|
||||
x[0] < y[0] && x[1] < y[1] || x[0] < y[1] && x[1] < y[0]
|
||||
};
|
||||
for i in 0..n {
|
||||
for j in 0..n {
|
||||
if is_fit(&boxes[j], &boxes[i]) {
|
||||
ch[i].push(j);
|
||||
ind[j] += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut res = 0;
|
||||
let mut dq = VecDeque::new();
|
||||
let mut dp = vec![0;n];
|
||||
for i in 0..n {
|
||||
if ind[i] == 0 {
|
||||
dq.push_back(i);
|
||||
}
|
||||
}
|
||||
while let Some(i) = dq.pop_front() {
|
||||
dp[i] += boxes[i][2];
|
||||
res = res.max(dp[i]);
|
||||
for &j in &ch[i] {
|
||||
dp[j] = dp[j].max(dp[i]);
|
||||
ind[j] -= 1;
|
||||
if ind[j] == 0 {
|
||||
dq.push_back(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut i = 0;
|
||||
loop {
|
||||
i += 1;
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let n: usize = buf.trim().parse::<usize>().unwrap();
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
let mut boxes = Vec::with_capacity(n);
|
||||
for _ in 0..n {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let v: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
boxes.push(v);
|
||||
}
|
||||
println!("Case {i}: maximum height = {}", solve(boxes));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,80 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Edge {
|
||||
to: usize,
|
||||
flow: i64,
|
||||
cap: i64,
|
||||
rev: usize,
|
||||
}
|
||||
|
||||
struct Net(Vec<Vec<Edge>>, Vec<usize>, Vec<bool>);
|
||||
|
||||
impl Net {
|
||||
fn new(n: usize) -> Self {
|
||||
Self(vec![vec![]; n+1], vec![0; n+1], vec![false; n+1])
|
||||
}
|
||||
fn add_edge(&mut self, from: usize, to: usize, cap: i64) {
|
||||
let to_edge = Edge {to, cap, flow: 0, rev: self.0[to].len()};
|
||||
self.0[from].push(to_edge);
|
||||
let from_edge = Edge {to: from, flow: 0, cap: 0, rev: self.0[from].len() - 1};
|
||||
self.0[to].push(from_edge);
|
||||
}
|
||||
fn bfs(&mut self, s: usize, t: usize) -> bool {
|
||||
self.1.fill(0);
|
||||
let mut dq = VecDeque::new();
|
||||
self.1[s] = 1;
|
||||
dq.push_back((s, 1));
|
||||
while let Some((v, l)) = dq.pop_front() {
|
||||
for e in &self.0[v] {
|
||||
if self.1[e.to] == 0 && e.cap > e.flow {
|
||||
self.1[e.to] = l + 1;
|
||||
dq.push_back((e.to, l + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
self.1[t] != 0
|
||||
}
|
||||
fn dfs(&mut self, s: usize, t: usize, cap: i64) -> i64 {
|
||||
if self.2[s] { return 0; }
|
||||
self.2[s] = true;
|
||||
if s == t { return cap; }
|
||||
let mut res = 0;
|
||||
let n = self.0[s].len();
|
||||
for i in 0..n {
|
||||
let e = self.0[s][i];
|
||||
if e.cap > e.flow && self.1[e.to] == self.1[s] + 1 {
|
||||
let new = self.dfs(e.to, t, (cap - res).min(e.cap - e.flow));
|
||||
self.0[s][i].flow += new;
|
||||
self.0[e.to][e.rev].flow -= new;
|
||||
res += new;
|
||||
if res == cap { return res; }
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
fn max_flow(&mut self, s: usize, t: usize) -> i64 {
|
||||
let mut res = 0;
|
||||
while self.bfs(s, t) {
|
||||
self.2.fill(false);
|
||||
res += self.dfs(s, t, i64::MAX);
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nmst: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, m, s, t) = (nmst[0], nmst[1], nmst[2], nmst[3]);
|
||||
let mut net = Net::new(n);
|
||||
for _ in 0..m {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let uvc: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
let (u, v, c) = (uvc[0] as usize, uvc[1] as usize, uvc[2]);
|
||||
net.add_edge(u, v, c);
|
||||
}
|
||||
println!("{}", net.max_flow(s, t));
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
use std::collections::VecDeque;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Edge {
|
||||
to: usize,
|
||||
cap: i64,
|
||||
flow: i64,
|
||||
rev: (usize, usize)
|
||||
}
|
||||
|
||||
struct Net(Vec<Vec<Edge>>);
|
||||
|
||||
impl Net {
|
||||
fn new(n: usize) -> Self {
|
||||
Self(vec![vec![];n+1])
|
||||
}
|
||||
fn add_edge(&mut self, from: usize, to: usize, cap: i64) {
|
||||
let to_edge = Edge {to, cap, flow: 0, rev: (to, self.0[to].len())};
|
||||
self.0[from].push(to_edge);
|
||||
let from_edge = Edge {to: from, cap: 0, flow: 0, rev: (from, self.0[from].len() - 1)};
|
||||
self.0[to].push(from_edge);
|
||||
}
|
||||
fn max_flow(&mut self, s: usize, t: usize) -> i64 {
|
||||
let n = self.0.len();
|
||||
let mut pa = vec![(0, 0);n+1];
|
||||
let mut res = 0;
|
||||
loop {
|
||||
let mut pf = vec![0;n+1];
|
||||
let mut dq = VecDeque::new();
|
||||
dq.push_back(s);
|
||||
pf[s] = i64::MAX;
|
||||
while let Some(v) = dq.pop_front() {
|
||||
for (i, ne) in self.0[v].iter().enumerate() {
|
||||
if pf[ne.to] == 0 && ne.cap > ne.flow {
|
||||
pf[ne.to] = pf[v].min(ne.cap - ne.flow);
|
||||
pa[ne.to] = (v, i);
|
||||
dq.push_back(ne.to);
|
||||
}
|
||||
}
|
||||
if pf[t] != 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if pf[t] == 0 {
|
||||
break;
|
||||
}
|
||||
let mut p = t;
|
||||
while pa[p].0 != 0 {
|
||||
let (x, y) = pa[p];
|
||||
self.0[x][y].flow += pf[t];
|
||||
let (z, w) = self.0[x][y].rev;
|
||||
self.0[z][w].flow -= pf[t];
|
||||
p = x;
|
||||
}
|
||||
res += pf[t];
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tmp: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, m, s, t) = (tmp[0], tmp[1], tmp[2], tmp[3]);
|
||||
let mut net = Net::new(n);
|
||||
for _ in 0..m {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let tmp: Vec<i64> = buf.trim().split(' ').map(|x|x.parse::<i64>().unwrap()).collect::<Vec<i64>>();
|
||||
net.add_edge(tmp[0] as usize, tmp[1] as usize, tmp[2]);
|
||||
}
|
||||
println!("{}", net.max_flow(s, t));
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
using ll = int64_t;
|
||||
constexpr int MAXN = 42;
|
||||
|
||||
int n, m;
|
||||
ll a[MAXN][MAXN];
|
||||
ll x[MAXN][MAXN];
|
||||
|
||||
int main() {
|
||||
cin >> n >> m;
|
||||
ll cnt1 = 0, sum1 = 0, cnt2 = 0, sum2 = 0;
|
||||
for(int i = 0; i < n; ++i) {
|
||||
for(int j = 0; j < m; ++j) {
|
||||
cin >> a[i][j];
|
||||
if ((i + j) & 1) {
|
||||
++cnt1;
|
||||
sum1 += a[i][j];
|
||||
} else {
|
||||
++cnt2;
|
||||
sum2 += a[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
ll l = 0, r = LLONG_MAX / (MAXN * MAXN);
|
||||
if ((n * m) & 1) {
|
||||
l = (sum1 - sum2) / (cnt1 - cnt2);
|
||||
r = l + 1;
|
||||
}
|
||||
ll res = LLONG_MAX;
|
||||
int o = 0;
|
||||
while(l < r) {
|
||||
memset(x, 0, sizeof(x));
|
||||
ll mid = l + r >> 1;
|
||||
cerr << mid << endl;
|
||||
int f = 1;
|
||||
ll cnt = 0;
|
||||
for(int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < m; ++j) {
|
||||
ll available = mid - a[i][j] - x[i][j];
|
||||
if (available < 0) {
|
||||
f = 0;
|
||||
goto finish;
|
||||
}
|
||||
if (i + 1 == n) {
|
||||
if (j + 1 == m) {
|
||||
continue;
|
||||
}
|
||||
ll lo = min(available, mid - a[i][j+1] - x[i][j+1]);
|
||||
x[i][j+1] += lo;
|
||||
if (available != lo) {
|
||||
f = 0;
|
||||
goto finish;
|
||||
}
|
||||
} else {
|
||||
if (j + 1 == m) {
|
||||
ll lo = min(available, mid - a[i+1][j] - x[i+1][j]);
|
||||
x[i+1][j] += lo;
|
||||
if (available != lo) {
|
||||
f = 0;
|
||||
goto finish;
|
||||
}
|
||||
} else {
|
||||
ll lo = min(available, mid - a[i][j+1] - x[i][j+1]);
|
||||
x[i][j+1] += lo;
|
||||
x[i+1][j] += available - lo;
|
||||
}
|
||||
}
|
||||
cnt += 2 * available;
|
||||
}
|
||||
}
|
||||
finish:
|
||||
if(f) {
|
||||
r = mid;
|
||||
o = 1;
|
||||
res = min(res, cnt);
|
||||
} else {
|
||||
l = mid + 1;
|
||||
}
|
||||
}
|
||||
if (o) {
|
||||
cout << res;
|
||||
} else {
|
||||
cout << -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
struct Sol {
|
||||
dp: Vec<usize>,
|
||||
pow: Vec<usize>,
|
||||
}
|
||||
|
||||
impl Sol {
|
||||
fn solve(&self, mut x: usize) -> Vec<usize> {
|
||||
let mut cnt = x;
|
||||
let mut digit = vec![];
|
||||
let mut res = vec![0;10];
|
||||
let mut len = 0;
|
||||
digit.push(0);
|
||||
while x != 0 {
|
||||
len += 1;
|
||||
digit.push(x as usize % 10);
|
||||
x /= 10;
|
||||
}
|
||||
for i in (1..=len).rev() {
|
||||
cnt -= digit[i] * self.pow[i-1];
|
||||
res[digit[i]] += cnt + 1;
|
||||
for b in 0..10 {
|
||||
res[b] += self.dp[i-1] * digit[i];
|
||||
}
|
||||
for j in 0..digit[i] {
|
||||
res[j] += self.pow[i-1];
|
||||
}
|
||||
res[0] -= self.pow[i-1];
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let ab: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let a = ab[0];
|
||||
let b = ab[1];
|
||||
let mut dp = vec![0;13];
|
||||
let mut pow = vec![1;13];
|
||||
for i in 1..13 {
|
||||
pow[i] = pow[i-1] * 10;
|
||||
dp[i] = dp[i-1] * 10 + pow[i-1];
|
||||
}
|
||||
let sol = Sol { dp, pow };
|
||||
let res_a_1 = sol.solve(a - 1);
|
||||
let res_b = sol.solve(b);
|
||||
for i in 0..10 {
|
||||
print!("{} ", res_b[i] - res_a_1[i]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,160 @@
|
|||
/////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Useful Macros
|
||||
* by subcrip
|
||||
* (requires C++17)
|
||||
*/
|
||||
|
||||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
/* macro helpers */
|
||||
#define __NARGS(...) std::tuple_size<decltype(std::make_tuple(__VA_ARGS__))>::value
|
||||
#define __DECOMPOSE_S(a, x) auto x = a;
|
||||
#define __DECOMPOSE_N(a, ...) auto [__VA_ARGS__] = a;
|
||||
constexpr void __() {}
|
||||
#define __AS_PROCEDURE(...) __(); __VA_ARGS__; __()
|
||||
#define __as_typeof(container) decltype(container)::value_type
|
||||
|
||||
/* type aliases */
|
||||
using ll = int64_t;
|
||||
using ull = uint64_t;
|
||||
using pii = pair<int, int>;
|
||||
using pil = pair<int, ll>;
|
||||
using pli = pair<ll, int>;
|
||||
using pll = pair<ll, ll>;
|
||||
|
||||
/* constants */
|
||||
constexpr int INF = 0x3f3f3f3f;
|
||||
constexpr ull MDL = 1e9 + 7;
|
||||
constexpr ull PRIME = 998'244'353;
|
||||
constexpr ull MDL1 = 825;
|
||||
constexpr ull MDL2 = 87825;
|
||||
|
||||
/* bit-wise operations */
|
||||
#define lowbit(x) ((x) & -(x))
|
||||
#define popcount(x) (__builtin_popcountll(ll(x)))
|
||||
#define parity(x) (__builtin_parityll(ll(x)))
|
||||
#define msp(x) (63LL - __builtin_clzll(ll(x)))
|
||||
#define lsp(x) (__builtin_ctzll(ll(x)))
|
||||
|
||||
/* arithmetic operations */
|
||||
#define mod(x, y) (((x) + (y)) % (y))
|
||||
|
||||
/* fast pairs */
|
||||
#define upair ull
|
||||
#define umake(x, y) (ull(x) << 32 | ull(y))
|
||||
#define u1(p) ((p) >> 32)
|
||||
#define u2(p) ((p) & ((1ULL << 32) - 1))
|
||||
#define ult std::less<upair>
|
||||
#define ugt std::greater<upair>
|
||||
|
||||
#define ipair ull
|
||||
#define imake(x, y) (umake(x, y))
|
||||
#define i1(p) (int(u1(p)))
|
||||
#define i2(p) (int(u2(p)))
|
||||
struct ilt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) < i2(b);
|
||||
else return i1(a) < i1(b);
|
||||
}
|
||||
};
|
||||
struct igt {
|
||||
bool operator()(const ipair& a, const ipair& b) const {
|
||||
if (i1(a) == i1(b)) return i2(a) > i2(b);
|
||||
else return i1(a) > i1(b);
|
||||
}
|
||||
};
|
||||
|
||||
/* conditions */
|
||||
#define loop while (1)
|
||||
#define if_or(var, val) if (!(var == val)) var = val; else
|
||||
#define continue_or(var, val) __AS_PROCEDURE(if (var == val) continue; var = val;)
|
||||
#define break_or(var, val) __AS_PROCEDURE(if (var == val) break; var = val;)
|
||||
|
||||
/* build data structures */
|
||||
#define unordered_counter(from, to) __AS_PROCEDURE(unordered_map<__as_typeof(from), size_t> to; for (auto&& x : from) ++to[x];)
|
||||
#define counter(from, to, cmp) __AS_PROCEDURE(map<__as_typeof(from), size_t, cmp> to; for (auto&& x : from) ++to[x];)
|
||||
#define pa(a) __AS_PROCEDURE(__typeof(a) pa; pa.push_back({}); for (auto&&x : a) pa.push_back(pa.back() + x);)
|
||||
#define sa(a) __AS_PROCEDURE(__typeof(a) sa(a.size() + 1); {int n = a.size(); for (int i = n - 1; i >= 0; --i) sa[i] = sa[i + 1] + a[i];};)
|
||||
#define adj(ch, n) __AS_PROCEDURE(vector<vector<int>> ch((n) + 1);)
|
||||
#define edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v), ch[v].push_back(u);)
|
||||
#define Edge(ch, u, v) __AS_PROCEDURE(ch[u].push_back(v);)
|
||||
|
||||
/* io */
|
||||
#define untie __AS_PROCEDURE(ios_base::sync_with_stdio(0), cin.tie(NULL))
|
||||
template<typename T> void __read(T& x) { cin >> x; }
|
||||
template<typename T, typename... U> void __read(T& x, U&... args) { cin >> x; __read(args...); }
|
||||
#define read(type, ...) __AS_PROCEDURE(type __VA_ARGS__; __read(__VA_ARGS__);)
|
||||
#define readvec(type, a, n) __AS_PROCEDURE(vector<type> a(n); for (int i = 0; i < (n); ++i) cin >> a[i];)
|
||||
#define putvec(a) __AS_PROCEDURE(for (auto&& x : a) cout << x << ' '; cout << endl;)
|
||||
#define debug(x) __AS_PROCEDURE(cerr << #x" = " << x << endl;)
|
||||
#define debugvec(a) __AS_PROCEDURE(cerr << #a" = "; for (auto&& x : a) cerr << x << ' '; cerr << endl;)
|
||||
|
||||
/* pops */
|
||||
#define poptop(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.top(); q.pop();)
|
||||
#define popback(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.back(); q.pop_back();)
|
||||
#define popfront(q, ...) __AS_PROCEDURE(auto [__VA_ARGS__] = q.front();q.pop_front();)
|
||||
|
||||
/* algorithms */
|
||||
|
||||
vector<int> calc_next(string t) { // pi function of t
|
||||
int n = (int)t.length();
|
||||
vector<int> pi(n);
|
||||
for (int i = 1; i < n; i++) {
|
||||
int j = pi[i - 1];
|
||||
while (j > 0 && t[i] != t[j]) j = pi[j - 1];
|
||||
if (t[i] == t[j]) j++;
|
||||
pi[i] = j;
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
vector<int> calc_z(string t) { // z function of t
|
||||
int m = t.length();
|
||||
vector<int> z;
|
||||
z.push_back(m);
|
||||
pair<int, int> prev = {1, -1};
|
||||
for (int i = 1; i < m; ++i) {
|
||||
if (z[i - prev.first] + i <= prev.second) {
|
||||
z.push_back(z[i - prev.first]);
|
||||
} else {
|
||||
int j = max(i, prev.second + 1);
|
||||
while (j < m && t[j] == t[j - i]) ++j;
|
||||
z.push_back(j - i);
|
||||
prev = {i, j - 1};
|
||||
}
|
||||
}
|
||||
return z;
|
||||
}
|
||||
vector<int> kmp(string s, string t) { // find all t in s
|
||||
string cur = t + '#' + s;
|
||||
int sz1 = s.size(), sz2 = t.size();
|
||||
vector<int> v;
|
||||
vector<int> lps = calc_next(cur);
|
||||
for (int i = sz2 + 1; i <= sz1 + sz2; i++) {
|
||||
if (lps[i] == sz2) v.push_back(i - 2 * sz2);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
/////////////////////////////////////////////////////////
|
||||
|
||||
int main() {
|
||||
read(string, s);
|
||||
int n = s.length();
|
||||
string rev(s.rbegin(), s.rend());
|
||||
auto translate = [&] (int i) {return n - i - 1;};
|
||||
auto z = calc_z(s);
|
||||
auto pi = calc_next(rev);
|
||||
int res = 0;
|
||||
for (int i = 1; i < n - 1; ++i) {
|
||||
int suf = pi[translate(i)];
|
||||
if (suf > 0 && suf <= z[i]) {
|
||||
res = max(res, suf);
|
||||
}
|
||||
}
|
||||
if (res) {
|
||||
cout << s.substr(0, res);
|
||||
} else {
|
||||
cout << "Just a legend";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
def kmp(s, t): # find all occurrences of t in s
|
||||
n = len(s)
|
||||
m = len(t)
|
||||
next = [-1]
|
||||
j = -1
|
||||
i = 0
|
||||
while i < m:
|
||||
if j == -1 or t[i] == t[j]:
|
||||
i += 1
|
||||
j += 1
|
||||
if i != m and t[j] == t[i]:
|
||||
next.append(next[j])
|
||||
else:
|
||||
next.append(j)
|
||||
else:
|
||||
j = next[j]
|
||||
res = []
|
||||
i = 0
|
||||
j = 0
|
||||
while i < n and j < m:
|
||||
if j == -1 or s[i] == t[j]:
|
||||
i += 1
|
||||
j += 1
|
||||
if j == m:
|
||||
res.append(i - j)
|
||||
j = next[j]
|
||||
else:
|
||||
j = next[j]
|
||||
return res
|
|
@ -0,0 +1,37 @@
|
|||
fn main() {
|
||||
let mut buf = String::new();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let t: usize = buf.trim().parse::<usize>().unwrap();
|
||||
let mut curr = vec![0;1000];
|
||||
let mut tmp = vec![0;1000];
|
||||
for _ in 0..t {
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let nxz: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
let (n, x, z) = (nxz[0], nxz[1], nxz[2]);
|
||||
buf.clear();
|
||||
std::io::stdin().read_line(&mut buf).ok();
|
||||
let a: Vec<usize> = buf.trim().split(' ').map(|x|x.parse::<usize>().unwrap()).collect::<Vec<usize>>();
|
||||
curr.fill(usize::MAX);
|
||||
curr[z%x] = z;
|
||||
let mut res = usize::MAX;
|
||||
for &i in &a {
|
||||
for j in 0..x {
|
||||
tmp[j] = curr[j];
|
||||
}
|
||||
for j in 0..x {
|
||||
if tmp[j] != usize::MAX {
|
||||
curr[(j + i) % x] = curr[(j + i) % x].min(tmp[j] + i);
|
||||
if (tmp[j] + i) % x == 0 {
|
||||
res = res.min(curr[0] / x);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if res != usize::MAX {
|
||||
println!("{res}");
|
||||
} else {
|
||||
println!("-1");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
void solve() {
|
||||
int n, k, x; cin >> n >> k >> x;
|
||||
vector<int> a(n);
|
||||
for (int i = 0; i < n; ++i) cin >> a[i];
|
||||
sort(a.begin(), a.end(), greater<>());
|
||||
vector<int> pa(n + 1);
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
pa[i] = pa[i - 1] + a[i - 1];
|
||||
sum += a[i - 1];
|
||||
}
|
||||
function<int(int)> get = [&](int idx) {
|
||||
if (idx + x > n) {
|
||||
return pa[n] - pa[idx];
|
||||
} else {
|
||||
return pa[idx + x] - pa[idx];
|
||||
}
|
||||
};
|
||||
int res = sum - 2 * get(0);
|
||||
// cerr << "*" << sum << "*" << get(0) << "*" << res << endl;
|
||||
for (int i = 1; i <= k; ++i) {
|
||||
sum -= a[i-1];
|
||||
res = max(res, sum - 2 * get(i));
|
||||
// cerr << "*" << sum << "*" << get(i) << "*" << res << endl;
|
||||
}
|
||||
cout << res << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int t; cin >> t;
|
||||
while(t--) solve();
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
#include<bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
ios_base::sync_with_stdio(0), cin.tie(NULL);
|
||||
int nl, nr, m;
|
||||
cin >> nl >> nr >> m;
|
||||
vector<vector<int>> left(nl+1);
|
||||
vector<vector<int>> right(nr+1);
|
||||
vector<int> choice_left(nl+1);
|
||||
vector<int> choice_right(nr+1);
|
||||
vector<int> vis(nl+1);
|
||||
while(m--) {
|
||||
int v, u;
|
||||
cin >> v >> u;
|
||||
left[v].push_back(u);
|
||||
right[u].push_back(v);
|
||||
}
|
||||
function<bool(int)> dfs = [&](int v) -> bool {
|
||||
if (vis[v]) return false;
|
||||
vis[v] = true;
|
||||
for (auto&& u : left[v]) {
|
||||
if (choice_right[u]) {
|
||||
int t = choice_right[u];
|
||||
if (dfs(t)) {
|
||||
choice_left[v] = u;
|
||||
choice_right[u] = v;
|
||||
vis[v] = false;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
choice_left[v] = u;
|
||||
choice_right[u] = v;
|
||||
vis[v] = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
for (int i = 1; i <= nl; ++i) {
|
||||
if (!choice_left[i]) {
|
||||
dfs(i);
|
||||
}
|
||||
}
|
||||
vector<int> res;
|
||||
int cnt = 0;
|
||||
for (int i = 1; i <= nl; ++i) {
|
||||
if (choice_left[i]) {
|
||||
++cnt;
|
||||
}
|
||||
res.push_back(choice_left[i]);
|
||||
}
|
||||
cout << cnt << endl;
|
||||
for (auto&& j : res)
|
||||
cout << j << ' ';
|
||||
}
|
Loading…
Reference in New Issue