QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#386028#8566. Can We Still Qualify For Semifinals?hos_lyricAC ✓1ms4000kbC++144.8kb2024-04-11 11:12:302024-04-11 11:12:31

Judging History

你现在查看的是最新测评结果

  • [2024-04-11 11:12:31]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:4000kb
  • [2024-04-11 11:12:30]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


template <class Flow> struct MaxFlow {
  // Watch out when using types other than int and long long.
  static constexpr Flow FLOW_EPS = 1e-10L;
  static constexpr Flow FLOW_INF = std::numeric_limits<Flow>::max();
  
  int n, m;
  vector<int> ptr, nxt, zu;
  vector<Flow> capa;

  explicit MaxFlow(int n_) : n(n_), m(0), ptr(n_, -1) {}
  void ae(int u, int v, Flow w0, Flow w1 = 0) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(0 <= w0);
    assert(0 <= w1);
    nxt.push_back(ptr[u]); zu.push_back(v); capa.push_back(w0); ptr[u] = m++;
    nxt.push_back(ptr[v]); zu.push_back(u); capa.push_back(w1); ptr[v] = m++;
  }

  vector<int> see, lev, que;

  Flow augment(int u, int t, Flow limFlow) {
    if (u == t) return limFlow;
    for (int &i = see[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
      const int v = zu[i];
      if (lev[u] < lev[v]) {
        const Flow f = augment(v, t, min(limFlow, capa[i]));
        if (f > FLOW_EPS) { capa[i] -= f; capa[i ^ 1] += f; return f; }
      }
    }
    return 0;
  }
  bool bfs(int s, int t) {
    for (int u = 0; u < n; ++u) { see[u] = ptr[u]; lev[u] = -1; }
    auto head = que.begin(), tail = que.begin();
    for (lev[*tail++ = s] = 0; head != tail; ) {
      const int u = *head++;
      for (int i = ptr[u]; ~i; i = nxt[i]) if (capa[i] > FLOW_EPS) {
        const int v = zu[i];
        if (!~lev[v]) {
          lev[*tail++ = v] = lev[u] + 1;
          if (v == t) return true;
        }
      }
    }
    return false;
  }
  Flow run(int s, int t, Flow limFlow = FLOW_INF) {
    see.resize(n); lev.resize(n); que.resize(n);
    Flow flow = 0;
    for (; flow + FLOW_EPS < limFlow && bfs(s, t); ) {
      for (Flow f; (f = augment(s, t, limFlow - flow)) > FLOW_EPS; flow += f) {}
    }
    return flow;
  }
};

////////////////////////////////////////////////////////////////////////////////


constexpr int N = 10;

int SLen;
char S[110];

bool solve() {
  vector<int> win(N, 0);
  vector<pair<int, int>> ps;
  {
    int pos = 0;
    for (int i = 0; i < N-1; ++i) {
      for (int j = 0; j < N/2; ++j) {
        const int u = (j == 0) ? 0 : (1 + ((j - 1 - i + (N-1)) % (N-1)));
        const int v = 1 + (-j - 1 - i + 2*(N-1)) % (N-1);
// cerr<<i<<" "<<j<<": "<<u<<" "<<v<<endl;
        if (pos < SLen) {
          ++win[(S[pos] == '1') ? u : v];
        } else {
          if (u == 0 || v == 0) {
            ++win[0];
          } else {
            ps.emplace_back(u, v);
          }
        }
        ++pos;
      }
    }
  }
  const int psLen = ps.size();
  for (int p = 0; p < 1 << N; p += 2) if (__builtin_popcount(p) == 3) {
    MaxFlow<int> mf(2 + psLen + N);
    bool ok = true;
    for (int u = 1; u < N; ++u) {
      if (p >> u & 1) {
        mf.ae(2 + psLen + u, 1, psLen);
      } else {
        if (win[0] < win[u]) {
          ok = false;
          break;
        }
        mf.ae(2 + psLen + u, 1, win[0] - win[u]);
      }
    }
    if (!ok) continue;
    for (int i = 0; i < psLen; ++i) {
      mf.ae(0, 2 + i, 1);
      mf.ae(2 + i, 2 + psLen + ps[i].first, 1);
      mf.ae(2 + i, 2 + psLen + ps[i].second, 1);
    }
    const int res = mf.run(0, 1);
    if (res == psLen) {
      return true;
    }
  }
  return false;
}

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d", &SLen);
    scanf("%s", S);
    
    const bool ans = solve();
    puts(ans ? "YES" : "NO");
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3708kb

input:

3
3
111
25
1000010101111111111010100
35
01111011110111101111011110111101111

output:

YES
YES
NO

result:

ok 3 token(s): yes count is 2, no count is 1

Test #2:

score: 0
Accepted
time: 0ms
memory: 3776kb

input:

10
16
0110000001010100
17
01111000110110101
15
001100010101111
16
0010101010011100
19
0000000100010110100
16
0011101010011100
18
011110010001100000
18
000110101001100011
20
01100010000100100100
15
001000111001101

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES

result:

ok 10 token(s): yes count is 10, no count is 0

Test #3:

score: 0
Accepted
time: 1ms
memory: 3964kb

input:

10
37
0110000001010100011101001011100110001
39
000100111101101001100101101000000000100
35
00111000100111100101011010111100100
33
010000010001110010110001101110001
30
000100010100000010010110101010
31
0000101000011010101001010000000
44
00001000000111101011010110000101100011000100
42
01111011110001001...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 0, no count is 10

Test #4:

score: 0
Accepted
time: 1ms
memory: 4000kb

input:

10
23
01100000010101000111010
38
01111001100011000101011110101001101001
27
010000000001001001110001001
26
01101001110011101101000110
8
00001000
22
0110100110001110110001
9
000100010
24
000000100101101010100100
6
011000
29
01101010100101000000000000100

output:

YES
NO
NO
NO
YES
YES
YES
YES
YES
NO

result:

ok 10 token(s): yes count is 6, no count is 4

Test #5:

score: 0
Accepted
time: 1ms
memory: 3712kb

input:

10
30
011000000101010001110100101110
29
01001010010011101110010110010
28
0110000000001000101101001001
23
01101001110011101101000
23
01000001000111001011000
24
011110001000010001010000
23
01001011010101001000011
30
000110011001010010000000000010
24
000110111001110011000011
28
000110001000011011110110...

output:

NO
NO
NO
YES
YES
YES
YES
NO
YES
NO

result:

ok 10 token(s): yes count is 5, no count is 5

Test #6:

score: 0
Accepted
time: 1ms
memory: 3764kb

input:

10
21
011000000101010001110
21
000110110101001010010
22
0111101101001100101101
24
000000001000101011000101
21
011010011100111011010
20
00110000010001101010
21
010010111100010000100
24
010100000100011010110010
23
00001010000110101010010
25
0000000000001000001101110

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
YES

result:

ok 10 token(s): yes count is 10, no count is 0

Test #7:

score: 0
Accepted
time: 1ms
memory: 3968kb

input:

10
26
01100000010101000111010010
26
01101010010100100111011100
26
00110010110100000000010010
27
011100010101110010110101101
30
010100011000001000110101001100
30
011110001000010001010000001001
28
0101100101000010100001101010
26
00101000000000000100000110
28
0110101101010000111000110001
27
00011011110...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 0, no count is 10

Test #8:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

10
25
0010100010011010111001111
26
01001010100010101010001010
26
01111001110000100111011110
26
10001000100110101110011110
26
10101010100110101110011110
27
110100010101010011010111001
27
101010101001101011100111101
31
1000010001010100110001011011110
37
1000101111000100110000011000000100101
40
1000101...

output:

NO
NO
NO
NO
NO
NO
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 0, no count is 10

Test #9:

score: 0
Accepted
time: 1ms
memory: 3700kb

input:

10
26
00001010000000000000000000
26
00000010000000000000000000
26
01101010100010101011011110
26
00011011110111101111011110
27
001100110101011001110111101
27
000110111101111011110111101
28
0110001001000010011101111011
29
01000000010001101000011110111
29
01000000010000101101011110111
30
01000011110111...

output:

YES
YES
YES
YES
YES
YES
YES
YES
YES
NO

result:

ok 10 token(s): yes count is 9, no count is 1

Test #10:

score: 0
Accepted
time: 0ms
memory: 4000kb

input:

10
1
0
2
00
10
0001101110
14
00101010000011
20
00000010010100101010
25
0000000101000100100001111
35
01110011010000101010000010010000100
40
0000100110001110101100001001000110000001
44
01011010110010101110011000010001010011100011
45
010010001001010011110111101011011000000100001

output:

YES
YES
YES
YES
YES
YES
NO
NO
NO
NO

result:

ok 10 token(s): yes count is 6, no count is 4

Extra Test:

score: 0
Extra Test Passed