CodeForces Gym 101806 简要题解

Puyo Puyo

首先把有奇数个东西的列用 [ 1 , 2 ] , [ 2 , 2 ] , [ 2 , 2 ] 消成偶数个东西,然后依次放就行了。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 25;

vector <pair <pii, pii>> ans;
vector <int> a[N];
int n, m, k;

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m), Read(k);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1, x; j <= m; ++j) {
      Read(x);
      if (x) {
        a[j].pb(x);
      }
    }
  }
  for (int i = 1; i <= m; ++i) {
    if (a[i].size() & 1) {
      int c = a[i].back(), d = c == 1 ? 2 : 1;
      ans.pb(mp(mp(1, i), mp(d, c)));
      ans.pb(mp(mp(1, i), mp(d, d)));
      ans.pb(mp(mp(1, i), mp(d, d)));
      a[i].pop_back();
    }
    reverse(a[i].begin(), a[i].end());
  }
  for (int i = 1; i <= m; ++i) {
    for (int j = 0; j < a[i].size(); j += 2) {
      ans.pb(mp(mp(1, i), mp(a[i][j + 1], a[i][j])));
    }
  }
  printf("%d\n", ans.size());
  for (auto p : ans) {
    printf("%d %d %d %d\n", p.X.X, p.X.Y, p.Y.X, p.Y.Y);
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

QueryreuQ

对于每个串跑Manacher即可。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 20005;

int n, m, a[N], f[N];
char s[N], t[N];

inline int Manacher() {
  for (int i = 1; i <= m; ++i) {
    a[i << 1] = t[i], a[i << 1 | 1] = -1;
  }
  a[0] = -2, a[1] = -1, a[m + 1 << 1] = -3;
  int ans = 0;
  for (int i = 2, r = 0, p = 0; i < m + 1 << 1; ++i) {
    f[i] = r > i ? min(r - i, f[(p << 1) - i]) : 1;
    for (; a[i - f[i]] == a[i + f[i]]; ++f[i]);
    if (CheckMax(r, i + f[i])) {
      p = i;
    }
    ans += f[i] >> 1;
  }
  return ans;
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), scanf("%s", s + 1);
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '-') {
      --m;
    } else {
      t[++m] = s[i];
    }
    printf("%d%c", Manacher(), i == n ? '\n' : ' ');
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Recipe

考虑暴力DP, f i = max ( f j + ( F j + 1 + j + 1 ) × C i ) i C i ( F j + 1 + j + 1 L i + i ) ,分治斜率优化,查询直接在凸包上二分。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 250005;
const LL inf = 1LL << 60;

vector <pair <int, LL>> s;
int n, a[N], b[N], c[N];
LL f[N];

inline bool Check(pair <int, LL> a, pair <int, LL> b, pair <int, LL> c) {
  b.X -= a.X, b.Y -= a.Y, c.X -= a.X, c.Y -= a.Y;
  return c.X * b.Y - c.Y * b.X <= 0;
}

inline void Extend(pair <int, LL> p) {
  if (p.Y == -inf) {
    return ;
  }
  if (!s.empty() && s.back().X == p.X) {
    if (s.back().Y >= p.Y) {
      return ;
    } else {
      s.pop_back();
    }
  }
  for (; s.size() > 1 && Check(p, s[s.size() - 1], s[s.size() - 2]); s.pop_back());
  s.pb(p);
}

inline LL Query(int k) {
  if (s.empty()) {
    return -inf;
  }
  int l = 0, r = s.size() - 2, ret = s.size() - 1;
  while (l <= r) {
    int mid = l + r >> 1;
    if (1LL * k * s[mid].X + s[mid].Y > 1LL * k * s[mid + 1].X + s[mid + 1].Y) {
      ret = mid, r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  return 1LL * k * s[ret].X + s[ret].Y;
}

inline void Solve(int l, int r) {
  if (l == r) {
    if (f[l] != -inf) {
      f[l] -= 1LL * b[l] * l;
    }
    return ;
  }
  int mid = l + r >> 1;
  Solve(l, mid);
  vector <pii> L, R;
  for (int i = l; i <= mid; ++i) {
    L.pb(mp(a[i + 1], i));
  }
  for (int i = mid + 1; i <= r; ++i) {
    R.pb(mp(c[i], i));
  }
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());
  s.clear();
  for (int i = R.size() - 1, j = L.size() - 1; ~i; --i) {
    for (; ~j && L[j].X >= R[i].X; Extend(mp(L[j].X, f[L[j].Y])), --j);
    CheckMax(f[R[i].Y], Query(b[R[i].Y]));
  }
  Solve(mid + 1, r);
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(a[i]), a[i] += i;
  }
  for (int i = 1; i <= n; ++i) {
    Read(b[i]);
  }
  for (int i = 1; i <= n; ++i) {
    Read(c[i]), c[i] += i;
  }
  for (int i = 1; i <= n; ++i) {
    f[i] = -inf;
  }
  Solve(0, n);
  if (f[n] == -inf) {
    puts("Impossible");
  } else {
    printf("%lld\n", f[n]);
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Segmentation

模拟。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const string ans[5][5] = {
{"New Customer", "Promising", "About to Sleep", "Lost", "Lost"},
{"Potential Loyalist", "Potential Loyalist", "About to Sleep", "Hibernating", "Lost"},
{"Potential Loyalist", "Potential Loyalist", "Need Attention", "About to Leave", "About to Leave"},
{"Loyal Customer", "Loyal Customer", "Loyal Customer", "About to Leave", "About to Leave"},
{"Champion", "Loyal Customer", "Loyal Customer", "About to Leave", "Can't Lose Them"}
};

map <string, int> cnt, lst;
int n, a[4], b[4];
string s;

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  for (int i = 0; i < 4; ++i) {
    Read(a[i]);
  }
  for (int i = 0; i < 4; ++i) {
    Read(b[i]);
  }
  Read(n);
  for (int i = 1, o; i <= n; ++i) {
    Read(o);
    cin >> s;
    if (o == 1) {
      ++cnt[s], lst[s] = i;
    } else {
      if (cnt.find(s) == cnt.end()) {
        puts("None");
      } else {
        int x = 0, y = 0;
        for (int j = 0; j < 4; ++j) {
          if (i - lst[s] > a[j]) {
            ++x;
          }
          if (cnt[s] > b[j]) {
            ++y;
          }
        }
        cout << ans[y][x] << endl;
      }
    }
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Touch The Sky

首先最终顺序一定是按照 L i + D i 排序。维护一个堆,贪心删除之前最大的保证当前高度不超过 L i + D i 即可。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 250005;

priority_queue <int> q;
pair <LL, int> a[N];
int n;
LL s;

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n);
  for (int i = 1; i <= n; ++i) {
    Read(a[i].X), Read(a[i].Y), a[i].X += a[i].Y;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    s += a[i].Y, q.push(a[i].Y);
    if (s > a[i].X) {
      s -= q.top(), q.pop();
    }
  }
  printf("%d\n", q.size());
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

United States of Eurasia

二分答案,对每一层内部先倍增再二分,检验是查最远点,旋转卡壳即可。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 500005;

int n, m, tot, x[N], yl[N], yr[N];
pii a[N];

namespace Far {
struct Point {
  int x, y;

  Point(int x = 0, int y = 0):x(x), y(y) {}
} p[N], q[N];

LL ans;
int n;

inline LL Cross(Point a, Point b, Point o) {
  return 1LL * (a.x - o.x) * (b.y - o.y) - 1LL * (b.x - o.x) * (a.y - o.y);
}

inline void ConvexHull() {
  q[0] = p[0], q[1] = p[1];
  int top = 1;
  for (int i = 2; i < n; ++i) {
    for (; top && Cross(q[top], p[i], q[top - 1]) <= 0; --top);
    q[++top] = p[i];
  }
  int tmp = top;
  for (int i = n - 2; ~i; --i) {
    for (; top > tmp && Cross(q[top], p[i], q[top - 1]) <= 0; --top);
    q[++top] = p[i];
  }
  n = top;
}

inline LL Dis(Point a, Point b) {
  return 1LL * (a.x - b.x) * (a.x - b.x) + 1LL * (a.y - b.y) * (a.y - b.y);  
}

inline void Solve() {
  q[n] = q[0];
  for (int i = 0, j = 1; i < n; ++i) {
    for (; Cross(q[i + 1], q[j + 1], q[i]) > Cross(q[i + 1], q[j], q[i]); j = (j + 1) % n);
    CheckMax(ans, Dis(q[i], q[j]));
    CheckMax(ans, Dis(q[i + 1], q[j + 1]));
  }
}
}

inline bool Check(int l, int r, LL mid) {
  Far::n = Far::ans = 0;
  for (int i = l; i <= r; ++i) {
    Far::p[Far::n++] = Far::Point(x[i], yl[i]);
    if (yl[i] != yr[i]) {
      Far::p[Far::n++] = Far::Point(x[i], yr[i]);
    }
  }
  if (Far::n <= 1) {
    return true;
  }
  Far::ConvexHull();
  Far::Solve();
  return Far::ans <= mid;
}

inline bool Check(LL mid) {
  for (int i = 1, tot = 0, len; i <= n; ++i) {
    if (tot == m) {
      return false;
    }
    for (len = 1; ; len <<= 1) {
      if (!Check(i, min(i + len - 1, n), mid)) {
        break;
      } else if (i + len - 1 >= n) {
        return true;
      }
    }
    len >>= 1;
    int l = i + len, r = min(i + (len << 1) - 1, n), ret = l - 1;
    while (l <= r) {
      int d = l + r >> 1;
      if (Check(i, d, mid)) {
        ret = d, l = d + 1;
      } else {
        r = d - 1;
      }
    }
    ++tot, i = ret;
  }
  return true;
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m);
  for (int i = 1; i <= n; ++i) {
    Read(a[i].X), Read(a[i].Y);
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    if (i == 1 || a[i].X != a[i - 1].X) {
      ++tot, x[tot] = a[i].X, yl[tot] = yr[tot] = a[i].Y;
    } else {
      yr[tot] = a[i].Y;
    }
  }
  n = tot;
  LL l = 0, r = 2000000000000000000, ret = r + 1;
  while (l <= r) {
    LL mid = l + r >> 1;
    if (Check(mid)) {
      ret = mid, r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%lld\n", ret);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Voronoi Diagram

求出多源最短路,对每条边单独计算贡献。注意重边。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 250005;
const LL inf = 1LL << 60;

priority_queue <pair <LL, int>> q;
int n, m, k, s[N], pre[N];
vector <pii> adj[N];
LL ans[N], dis[N];
bool vis[N];

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m);
  for (int i = 1, x, y, w; i <= m; ++i) {
    Read(x), Read(y), Read(w);
    adj[x].pb(mp(y, w));
    if (x != y) {
      adj[y].pb(mp(x, w));
    }
  }
  for (int i = 1; i <= n; ++i) {
    dis[i] = inf;
  }
  Read(k);
  for (int i = 1; i <= k; ++i) {
    Read(s[i]);
    dis[s[i]] = 0, pre[s[i]] = s[i];
    q.push(mp(0, -s[i]));
  }
  while (!q.empty()) {
    int x = -q.top().Y;
    q.pop();
    if (vis[x]) {
      continue;
    }
    vis[x] = true;
    for (auto e : adj[x]) {
      if (CheckMin(dis[e.X], dis[x] + e.Y)) {
        pre[e.X] = pre[x], q.push(mp(-dis[e.X], -e.X));
      } else if (dis[e.X] == dis[x] + e.Y) {
        CheckMin(pre[e.X], pre[x]);
      }
    }
  }
  for (int x = 1; x <= n; ++x) {
    for (auto e : adj[x]) {
      int y = e.X, w = e.Y;
      if (x > y) {
        continue;
      }
      if (dis[x] >= dis[y] + w) {
        ans[pre[y]] += w << 1;
      } else if (dis[y] >= dis[x] + w) {
        ans[pre[x]] += w << 1;
      } else {
        ans[pre[x]] += dis[y] - dis[x] + w;
        ans[pre[y]] += dis[x] - dis[y] + w;
      }
    }
  }
  for (int i = 1; i <= k; ++i) {
    printf("%lld.%d\n", ans[s[i]] >> 1, ans[s[i]] & 1 ? 5 : 0);
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Winter Olympic Games

前面连续的 1 应该贪心保留,剩下要求一个最大后缀,后缀数组即可。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 1000005;

int n, a[N], s[N], arr[N], rnk[N];
char t[N];

inline void Build() {
  static int a[N], b[N], t[N], cnt_a[N], cnt_b[N];
  for (int i = 1; i <= n; ++i) {
    ++cnt_a[s[i]];
  }
  for (int i = 1; i < 26; ++i) {
    cnt_a[i] += cnt_a[i - 1];
  }
  for (int i = n; i; --i) {
    arr[cnt_a[s[i]]--] = i;
  }
  rnk[arr[1]] = 1;
  for (int i = 2; i <= n; ++i) {
    rnk[arr[i]] = rnk[arr[i - 1]] + (s[arr[i]] != s[arr[i - 1]]);
  }
  for (int l = 1; rnk[arr[n]] < n; l <<= 1) {
    for (int i = 0; i <= n; ++i) {
      cnt_a[i] = cnt_b[i] = 0;
    }
    for (int i = 1; i <= n; ++i) {
      ++cnt_a[a[i] = rnk[i]], ++cnt_b[b[i] = i + l > n ? 0 : rnk[i + l]];
    }
    for (int i = 1; i <= n; ++i) {
      cnt_a[i] += cnt_a[i - 1], cnt_b[i] += cnt_b[i - 1];
    }
    for (int i = n; i; --i) {
      t[cnt_b[b[i]]--] = i;
    }
    for (int i = n; i; --i) {
      arr[cnt_a[a[t[i]]]--] = t[i];
    }
    rnk[arr[1]] = 1;
    for (int i = 2; i <= n; ++i) {
      rnk[arr[i]] = rnk[arr[i - 1]] + (a[arr[i]] != a[arr[i - 1]] || b[arr[i]] != b[arr[i - 1]]);
    }
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), scanf("%s", t + 1);
  int sta = 1;
  while (sta <= n && t[sta] == '1') {
    ++sta;
  }
  if (sta > n) {
    puts("0 0");
    return 0;
  }
  printf("%d ", sta - 1);
  for (int i = sta; i <= n; ++i) {
    s[i - sta + 1] = t[i] - '0';
  }
  n = n - sta + 1;
  Build();
  printf("%d\n", arr[n] - 1);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Xtreme NP-hard Problem?!

如果 k n 或者 k > m 答案是 1 ,不然 k 5

补虚点让 k = 5 ,考虑一条路径 1 x y u v n ,枚举 y u ,剩下的只用记录 1 n i 的前三短的经过两条边的路径即可。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int N = 1000005;
const int inf = 1 << 29;

pii f[N][3], g[N][3];
vector <pii> adj[N];
int n, m, k, ans;

inline void Build(int x, pii f[N][3]) {
  for (int i = 1; i <= n; ++i) {
    f[i][0] = f[i][1] = f[i][2] = mp(inf, 0);
  }
  for (auto p : adj[x]) {
    int y = p.X;
    if (y != 1 && y != n) {
      for (auto q : adj[y]) {
        int z = q.X;
        if (z != 1 && z != y) {
          pii w = mp(p.Y + q.Y, y);
          if (w < f[z][0]) {
            f[z][2] = f[z][1], f[z][1] = f[z][0], f[z][0] = w;
          } else if (w < f[z][1]) {
            f[z][2] = f[z][1], f[z][1] = w;
          } else {
            f[z][2] = w;
          }
        }
      }
    }
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m), Read(k), ans = inf;
  if (k >= n || k > m) {
    puts("-1");
    return 0;
  }
  for (int i = 1, x, y, w; i <= m; ++i) {
    Read(x), Read(y), Read(w);
    adj[x].pb(mp(y, w)), adj[y].pb(mp(x, w));
  }
  for (int i = 1; i <= 5 - k; ++i) {
    adj[n].pb(mp(n + 1, 0)), adj[n + 1].pb(mp(n, 0)), ++n;
  }
  Build(1, f), Build(n, g);
  for (int x = 2; x < n; ++x) {
    for (auto e : adj[x]) {
      int y = e.X;
      if (y != 1 && y != n) {
        int i = 0, j = 0;
        if (f[x][i].Y == y) {
          ++i;
        }
        if (g[y][j].Y == x) {
          ++j;
        }
        if (f[x][i].Y != g[y][j].Y) {
          CheckMin(ans, e.Y + f[x][i].X + g[y][j].X);
        } else {
          int k = i + 1, l = j + 1;
          if (f[x][k].Y == y) {
            ++k;
          }
          if (g[y][l].Y == x) {
            ++l;
          }
          CheckMin(ans, e.Y + min(f[x][i].X + g[y][l].X, f[x][k].X + g[y][j].X));
        }
      }
    }
  }
  if (ans == inf) {
    ans = -1;
  }
  printf("%d\n", ans);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Yut Nori

模拟。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

const int nxt[30][5] = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5, 6},
{3, 4, 5, 6, 7},
{4, 5, 6, 7, 8},
{5, 6, 7, 8, 9},
{20, 21, 22, 23, 24},
{7, 8, 9, 10, 11},
{8, 9, 10, 11, 12},
{9, 10, 11, 12, 13},
{10, 11, 12, 13, 14},
{25, 26, 22, 27, 28},
{12, 13, 14, 15, 16},
{13, 14, 15, 16, 17},
{14, 15, 16, 17, 18},
{15, 16, 17, 18, 19},
{16, 17, 18, 19, 29},
{17, 18, 19, 29, 0},
{18, 19, 29, 0, 0},
{19, 29, 0, 0, 0},
{29, 0, 0, 0, 0},
{21, 22, 23, 24, 15},
{22, 23, 24, 15, 16},
{27, 28, 29, 0, 0},
{24, 15, 16, 17, 18},
{15, 16, 17, 18, 19},
{26, 22, 27, 28, 29},
{22, 27, 28, 29, 0},
{28, 29, 0, 0, 0},
{29, 0, 0, 0, 0},
{0, 0, 0, 0, 0}
};

const int pos[30][2] = {
{30, 30},
{24, 30},
{18, 30},
{12, 30},
{6, 30},
{0, 30},
{0, 24},
{0, 18},
{0, 12},
{0, 6},
{0, 0},
{6, 0},
{12, 0},
{18, 0},
{24, 0},
{30, 0},
{30, 6},
{30, 12},
{30, 18},
{30, 24},
{5, 25},
{10, 20},
{15, 15},
{20, 10},
{25, 5},
{5, 5},
{10, 10},
{20, 20},
{25, 25},
{30, 30}
};

string ans[32] = {
"..----..----..----..----..----..",
"..    ..    ..    ..    ..    ..",
"| \\                          / |",
"|  \\                        /  |",
"|   \\                      /   |",
"|    ..                  ..    |",
"..   ..                  ..   ..",
"..     \\                /     ..",
"|       \\              /       |",
"|        \\            /        |",
"|         ..        ..         |",
"|         ..        ..         |",
"..          \\      /          ..",
"..           \\    /           ..",
"|             \\  /             |",
"|              ..              |",
"|              ..              |",
"|             /  \\             |",
"..           /    \\           ..",
"..          /      \\          ..",
"|         ..        ..         |",
"|         ..        ..         |",
"|        /            \\        |",
"|       /              \\       |",
"..     /                \\     ..",
"..   ..                  ..   ..",
"|    ..                  ..    |",
"|   /                      \\   |",
"|  /                        \\  |",
"| /                          \\ |",
"..    ..    ..    ..    ..    ..",
"..----..----..----..----..----.."
};

int n, p[8];
char s[10];

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  for (Read(n); n; --n) {
    scanf("%s", s);
    int ind;
    if (s[0] >= 'a' && s[0] <= 'z') {
      ind = s[0] - 'a';
    } else {
      ind = s[0] - 'A' + 4;
    }
    scanf("%s", s);
    int cnt = 0;
    for (int i = 0; i < 4; ++i) {
      if (s[i] == 'F') {
        ++cnt;
      }
    }
    if (!cnt) {
      cnt = 4;
    } else {
      --cnt;
    }
    int a = p[ind], b = nxt[a][cnt];
    if (ind < 4) {
      for (int i = 4; i < 8; ++i) {
        if (p[i] == b) {
          p[i] = 0;
        }
      }
      p[ind] = b;
      if (a) {
        for (int i = 0; i < 4; ++i) {
          if (p[i] == a) {
            p[i] = b;
          }
        }
      }
    } else {
      for (int i = 0; i < 4; ++i) {
        if (p[i] == b) {
          p[i] = 0;
        }
      }
      p[ind] = b;
      if (a) {
        for (int i = 4; i < 8; ++i) {
          if (p[i] == a) {
            p[i] = b;
          }
        }
      }
    }
  }
  for (int i = 0; i < 8; ++i) {
    if (p[i]) {
      int x = pos[p[i]][0], y = pos[p[i]][1];
      if (i & 2) {
        ++x;
      }
      if (i & 1) {
        ++y;
      }
      ans[x][y] = i < 4 ? i + 'a' : i - 4 + 'A';
    }
  }
  for (int i = 0; i < 32; ++i) {
    cout << ans[i] << endl;
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Zigzag

模拟。

#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define Debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef long double LD;
typedef unsigned int uint;
typedef pair <int, int> pii;
typedef unsigned long long uLL;

template <typename T> inline void Read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}

template <typename T> inline bool CheckMax(T &a, const T &b) {
  return a < b ? a = b, true : false;
}

template <typename T> inline bool CheckMin(T &a, const T &b) {
  return a > b ? a = b, true : false;
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  int n, ans = 2;
  Read(n);
  vector <int> a(n);
  for (int i = 0; i < n; ++i) {
    Read(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + 2; j < n; ++j) {
      if (!(a[j - 2] <= a[j - 1] && a[j - 1] <= a[j]) && !(a[j - 2] >= a[j - 1] && a[j - 1] >= a[j])) {
        CheckMax(ans, j - i + 1);
      } else {
        break;
      }
    }
  }
  printf("%d\n", ans);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

猜你喜欢

转载自blog.csdn.net/wxh010910/article/details/80464255