10月15日模拟赛题解

A 树

Limitations

$100\%$ $1 \leq b \leq n \leq 10^5,~a \leq 1000$

$60\%$ $1 \leq b，n\leq 1000$

$30\%$ $1 \leq b, n \leq 10$

Code

#include <cstdio>
#include <stack>
#include <vector>

const int maxn = 100010;

struct Edge {
int v;
Edge *nxt;

Edge(const int _v, Edge *h) : v(_v), nxt(h) {}
};
Edge *hd[maxn];

int n;
int b[maxn], ans[maxn];
bool vis[maxn];
std::stack<int>stk[maxn];
std::vector<int>son[maxn];

void dfs(const int u);

int main() {
freopen("tree.in", "r", stdin);
freopen("tree.out", "w", stdout);
qr(n);
for (int i = 1; i <= n; ++i) {
qr(ans[i]); qr(b[i]);
}
for (int i = 1, u, v; i < n; ++i) {
u = v = 0; qr(u); qr(v);
hd[u] = new Edge(v, hd[u]);
hd[v] = new Edge(u, hd[v]);
}
dfs(1);
for (int i = 1; i <= n; ++i) {
qw(ans[i], i == n ? '\n' : ' ', true);
}
return 0;
}

void dfs(const int u) {
vis[u] = true;
int k = b[u];
if (stk[k].size()) {
son[stk[k].top()].push_back(u);
}
stk[k].push(u);
for (auto e = hd[u]; e; e = e->nxt) if (!vis[e->v]) {
int v = e->v;
dfs(v);
}
for (auto v : son[u]) {
ans[u] += ans[v];
}
stk[k].pop();
}


B 小偷

Limitations

$100\%$ $3 \leq m \leq 10^{15}$

$60\%$ $3 \leq m \leq 100000$

$30\%$ $3 \leq m \leq 1000$

Solution

#include <cstdio>

typedef long long int ll;

ll m, ans;

ll calc(const ll n);

int main() {
freopen("thief.in", "r", stdin);
freopen("thief.out", "w", stdout);
qr(m);
for (ll l = 1, r = 1000000000000000000ll, mid = 1000000000ll; l <= r; mid = (l + r) >> 1) {
if (calc(mid) >= m) {
ans = mid; r = mid - 1;
} else {
l = mid + 1;
}
}
qw(calc(ans) == m ? ans : -1, '\n', true);
return 0;
}

ll calc(const ll n) {
ll _ret = 0;
for (int k = 2; ; ++k) {
ll cnt = 0;
for (ll i = n; i; i /= k) {
if ((++cnt) >= 4) { _ret += i; }
}
if (cnt < 4) break;
}
return _ret;
}


C 盘子

Limitations

$100\%$ $3 \leq n \leq 4000,~m \leq 10^9, r_i \leq 10^5$

$60\%$ $3 \leq n \leq 100,~~m \leq 200$

$30\%$ $2 \leq n \leq 5,~~m \leq 30$

Solution

$30\%$ 爆搜即可。时间复杂度 $C_{m}^{n}$

posted @ 2019-10-16 00:45  一扶苏一  阅读(150)  评论(0编辑  收藏  举报