Идея: BledDest
Разбор
Tutorial is loading...
Решение (adedalic)
fun main() {
repeat(readLine()!!.toInt()) {
val s = readLine()!!
println(s.last() + s.substring(1))
}
}
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
using li = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
li n, k;
cin >> n >> k;
li ans = 0, cur = 1;
while (cur < k) {
cur *= 2;
++ans;
}
if (cur < n) ans += (n - cur + k - 1) / k;
cout << ans << '\n';
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
k += 1;
vector<int> a(n);
for (int& x : a) {
cin >> x;
int cur = 1;
while (x--) cur *= 10;
x = cur;
}
long long res = 0;
for (int i = 0; i < n; i++) {
int cnt = k;
if (i + 1 < n) cnt = min(cnt, a[i + 1] / a[i] - 1);
res += a[i] * 1LL * cnt;
k -= cnt;
}
cout << res << '\n';
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
const int INF = 1e9;
int main() {
int tc;
scanf("%d", &tc);
while (tc--){
int n, m;
scanf("%d%d", &n, &m);
vector<vector<int>> a(n, vector<int>(m));
forn(i, n) forn(j, m) scanf("%d", &a[i][j]);
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&a](int x, int y){ return a[x][0] > a[y][0]; });
vector<vector<int>> mxl(n, vector<int>(m, -INF));
vector<vector<int>> mnr(n, vector<int>(m, INF));
for (int i = n - 1; i >= 0; --i) forn(j, m){
mxl[i][j] = a[ord[i]][j];
if (i < n - 1) mxl[i][j] = max(mxl[i][j], mxl[i + 1][j]);
if (j > 0) mxl[i][j] = max(mxl[i][j], mxl[i][j - 1]);
}
for (int i = n - 1; i >= 0; --i) for (int j = m - 1; j >= 0; --j){
mnr[i][j] = a[ord[i]][j];
if (i < n - 1) mnr[i][j] = min(mnr[i][j], mnr[i + 1][j]);
if (j < m - 1) mnr[i][j] = min(mnr[i][j], mnr[i][j + 1]);
}
vector<int> mnl(m, INF), mxr(m, -INF);
pair<int, int> ans(-1, -1);
forn(i, n - 1){
forn(j, m){
mnl[j] = min(mnl[j], a[ord[i]][j]);
if (j > 0) mnl[j] = min(mnl[j], mnl[j - 1]);
}
for (int j = m - 1; j >= 0; --j){
mxr[j] = max(mxr[j], a[ord[i]][j]);
if (j < m - 1) mxr[j] = max(mxr[j], mxr[j + 1]);
}
forn(j, m - 1) if (mnl[j] > mxl[i + 1][j] && mxr[j + 1] < mnr[i + 1][j + 1]){
ans = {i, j};
}
}
if (ans.first == -1){
puts("NO");
continue;
}
puts("YES");
string res(n, '.');
forn(i, n) res[ord[i]] = i <= ans.first ? 'R' : 'B';
printf("%s %d\n", res.c_str(), ans.second + 1);
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
const int N = 505;
const int MOD = 998244353;
int n, x;
int c[N][N], dp[N][N];
int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
return x;
}
int mul(int x, int y) {
return x * 1ll * y % MOD;
}
int main() {
cin >> n >> x;
for (int i = 0; i <= n; ++i) {
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; ++j)
c[i][j] = add(c[i - 1][j], c[i - 1][j - 1]);
}
dp[n][0] = 1;
for (int i = n; i > 1; i--) {
for (int j = 0; j < x; ++j) {
if (!dp[i][j]) continue;
int pw = 1, nj = min(x, j + i - 1);
for (int k = i; k >= 0; k--) {
dp[k][nj] = add(dp[k][nj], mul(dp[i][j], mul(c[i][k], pw)));
pw = mul(pw, nj - j);
}
}
}
int ans = 0;
for (int i = 0; i <= x; ++i)
ans = add(ans, dp[0][i]);
cout << ans << endl;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
#include <bits/stdc++.h>
using namespace std;
struct Vertex
{
int cost;
int depth;
int idx;
Vertex() {};
Vertex(int cost, int depth, int idx) : cost(cost), depth(depth), idx(idx) {};
};
bool operator <(const Vertex& a, const Vertex& b)
{
if(a.cost != b.cost)
return a.cost > b.cost;
if(a.depth != b.depth)
return a.depth > b.depth;
return a.idx < b.idx;
}
struct DSU
{
int n;
vector<int> p;
vector<int> top;
int get(int x)
{
if(p[x] == x)
return x;
return p[x] = get(p[x]);
}
int get_top(int x)
{
return top[get(x)];
}
void merge(int x, int par)
{
p[x] = par;
}
DSU(int k = 0)
{
n = k;
p.resize(n);
iota(p.begin(), p.end(), 0);
top = p;
};
};
const int N = 200043;
int n;
vector<int> g[N];
int p[N], d[N], tin[N], tout[N];
int qv[N];
int qk[N];
int T = 0;
void dfs(int v = 0, int par = -1)
{
tin[v] = T++;
p[v] = par;
if(par == -1)
d[v] = 0;
else
d[v] = d[par] + 1;
for(auto x : g[v])
if(x != par)
dfs(x, v);
tout[v] = T;
}
pair<long long, long long> tree[4 * N];
pair<long long, long long> operator+(const pair<long long, long long>& a, const pair<long long, long long>& b)
{
return make_pair(a.first + b.first, a.second + b.second);
}
pair<long long, long long> get(int v, int l, int r, int L, int R)
{
if(L >= R) return {0, 0};
if(l == L && r == R) return tree[v];
int m = (l + r) / 2;
return get(v * 2 + 1, l, m, L, min(R, m)) + get(v * 2 + 2, m, r, max(m, L), R);
}
void add(int v, int l, int r, int pos, pair<long long, long long> val)
{
if(l == r - 1)
tree[v] = tree[v] + val;
else
{
int m = (l + r) / 2;
if(pos < m) add(v * 2 + 1, l, m, pos, val);
else add(v * 2 + 2, m, r, pos, val);
tree[v] = tree[v] + val;
}
}
pair<long long, long long> get_vertex_value(int v)
{
return get(0, 0, n, tin[v], tout[v]);
}
void add_on_path(int x, int y, pair<long long, long long> val)
{
// x is a descendant of y
add(0, 0, n, tin[x], val);
if(p[y] != -1)
add(0, 0, n, tin[p[y]], make_pair(-val.first, -val.second));
}
int calculate_cost(int v, int correction)
{
//cerr << v << " " << correction << endl;
pair<long long, long long> res = get_vertex_value(v);
// first - (second + 1) * k <= 0
long long k = (res.first + res.second) / (res.second + 1) - 1;
if(correction < k) return correction;
return k;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
--x;
--y;
g[x].push_back(y);
g[y].push_back(x);
}
int q;
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%d %d", &qv[i], &qk[i]);
--qv[i];
}
dfs();
DSU dsu(n);
vector<long long> ans(q);
set<Vertex> pq;
vector<Vertex> curv(n);
for(int i = 0; i < n; i++)
{
int count_children = g[i].size();
if(i != 0) count_children--;
add_on_path(i, i, make_pair((long long)(count_children), 0ll));
if(i != 0)
{
curv[i] = Vertex(calculate_cost(i, 200000), d[i], i);
pq.insert(curv[i]);
}
}
for(int i = 0; i < q; i++)
pq.insert(Vertex(qk[i], -1, i));
while(!pq.empty())
{
Vertex z = *pq.begin();
pq.erase(pq.begin());
if(z.depth == -1)
{
auto res = get_vertex_value(qv[z.idx]);
ans[z.idx] = res.first - res.second * z.cost;
}
else
{
int v = z.idx;
int pv = p[v];
int newtop = dsu.get_top(pv);
pair<long long, long long> val = get_vertex_value(v);
val.second++;
val.first--;
if(newtop != 0)
pq.erase(curv[newtop]);
add_on_path(pv, newtop, val);
if(newtop != 0)
curv[newtop].cost = calculate_cost(newtop, z.cost);
if(newtop != 0)
pq.insert(curv[newtop]);
dsu.merge(v, pv);
}
}
for(int i = 0; i < q; i++)
printf("%lld\n", ans[i]);
}