October 15 race simulation solution to a problem

October 15 race simulation solution to a problem

A tree

Description

Given a \ (n-\) nodes of the tree, each node has two parameters \ (A, B ~ \) , for each node in the tree Qiuzi parameters \ (B \) for all the nodes \ (a \) the sum of

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 \)

Solution

For \ (30 \% \) data, I would not do it.

For \ (60 \% \) data, consider what enumeration \ (B \) , then \ (O (n-) \) DFS to the tree.

Consider out of practice.

One obvious idea is for a \ (b_i \) , we traverse his subtree parameter \ (b \) and \ (b_i \) different nodes does not make sense, so we consider after completion of traversing a node directly jump within its sub-tree to its nearest \ (B \) the same node, in other words, for each node, it is to find a parameter \ (B \) same as a parent node nearest ancestor.

In consideration dfsof the time, if we each \ (b \) to maintain the root node in the chain if and parameters \ (b \) with the parameters of the parent node node series, the current node is the last node corresponds to the sequence . Consider a sequence of nodes need only maintain this open \ (n-\) th stack maintained separately for each \ (B \) sequence can be, i.e. dfswhen the node to a node onto the stack, pop leave, so that the stack be apparent guaranteed element is an ordered sequence of nodes to the root node of the current chain.

We then equivalent for each \ (b \) have built a tree, these trees do dfscan be. In fact, in the original trees do dfswhen maintenance is good as long as each node can directly count the trees in the new children do not need to explicitly build a new tree.

Temporal complexity \ (O (n-) \) , the desired score \ (100 ~ pts \)

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 thief

Description

It has a length of not less than \ (4 \) sequence \ (A \) , satisfies \ (\ A_ FRAC {{{I} A_i -. 1}} = K \) , where \ (K \) is a positive integer constant maximum value, and the sequence does not exceed \ (n-\) .

Obviously there are so many legitimate sequence, and now we know nothing about the sequence of parameters, only know that there is a legitimate sequence \ (m \) th, seeking \ (n \) minimum is. No solution output -1.

Limitations

\ (100 \% \) \ (3 \ leq m \ leq 10 ^ {15} \)

\ (60 \% \) \ (3 \ leq m \ leq 100000 \)

\ (30 \% \) \ (3 \ leq m \ leq 1000 \)

Solution

For the first \ (30 \% \) data found some burst sequence can be.

For the first \ (60 \% \) data about the enumeration \ (K \) can then sequence length can be enumerated. It is noted that since each sequence is divided by \ (K \) , and therefore the length of the sequence is \ (O (\ log) \ ) level, and the maximum value is probably the sequence \ (O (m) \) level, a maximum value in addition to at least \ (3 \) times \ (k \) result must be a positive number, so \ (k \) will not be great, probably in \ (10 ^ 2 \) orders of magnitude, so too can enumerate. Time complexity \ (O (m \ log m ) \)

As proof of the maximum value is \ (O (m) \) of ...... I was playing table to look out for (fog

For all the data, considering minimization, and obviously not greedy, DP the sequence length of the design can not be uncertain state, consider two points answer.

Consider a fixed \ (n-\) , when the \ (n-\) becomes large, the number of valid sequences is not reduced, because \ (n-\) is small legitimate sequence, when \ (\ n-) After large It is still legal. Could then half the answer.

After half an answer, consider an enumeration Prime Minister \ (k \) , then you can enumerate the sequence length. Also since \ (n-\) is \ (O (m) \) to give \ (K \ Leq 10 ^ \ FRAC. 4} {15} {\ Leq. 4 ^ 10 \) . And since the sequence length is \ (O (\ log m) \) , and so the total number of the complexity of the program is seeking \ (O (\ log 10 m ^ \ FRAC {\ log {m}}. 4) \) .

Therefore, the total time complexity is \ (O (10 ^ {\ FRAC {\ log {m}}}. 4 \ ^ log 2 m) \) .

#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 plates

Description

Given \ (n-\) round, the \ (I \) a radius of a circle \ (r_i \) , there are \ (m \) th pitch \ (1 \) and points in a row, in these points as it requires \ (n-\) th center of the circle, and the circle between mutually disjoint but allows tangent to find the results of the program number modulo large prime number.

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

Before \ (30 \% \) burst search can be. Time complexity \ (C_ {n-m} ^ {} \) .

For \ (60 \% \) data, considering only consider where the center straight line, the problem is converted to a pile of a given segment, seeking the number of disjoint placement program.

Consider a length of \ (1 \) is also regarded as an element of the blank, the problem is converted to a full number of repeating elements are arranged, can be set formulas.

The most noticed on both sides of the line can have up to a half out, consider this enumeration element at both ends, and then enumerate how much they have exposed, violence program count the number of formulas in each case, notice the exposed each inwardly \ (1 \) the length, the number of the program in this case can be \ (O (1) \) maintenance, thus obtaining a \ (O (n ^ 3 + n ^ 2 r) \) of practice, both segments then noted inwardly during movement, the contribution of each number generated in the formula is \ (O (1) \) is calculated, and thus obtain a \ (n O (^ 3 + n ^ 2 \ times poly (\ log )) \) approach, but did not tune out.

Guess you like

Origin www.cnblogs.com/yifusuyi/p/11682102.html