Goodbye Yiwei

A

Greedy take the suffix \ (\ min \) than the bottom of the stack and a small stack has been taken to the suffix \ (\ min \) , or take the top of the stack and the smallest of the bottom of the stack.

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++ i)
#define Rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-8, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
#define SZ(x) ((int)(x).size())

const int MAXN = 100000 + 5;

std::deque<int> dq;
int a[MAXN], suf[MAXN];

int main() {
  int T;
  scanf("%d", &T);
  while (T --) {
    dq.clear();
    int n;
    scanf("%d", &n);
    For(i, 1, n) {
      scanf("%d", &a[i]);
    }
    suf[n + 1] = INF;
    Rep(i, n, 1) {
      suf[i] = mymin(suf[i + 1], a[i]);
    }
    int cur = 1, cnt = 0;
    while (cnt < n) {
      while (dq.empty() || mymin(dq.front(), dq.back()) > suf[cur]) {
        dq.push_back(a[cur ++]);
      }
      while (!dq.empty() && mymin(dq.front(), dq.back()) < suf[cur]) {
        if (dq.front() < dq.back()) {
          printf("%d ", dq.front());
          dq.pop_front();
        } else {
          printf("%d ", dq.back());
          dq.pop_back();
        }
        ++ cnt;
      }
    }
    puts("");
  }
  return 0;
}

B

A point is safe if and only if:

Its degree is 1. \ (1 \) ;
2. it a degree of \ (1 \) connected point;
3. a point of presence, and after this point ignore it even edges of the two adjacent points the same point.

The first two conditions are judged good, the third condition if the two points are not adjacent to row sequence on the line, then enumerated directly adjacent each edge can be determined and violence.

The complexity of the violence is right, because it is equivalent to the equation:

\ [\ Sum ^ m [deg_u = deg_v] deg_u \]

The contribution of each point up \ (O (\ sqrt {m }) \) times, and degrees of all points \ (O (m) \) , so complexity is \ (O (m \ sqrt { m}) \) .

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++ i)
#define Rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-8, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
#define SZ(x) ((int)(x).size())

struct Input {
  char buf[1 << 25], *s;

  Input() {
#ifndef ONLINE_JUDGE
    freopen("police.in", "r", stdin);
#endif
    fread(s = buf, 1, 1 << 25, stdin);
  }

  friend Input &operator>>(Input &io, int &x) {
    x = 0;
    while (!isdigit(*io.s)) {
      ++ io.s;
    }
    while (isdigit(*io.s)) {
      x =  x * 10 + *io.s ++ - '0';
    }
    return io;
  }
} cin;

const int MAXN = 100000 + 5, MAXM = 200000 + 5;

struct Edge {
  int u, v;
} edge[MAXM];

std::vector<int> adj[MAXN];
std::pair<std::vector<int>, int> tp[MAXN];
int deg[MAXN];
bool ok[MAXN];

int main() {
  int T;
  cin >> T;
  while (T --) {
    int n, m;
    cin >> n >> m;
    memset(deg, 0, sizeof deg);
    memset(ok, 0, sizeof ok);
    For(i, 1, n) {
      adj[i].clear();
    }
    For(i, 1, m) {
      cin >> edge[i].u >> edge[i].v;
      ++ deg[edge[i].u], ++ deg[edge[i].v];
      adj[edge[i].u].push_back(edge[i].v);
      adj[edge[i].v].push_back(edge[i].u);
    }
    For(i, 1, n) {
      std::sort(adj[i].begin(), adj[i].end());
      tp[i] = {adj[i], i};
    }
    std::sort(tp + 1, tp + n + 1);
    FOR(i, 1, n) {
      if (tp[i].first == tp[i + 1].first) {
        ok[tp[i].second] = ok[tp[i + 1].second] = 1;
      }
    }
    For(i, 1, m) {
      if (deg[edge[i].u] == 1 || deg[edge[i].v] == 1) {
        ok[edge[i].u] = ok[edge[i].v] = 1;
      }
    }
    For(i, 1, m) {
      if ((!ok[edge[i].u] || !ok[edge[i].v]) && deg[edge[i].u] == deg[edge[i].v]) {
        int u = edge[i].u, v = edge[i].v;
        bool yes = 1;
        for (int x = 0, y = 0; x < SZ(adj[u]) && y < SZ(adj[v]); ++ x, ++ y) {
          if (adj[u][x] == v) {
            ++ x;
          }
          if (adj[v][y] == u) {
            ++ y;
          }
          if (x >= SZ(adj[u]) || y >= SZ(adj[v])) {
            break;
          }
          if (adj[u][x] != adj[v][y]) {
            yes = 0;
            break;
          }
        }
        if (yes) {
          ok[u] = ok[v] = 1;
        }
      }
    }
    int cnt = 0;
    For(i, 1, n) {
      cnt += ok[i];
    }
    printf("%d\n", cnt);
    For(i, 1, n) {
      if (ok[i]) {
        printf("%d ", i);
      }
    }
    puts("");
  } 
  return 0;
}

C

Consider Boruvka algorithm, for each communication round assigned a color block, then the bit is determined by the best edge weights Dalian, you can find the maximum value and the actually required time a large value of a superset of the set (for Analyzing if only one color), and high-dimensional prefix can be.

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++ i)
#define Rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-8, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
#define SZ(x) ((int)(x).size())

const int MAXN = 100000 + 5, MASK = 1 << 18;

struct DisjointSet {
  int fa[MAXN];

  void init(int n) {
    For(i, 1, n) {
      fa[i] = i;
    }
  }

  int find(int u) {
    return fa[u] == u ? u : fa[u] = find(fa[u]);
  }

  bool same(int u, int v) {
    return find(u) == find(v);
  }

  void merge(int u, int v) {
    if (!same(u, v)) {
      fa[fa[v]] = fa[u];
    }
  }
} dsu;

int a[MAXN], col[MAXN], tp[MAXN], to[MAXN], val[MAXN], col_sec[MASK], col_max[MASK], num_sec[MASK], num_max[MASK];

int main() {
#ifndef ONLINE_JUDGE
  freopen("andmst.in", "r", stdin);
#endif
  int n, m;
  scanf("%d%d", &n, &m);
  For(i, 1, n) {
    scanf("%d", &a[i]);
  }
  int cnt = 0, cur = n;
  For(i, 1, n) {
    col[i] = i;
  }
  LL ans = 0;
  while (cnt < n - 1) {
    dsu.init(cur);
    memset(col_sec, 0, sizeof col_sec);
    memset(col_max, 0, sizeof col_max);
    memset(num_sec, 0, sizeof num_sec);
    memset(num_max, 0, sizeof num_max);
    For(i, 1, n) {
      if (col_max[a[i]] < col[i]) {
        col_sec[a[i]] = col_max[a[i]];
        col_max[a[i]] = col[i];
        num_sec[a[i]] = num_max[a[i]];
        num_max[a[i]] = i;
      } else if (col_max[a[i]] != col[i] && col_sec[a[i]] < col[i]) {
        col_sec[a[i]] = col[i];
        num_sec[a[i]] = i;
      }
    }
    FOR(i, 0, m) {
      REP(S, 1 << m, 0) {
        if ((~S >> i) & 1) {
          if (col_max[S] < col_max[S | (1 << i)]) {
            col_sec[S] = col_max[S];
            col_max[S] = col_max[S | (1 << i)];
            num_sec[S] = num_max[S];
            num_max[S] = num_max[S | (1 << i)];
            if (col_sec[S] < col_sec[S | (1 << i)]) {
              col_sec[S] = col_sec[S | (1 << i)];
              num_sec[S] = num_sec[S | (1 << i)];
            }
          } else if (col_max[S] != col_max[S | (1 << i)] && col_sec[S] < col_max[S | (1 << i)]) {
            col_sec[S] = col_max[S | (1 << i)];
            num_sec[S] = num_max[S | (1 << i)];
          } else if (col_max[S] == col_max[S | (1 << i)] && col_sec[S] < col_sec[S | (1 << i)]) {
            col_sec[S] = col_sec[S | (1 << i)];
            num_sec[S] = num_sec[S | (1 << i)];
          }
        }
      }
    }
    For(i, 1, cur) {
      to[i] = 0, val[i] = -1;
    }
    For(i, 1, n) {
      int s = 0;
      REP(j, m, 0) {
        if ((a[i] >> j) & 1) {
          int t = s << 1 | 1, p = t << j;
          if (col_max[p] && (col_max[p] != col[i] || (col_sec[p] && col_sec[p] != col[i]))) {
            s = t;
          } else {
            s = t - 1;
          }
        } else {
          s <<= 1;
        }
      }
      int v = col_max[s] == col[i] ? num_sec[s] : num_max[s], w = a[i] & a[v];
      if (chkmax(val[col[i]], w)) {
        to[col[i]] = col[v];
      }
    }
    For(i, 1, cur) {
      if (!dsu.same(i, to[i])) {
        dsu.merge(i, to[i]);
        ++ cnt;
        ans += val[i];
      }
    }
    int fn = 0;
    For(i, 1, cur) {
      if (dsu.find(i) == i) {
        tp[i] = ++ fn;
      }
    }
    For(i, 1, cur) {
      tp[i] = tp[dsu.find(i)];
    }
    For(i, 1, n) {
      col[i] = tp[col[i]];
    }
    cur = fn;
  }
  printf("%lld\n", ans);
  return 0;
}

D

First-half the answer, then do consider backwards, the merger became divided, then it becomes you have an initial mine, every time you can put a power as \ (k \) Ray becomes \ (m \) th power is \ (\ {k - b_1, k - b_2, \ cdots, k - b_m \} \) mine, and finally let the power of each mine after sorting are greater than or equal to the corresponding position \ (a_i \) .

We call Ray collection \ (S \) to match the set \ (T \) , if and only if \ (S \) may be split by a series of operations such that \ (| S | = | T | \) , and sorting after \ (S \) Ray corresponding position in the power is greater than \ (T \) in.

Every consideration \ (S \) in the largest mine can split out matches (T \) \ the largest mine of mine, or put \ (T \) in the biggest match mine out, you can maintain a multiset .

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++ i)
#define Rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-8, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
#define SZ(x) ((int)(x).size())

const int MAXN = 50000 + 5;

int a[MAXN], b[MAXN];

IL bool check(int n, int m, LL k) {
  std::multiset<LL> s({k}), t(a + 1, a + n + 1);
  while (SZ(s) < SZ(t)) {
    if (s.empty()) {
      return 0;
    }
    LL x = *s.rbegin(), y = *t.rbegin();
    if (x - b[1] >= y) {
      For(i, 1, m) {
        s.insert(x - b[i]);
      }
      s.erase(s.find(x));
    } else {
      auto it = s.lower_bound(y);
      if (it == s.end()) {
        return 0;
      }
      s.erase(it), t.erase(t.find(y));
    }
  }
  for (auto x = s.begin(), y = t.begin(); x != s.end() && y != t.end(); ++ x, ++ y) {
    if (*x < *y) {
      return 0;
    }
  }
  return 1;
}

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  For(i, 1, n) {
    scanf("%d", &a[i]);
  }
  For(i, 1, m) {
    scanf("%d", &b[i]);
  }
  std::sort(a + 1, a + n + 1);
  std::sort(b + 1, b + m + 1);
  LL l = a[n], r = a[n] + (LL)(n - 1) / (m - 1) * b[m], ans = 0;
  while (l <= r) {
    LL mid = (l + r) >> 1;
    if (check(n, m, mid)) {
      ans = mid, r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", ans);
  return 0;
}

E

If a range is found \ ([0,1024] \) then can be used as long \ (10240 \) string transfer, then consider this a hash function \ (1024 \) coordinates are mapped to the range up.

The problem is that the random function if too many times as necessary, consider converting it method, each of these coordinates in half, so that the random number will be accepted.

But the number of such need to send too much, then you can consider small-scale method begins with violence.

One problem is that if you use the number of fixed-length coded and transmitted, then the fixed-length will be relatively large, so you can use variable length coding, specific implementation can be found in the code.

#include <bits/stdc++.h>

#define IL __inline__ __attribute__((always_inline))

#define For(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++ i)
#define FOR(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++ i)
#define Rep(i, a, b) for (int i = (a), i##end = (b); i >= i##end; -- i)
#define REP(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; -- i)

typedef long long LL;

template <class T>
IL bool chkmax(T &a, const T &b) {
  return a < b ? ((a = b), 1) : 0;
}

template <class T>
IL bool chkmin(T &a, const T &b) {
  return a > b ? ((a = b), 1) : 0;
}

template <class T>
IL T mymax(const T &a, const T &b) {
  return a > b ? a : b;
}

template <class T>
IL T mymin(const T &a, const T &b) {
  return a < b ? a : b;
}

template <class T>
IL T myabs(const T &a) {
  return a > 0 ? a : -a;
}

const int INF = 0X3F3F3F3F;
const double EPS = 1E-8, PI = acos(-1.0);

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define OK DEBUG("Passing [%s] in LINE %d...\n", __FUNCTION__, __LINE__)
#define SZ(x) ((int)(x).size())

const int MAXN = 1024, MAXM = 20000 + 5;

IL unsigned xorShift() {
  static unsigned x = 20030102;
  x ^= x << 13;
  x ^= x >> 17;
  x ^= x << 5;
  return x;
}

struct Hash {
  static const int MOD = 998244353;

  int a, b;

  Hash() : a(114514), b(1919810) {}

  void next() {
    a = xorShift() % MOD, b = xorShift() % MOD;
  }

  int get(unsigned x) {
    return ((LL)a * x + b) % MOD;
  }
} h;

namespace Encode {
unsigned key[MAXN];
int val[MAXN], to[MAXN], cur;
bool ret[MAXM];

IL void print(int x, int digit, bool mode) {
  if (mode) {
    int real = x ? std::__lg(x) + 1 : 0;
    For(i, 1, real - digit) {
      ret[cur ++] = 1;
    }
    ret[cur ++] = 0;
  }
  for (; x; x >>= 1, -- digit) {
    ret[cur ++] = x & 1;
  }
  for (; digit > 0; -- digit) {
    ret[cur ++] = 0;
  }
}

void solve(const std::vector<int> &v, int high) {
  static int tp[MAXN];
  int times = 0;
  if (SZ(v) <= 8) {
    while (1) {
      FOR(i, 0, 8) {
        tp[i] = -1;
      }
      FOR(i, 0, 8) {
        tp[h.get(key[v[i]]) % 8] = v[i];
      }
      bool ok = 1;
      FOR(i, 0, 8) {
        if (!~tp[i]) {
          ok = 0;
          break;
        }
      }
      if (ok) {
        break;
      }
      ++ times;
      h.next();
    }
    FOR(i, 0, 8) {
      to[high << 3 | i] = tp[i];
    }
    print(times, 9, 1);
    return;
  }
  int n = SZ(v);
  while (1) {
    FOR(i, 0, n) {
      tp[i] = h.get(key[v[i]]) % 2;
    }
    int cnt = 0;
    FOR(i, 0, n) {
      cnt += tp[i];
    }
    if (cnt == n / 2) {
      break;
    }
    ++ times;
    h.next();
  }
  print(times, 4, 1);
  std::vector<int> x, y;
  FOR(i, 0, n) {
    if (tp[i]) {
      y.push_back(v[i]);
    } else {
      x.push_back(v[i]);
    }
  }
  solve(x, high << 1), solve(y, high << 1 | 1);
}

IL void main() {
  FOR(i, 0, MAXN) {
    scanf("%u%d", &key[i], &val[i]);
  }
  std::vector<int> v(MAXN);
  FOR(i, 0, MAXN) {
    v[i] = i;
  }
  solve(v, 0);
  FOR(i, 0, MAXN) {
    print(val[to[i]], 10, 0);
  }
  FOR(i, 0, cur) {
    putchar('0' + ret[i]);
  }
  puts("");
}
}

namespace Decode {
unsigned val[MAXN];
int to[MAXN], ans[MAXN], cur;
int len;
char str[MAXM];

IL int read(int digit, bool mode) {
  int more = 0;
  if (mode) {
    while (str[cur ++] == '1') {
      ++ more;
    }
  }
  int ans = 0;
  FOR(i, 0, digit + more) {
    ans += (str[cur ++] - '0') << i;
  }
  return ans;
}

void solve(const std::vector<int> &v, int cur, int high) {
  if (cur <= 8) {
    int times = read(9, 1);
    For(i, 1, times) {
      h.next();
    }
    for (auto &x : v) {
      to[high << 3 | (h.get(val[x]) % 8)] = x;
    }
    return;
  }
  int times = read(4, 1);
  For(i, 1, times) {
    h.next();
  }
  std::vector<int> x, y;
  for (auto &a : v) {
    if (h.get(val[a]) % 2) {
      y.push_back(a);
    } else {
      x.push_back(a);
    }
  }
  solve(x, cur >> 1, high << 1), solve(y, cur >> 1, high << 1 | 1);
}

IL void main() {
  memset(to, -1, sizeof to);
  scanf("%s", str);
  len = strlen(str);
  int q;
  scanf("%d", &q);
  std::vector<int> v(q);
  FOR(i, 0, q) {
    scanf("%u", &val[i]);
    v[i] = i;
  }
  solve(v, MAXN, 0);
  FOR(i, 0, MAXN) {
    int x = read(10, 0);
    if (to[i] != -1) {
      ans[to[i]] = x;
    }
  }
  FOR(i, 0, q) {
    printf("%d\n", ans[i]);
  }
}
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("decode.in", "r", stdin);
  freopen("decode.out", "w", stdout);
#endif
  char str[10];
  scanf("%s", str);
  if (str[0] == 'e') {
    Encode::main();
  } else {
    Decode::main();
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/sjkmost/p/12197096.html