QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125650#6683. Difficult Constructive ProblemUNos_maricones#AC ✓77ms20692kbC++202.7kb2023-07-17 07:38:562023-07-17 07:38:57

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-17 07:38:57]
  • Judged
  • Verdict: AC
  • Time: 77ms
  • Memory: 20692kb
  • [2023-07-17 07:38:56]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std ;

#define ff    first
#define ss    second
#define pb    push_back

typedef long long    ll;
typedef pair<ll,ll>    ii;
typedef long double    lf;


mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll SQ = 320;
const ll N = 6e5+5;
const ll mod = 998244353;
const ll oo = 1e9;

int main(){
  #ifdef LOCAL
  freopen("input.txt","r",stdin);
  #endif // LOCAL
  ios_base::sync_with_stdio(0);
  cin.tie(0);cout.tie(0);

  int t; cin >> t;
  while (t--) {
    int n, k; cin >> n >> k;
    string s; cin >> s;

    vector <vector<ii>> dp(n + 1, vector<ii>(3));
    vector <vector<ii>> dp2(n + 1, vector<ii>(3));
    dp[n][0] = dp[n][1] = dp[n][2] = {-oo, 0};
    dp2[n][0] = dp2[n][1] = dp2[n][2] = {oo, 0};
    for (int i = n-1; i >= 0; --i) {
      for (int j = 0; j < 3; ++j) {
        dp[i][j] = {-oo, -oo};
        dp2[i][j] = {oo,oo};
        int l = 0, r = 0;
        if (s[i] == '1') l = 1, r = 1;
        else if (s[i] == '?') l = 0, r = 1;
        for (int h = l; h <= r; ++h) {
          int cost = (j < 2 && h != j);
          if (cost) {
            dp[i][j].ff = max(dp[i][j].ff, dp[i + 1][h].ss + 1);
            dp[i][j].ss = max(dp[i][j].ss, dp[i + 1][h].ff + 1);
            dp2[i][j].ff = min(dp2[i][j].ff, dp2[i + 1][h].ss + 1);
            dp2[i][j].ss = min(dp2[i][j].ss, dp2[i + 1][h].ff + 1);
          }
          else {
            dp[i][j].ff = max(dp[i][j].ff, dp[i + 1][h].ff);
            dp[i][j].ss = max(dp[i][j].ss, dp[i + 1][h].ss);
            dp2[i][j].ff = min(dp2[i][j].ff, dp2[i + 1][h].ff);
            dp2[i][j].ss = min(dp2[i][j].ss, dp2[i + 1][h].ss);
          }
        }
      }
    }

    if ((k % 2 && dp[0][2].ff < k) || (k % 2 == 0 && dp[0][2].ss < k)) {
      cout << "Impossible\n";
      continue;
    }
    if ((k % 2 && dp2[0][2].ff > k) || (k % 2 == 0 && dp2[0][2].ss > k)) {
      cout << "Impossible\n";
      continue;
    }
    int ls = -1;
    for (int i = 0; i < n; ++i) {
      if (s[i] == '?') {
        int cost = (ls == 1);
        int nk = k - cost;
//        cout << nk << ' ' << dp[i + 1][0].ff << ' ' << dp[i + 1][0].ss << '\n';
        if (nk >= 0 && ((nk % 2 && dp[i + 1][0].ff >= nk) || (nk % 2 == 0 && dp[i + 1][0].ss >= nk)) && ((nk % 2 && dp2[i + 1][0].ff <= nk) || (nk % 2 == 0 && dp2[i + 1][0].ss <= nk))) {
          cout << 0;
          ls = 0;
          k = nk;
        }
        else {
          cout << 1;
          k -= (ls == 0);
          ls = 1;
        }
      }
      else {
        cout << s[i];
        if (s[i] - '0' != ls && ls != -1) k--;
        ls = s[i] - '0';
      }
    }
    cout << '\n';
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3428kb

input:

5
9 6
1?010??01
9 5
1?010??01
9 6
100101101
9 5
100101101
9 3
????????1

output:

100100101
Impossible
100101101
Impossible
000000101

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 77ms
memory: 20692kb

input:

6110
2 0
01
9 5
????110??
18 8
???111???00??010??
11 8
001??01???0
2 0
00
8 1
?????111
11 2
110???01???
13 7
??100???01??1
6 2
?????0
18 8
110??11??011??110?
12 5
00??011??00?
20 10
??01???0011???0010??
1 0
?
13 6
???10??110??0
6 2
00???1
17 10
??010??001???11??
5 2
????0
14 7
010???00???11?
2 1
01
...

output:

Impossible
001011001
000111000000101010
00101010010
00
00000111
11000001111
0010000101001
000010
110001100011001101
000001101001
00010000011001001010
0
0001000110010
Impossible
00010010010101100
00010
01000100010111
01
0100100001
Impossible
001100010100100101
00111111000
000
01111001
001000000100010...

result:

ok 6110 lines