CodeForces Gym 101745 简要题解

Police Patrol

一定是不断放 1,2,,k 1 , 2 , ⋯ , k 2,3,,k+1 2 , 3 , ⋯ , k + 1 ,这样 k+1 k + 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;
}

int n, m;

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m);
  printf("%d\n", (n / (m + 1) << 1) + min(2, n % (m + 1)));
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Alphabetic Subsequence:

直接暴力枚举映射检验。

#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 = 105;

int n, ans, per[N];
char s[N];

inline void Check() {
  int cur = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] - '0' == per[cur]) {
      ++cur;
      if (cur == 10) {
        ++ans;
      }
    }
  }
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  scanf("%s", s), n = strlen(s);
  for (int i = 0; i < 10; ++i) {
    per[i] = i;
  }
  do {
    Check();
  } while (next_permutation(per, per + 10));
  printf("%d\n", ans);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Infinite Graph Game:

首先如果 x x 出现过 i 次,由等比数列求和公式不难得出 x x 的实际贡献是 vx×2i2i1 ,无解的情况很好判断。

按度数分块,如果一个点度数大于 n n 就认为它是大点否则它是小点,对于每个点记录小点当前的权值和,每次小点修改的时候暴力修改与它相连的点,查询的时候将大点的权值加上。

#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 = 100005;
const int M = 325;
const int mod = 1e9 + 7;

int n, m, k, ans, seq[N], sum[N], val[N], vis[N];
vector <int> adj[N], adv[N];

inline int Qow(int x, int y) {
  int r = 1;
  for (; y; y >>= 1, x = 1LL * x * x % mod) {
    if (y & 1) {
      r = 1LL * r * x % mod;
    }
  }
  return r;
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(m), Read(k);
  for (int i = 1; i <= n; ++i) {
    Read(val[i]);
  }
  for (int i = 1; i <= k; ++i) {
    Read(seq[i]), ++vis[seq[i]];
  }
  for (int i = 1, x, y; i <= m; ++i) {
    Read(x), Read(y), adj[x].pb(y), adj[y].pb(x);
  }
  for (int i = 1; i <= n; ++i) {
    if (vis[i]) {
      int k = Qow(2, vis[i]);
      val[i] = 1LL * val[i] * k % mod * Qow(k - 1, mod - 2) % mod;
    } else {
      if (val[i]) {
        for (auto j : adj[i]) {
          if (vis[j]) {
            puts("-1");
            return 0;
          }
        }
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (adj[i].size() > M) {
      for (auto j : adj[i]) {
        adv[j].pb(i);
      }
    } else {
      for (auto j : adj[i]) {
        sum[j] = (sum[j] + val[i]) % mod;
      }
    }
  }
  for (int i = 1; i <= k; ++i) {
    int x = seq[i];
    ans = (ans + sum[x]) % mod;
    for (auto y : adv[x]) {
      ans = (ans + val[y]) % mod;
    }
    val[x] = 1LL * val[x] * (mod + 1) / 2 % mod;
    if (adj[x].size() <= M) {
      for (auto y : adj[x]) {
        sum[y] = (sum[y] - val[x] + mod) % mod;
      }
    }
  }
  printf("%d\n", ans);
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Stamp Stamp Stamp:

枚举所有子串暴力DP检验,DP可以压位。

#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 = 155;

bitset <N> p[26], f[N];
set <string> ans, vis;
string s, t;
int n, m;

inline bool Check() {
  if (s[0] != t[0]) {
    return false;
  }
  for (int i = 0; i < 26; ++i) {
    p[i].reset();
  }
  for (int i = 0; i < m; ++i) {
    p[t[i] - 'a'][i] = true;
  }
  for (int i = 0; i < n; ++i) {
    f[i].reset();
  }
  f[0][0] = true;
  for (int i = 1; i < n; ++i) {
    f[i] = p[s[i] - 'a'] & (f[i - 1] << 1);
    if (f[i - 1][m - 1]) {
      f[i] = p[s[i] - 'a'];
    }
    if (s[i] == t[0] && f[i - 1].any()) {
      f[i][0] = true;
    }
  }
  return f[n - 1][m - 1];
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  cin >> s, n = s.length();
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; i + j <= n; ++j) {
      t = s.substr(j, i), m = i;
      if (vis.find(t) == vis.end()) {
        if (Check()) {
          ans.insert(t);
        }
        vis.insert(t);
      }
    }
  }
  for (auto t : ans) {
    cout << t << endl;
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Increasing Sequence:

考虑对于每个数修改或者不修改,相邻的数对它的影响,可以得出 k k 的一些不合法区间,扫描线维护即可。

#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 = 100005;
const int M = 1000005;
const int inf = 0x7f7f7f7f;

struct Event {
  int i, p, v;

  bool operator < (const Event &b) const {
    return p < b.p;
  }
} eve[M];

int n, m, ans, cur, a[N], c[N << 1];

inline void Modify(int l, int r, int i) {
  eve[m++] = {i, l, 1}, eve[m++] = {i, r + 1, -1};
}

inline void Modify(int i, int v) {
  cur -= c[i] && c[i ^ 1];
  c[i] += v;
  cur += c[i] && c[i ^ 1];
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), ans = -1;
  for (int i = 0; i < n; ++i) {
    Read(a[i]), Modify(0, a[i] - 1, i << 1);
  }
  for (int i = 1; i < n; ++i) {
    if (a[i] == a[i - 1]) {
      Modify(0, a[i] + a[i - 1], i - 1 << 1 | 1);
      Modify(a[i] + a[i - 1], inf, i << 1 | 1);
      Modify(a[i] + a[i - 1], inf, i - 1 << 1);
      Modify(0, a[i] + a[i - 1], i << 1);
    } else if (a[i - 1] > a[i]) {
      Modify(0, a[i] + a[i - 1], i - 1 << 1 | 1);
      Modify(a[i] + a[i - 1], inf, i << 1 | 1);
    } else {
      Modify(a[i] + a[i - 1], inf, i - 1 << 1);
      Modify(0, a[i] + a[i - 1], i << 1);
    }
  }
  Modify(0, -1, 0);
  sort(eve, eve + m);
  for (int l = 0, r = 0; l < m; l = r) {
    if (eve[l].p >= inf) {
      break;
    }
    for (; r < m && eve[r].p == eve[l].p; ++r);
    for (int i = l; i < r; ++i) {
      Modify(eve[i].i, eve[i].v);
    }
    if (!cur) {
      ans = eve[l].p;
      break;
    }
  }
  printf("%d\n", ans);
  if (~ans) {
    for (int i = 0; i < n; ++i) {
      printf("%d%c", c[i << 1] ? a[i] : ans - a[i], i == n - 1? '\n' : ' ');
    }
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}

Yet Another Binary Matrix:

问题即 maxr(W(A,B+X))+r(W(RA,CBX))

M1={Xr(W(A,X))=|X|} M 1 = { X ∣ r ( W ( A , X ) ) = | X | } M2={Yr(W(RA,CY))=|R||A|} M 2 = { Y ∣ r ( W ( R − A , C − Y ) ) = | R | − | A | }

定义 valx=[xB] v a l x = [ x ∈ B ] ,问题即求最大权拟阵交:

这里给出算法,相关证明可以参见这篇论文

I I 表示答案,初始 I= ,建一个新图 DM1,M2(I) D M 1 , M 2 ( I )

点: source s o u r c e , sink s i n k , I I SI

边:

yx:Iy+xI1 y → x : I − y + x ∈ I 1

xy:Iy+xI2 x → y : I − y + x ∈ I 2

sourcex:xI,I+xI1 s o u r c e → x : x ∉ I , I + x ∈ I 1

xsink:xI,I+xI2 x → s i n k : x ∉ I , I + x ∈ I 2

求出 source s o u r c e sink s i n k 的最长路,让路径上所有点状态取反即可。

#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 = 55;
const int inf = 0x3f3f3f3f;

int n, cnt_a, cnt_b, dis[N], pre[N], mat[N][N];
bool a[N], b[N], ins[N], vis[N];
vector <int> ans, adj[N];
priority_queue <pii> q;
LL fir[N], sec[N];
char s[N];

struct LinearBase {
  LL val[N];

  LinearBase() {
    memset(val, 0, sizeof val);
  }

  inline bool Extend(LL x) {
    for (int i = 49; ~i; --i) {
      if (x >> i & 1) {
        if (val[i]) {
          x ^= val[i];
        } else {
          val[i] = x;
          return true;
        }
      }
    }
    return false;
  }

  inline int Size() {
    int r = 0;
    for (int i = 0; i < 50; ++i) {
      r += val[i] > 0;
    }
    return r;
  }
} tmp;

inline bool CheckX() {
  LinearBase all;
  for (int i = 0; i < n; ++i) {
    if (ins[i]) {
      if (!all.Extend(fir[i])) {
        return false;
      }
    }
  }
  return true;
}

inline bool CheckY() {
  LinearBase all;
  for (int i = 0; i < n; ++i) {
    if (!ins[i]) {
      all.Extend(sec[i]);
    }
  }
  return all.Size() >= n - cnt_a;
}

inline bool FindPath() {
  for (int i = 0; i < n + 2; ++i) {
    adj[i].clear(), dis[i] = inf, vis[i] = false;
  }
  for (int i = 0; i < n; ++i) {
    if (!ins[i]) {
      for (int j = 0; j < n; ++j) {
        if (ins[j]) {
          ins[i] = true, ins[j] = false;
          if (CheckX()) {
            adj[i].pb(j);
          }
          if (CheckY()) {
            adj[j].pb(i);
          }
          ins[i] = false, ins[j] = true;
        }
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!ins[i]) {
      ins[i] = true;
      if (CheckX()) {
        adj[i].pb(n + 1);
      }
      if (CheckY()) {
        adj[n].pb(i);
      }
      ins[i] = false;
    }
  }
  dis[n] = 0, q.push(mp(0, n));
  while (!q.empty()) {
    int x = q.top().Y;
    q.pop();
    if (vis[x]) {
      continue;
    }
    vis[x] = true;
    for (auto y : adj[x]) {
      if (CheckMin(dis[y], dis[x] + (!b[y] ? 0 : ins[y] ? 1 : -1))) {
        q.push(mp(-dis[y], y)), pre[y] = x;
      }
    }
  }
  if (dis[n + 1] == inf) {
    return false;
  }
  for (int i = pre[n + 1]; i != n; ins[i] = !ins[i], i = pre[i]);
  return true;
}

int main() {
#ifdef wxh010910
  freopen("d.in", "r", stdin);
#endif
  Read(n), Read(cnt_a), Read(cnt_b);
  for (int i = 0, x; i < cnt_a; ++i) {
    Read(x), a[x - 1] = true;
  }
  for (int i = 0, x; i < cnt_b; ++i) {
    Read(x), b[x - 1] = true;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%s", s);
    for (int j = 0; j < n; ++j) {
      mat[j][i] = s[j] - '0';
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (a[j]) {
        fir[i] = fir[i] << 1 | mat[i][j];
      } else {
        sec[i] = sec[i] << 1 | mat[i][j];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    tmp.Extend(sec[i]);
  }
  if (tmp.Size() < n - cnt_a) {
    puts("-1");
    return 0;
  }
  while (FindPath());
  for (int i = 0; i < n; ++i) {
    if (b[i] && !ins[i]) {
      puts("-1");
      return 0;
    } else if (!b[i] && ins[i]) {
      ans.pb(i + 1);
    }
  }
  printf("%d\n", ans.size());
  for (int i = 0; i < ans.size(); ++i) {
    printf("%d%c", ans[i], i == ans.size() - 1 ? '\n' : ' ');
  }
  if (ans.empty()) {
    putchar(10);
  }
#ifdef wxh010910
  Debug("My Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值