QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#382171#6683. Difficult Constructive Problemkevinyang#AC ✓20ms4580kbC++2010.9kb2024-04-08 04:44:152024-04-08 04:44:15

Judging History

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

  • [2024-04-08 04:44:15]
  • 评测
  • 测评结果:AC
  • 用时:20ms
  • 内存:4580kb
  • [2024-04-08 04:44:15]
  • 提交

answer

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

/* Macros {{{ */
/*  A lot of this is from some of Benq's submissions
    [https://codeforces.com/profile/Benq]
    Ugly af to the eyes, but with vim fold its barable
    Hopefully c++20 concepts can make all this stuff must cleaner */

/* Basics {{{ */
using ll = long long;
using ld = long double;
using str = string;

using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define mp make_pair
#define fi first
#define se second

#define arr array
#define ve vector
using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;

using vpi = vector<pi>;
using vpll = vector<pll>;
using vpld = vector<pld>;

using vvi = vector<vi>;
using vvll = vector<vll>;
using vvld = vector<vld>;

using vvpi = vector<vpi>;
using vvpll = vector<vpll>;
using vvpld = vector<vpld>;

#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz size()
#define rsz(a) resize(a)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

#define For(i, a, b) for (int i = a; i < b; ++i)
#define Rof(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define rep(a) For(_, 0, a)
#define each(a, x) for (auto &a : x)
#define reach(a, x) for (auto a = x.rbegin(); a != x.rend(); ++a)

template <typename T, typename U>
inline void cmin(T &x, U y) {
    if (y < x) x = y;
}
template <typename T, typename U>
inline void cmax(T &x, U y) {
    if (x < y) x = y;
}
/*}}}*/

/* IO {{{ */

/* Template Macros {{{ */
#define tcT template <class T
#define tcTU tcT, class U
#define tcTUU tcT, class... U
/*}}}*/

inline namespace Helpers { /*{{{*/
tcT, class = void > struct is_iterable : false_type {};
tcT > struct is_iterable<
          T, void_t<decltype(begin(declval<T>())), decltype(end(declval<T>()))>>
    : true_type {};
tcT > constexpr bool is_iterable_v = is_iterable<T>::value;

tcT, class = void > struct is_readable : false_type {};
tcT > struct is_readable<T, typename std::enable_if_t<is_same_v<
                                decltype(cin >> declval<T &>()), istream &>>>
    : true_type {};
tcT > constexpr bool is_readable_v = is_readable<T>::value;

tcT, class = void > struct is_printable : false_type {};
tcT > struct is_printable<T, typename std::enable_if_t<is_same_v<
                                 decltype(cout << declval<T>()), ostream &>>>
    : true_type {};
tcT > constexpr bool is_printable_v = is_printable<T>::value;
} /* namespace Helpers */
/*}}}*/

inline namespace Input { /*{{{*/
tcT > constexpr bool needs_input_v = !is_readable_v<T> && is_iterable_v<T>;
tcTUU > void re(T &t, U &...u);
tcTU > void re(pair<T, U> &p); /* pairs */

/* re: read{{{ */
tcT > typename enable_if<is_readable_v<T>, void>::type re(T &x) {
    cin >> x;
} /* default */
tcT > typename enable_if<needs_input_v<T>, void>::type re(
          T &i);                                   // vectors, arrays, etc...
tcTU > void re(pair<T, U> &p) { re(p.fi, p.se); }  // pairs
tcT > typename enable_if<needs_input_v<T>, void>::type re(T &i) {
    each(x, i) re(x);
}
tcTUU > void re(T &t, U &...u) {
    re(t);
    re(u...);
} /* read multiple}}} */

/* rv: resize and read vectors{{{ */
void rv(size_t) {}
tcTUU > void rv(size_t N, ve<T> &t, U &...u);
template <class... U>
void rv(size_t, size_t N2, U &...u);
tcTUU > void rv(size_t N, ve<T> &t, U &...u) {
    t.rsz(N);
    re(t);
    rv(N, u...);
}
template <class... U>
void rv(size_t, size_t N2, U &...u) {
    rv(N2, u...);
} /*}}}*/

/* dumb shortcuts to read in ints{{{ */
void decrement() {} /* subtract one from each */
tcTUU > void decrement(T &t, U &...u) {
    --t;
    decrement(u...);
}
#define ints(...)    \
    int __VA_ARGS__; \
    re(__VA_ARGS__);
#define int1(...)      \
    ints(__VA_ARGS__); \
    decrement(__VA_ARGS__); /*}}}*/
} /* namespace Input */
/*}}}*/

inline namespace ToString { /*{{{*/
tcT > constexpr bool needs_output_v = !is_printable_v<T> && is_iterable_v<T>;

/* ts: string representation to print */
tcT > typename enable_if<is_printable_v<T>, str>::type ts(T v) {
    stringstream ss;
    ss << fixed << setprecision(15) << v;
    return ss.str();
} /* default */
tcT > str bit_vec(T t) { /* bit vector to string */
    str res = "{";
    For(i, 0, t.sz) res += ts(t[i]);
    res += "}";
    return res;
}
str ts(ve<bool> v) { return bit_vec(v); }
template <size_t SZ>
str ts(bitset<SZ> b) {
    return bit_vec(b);
} /* bit vector */
tcTU > str ts(pair<T, U> p); /* pairs */
tcT > typename enable_if<needs_output_v<T>, str>::type ts(
          T v); /* vectors, arrays */
tcTU > str ts(pair<T, U> p) { return "(" + ts(p.fi) + ", " + ts(p.se) + ")"; }
tcT > typename enable_if<is_iterable_v<T>, str>::type ts_sep(T v, str sep) {
    /* convert container to string w/ separator sep */
    bool fst = 1;
    str res = "";
    for (const auto &x : v) {
        if (!fst) res += sep;
        fst = 0;
        res += ts(x);
    }
    return res;
}
tcT > typename enable_if<needs_output_v<T>, str>::type ts(T v) {
    return "{" + ts_sep(v, ", ") + "}";
}

/* for nested DS */
template <int, class T>
typename enable_if<!needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
    return {ts(v)};
}
template <int lev, class T>
typename enable_if<needs_output_v<T>, ve<str>>::type ts_lev(const T &v) {
    if (lev == 0 || !v.sz) return {ts(v)};
    ve<str> res;
    for (const auto &t : v) {
        if (res.sz) res.back() += ",";
        ve<str> tmp = ts_lev<lev - 1>(t);
        res.insert(end(res), all(tmp));
    }
    For(i, 0, res.sz) {
        str bef = " ";
        if (i == 0) bef = "{";
        res[i] = bef + res[i];
    }
    res.back() += "}";
    return res;
}
} /* namespace ToString */
/*}}}*/

inline namespace Output { /*{{{*/
template <class T>
void pr_sep(ostream &os, str, const T &t) {
    os << ts(t);
}
template <class T, class... U>
void pr_sep(ostream &os, str sep, const T &t, const U &...u) {
    pr_sep(os, sep, t);
    os << sep;
    pr_sep(os, sep, u...);
}
/* print w/ no spaces */
template <class... T>
void pr(const T &...t) {
    pr_sep(cout, "", t...);
}
/* print w/ spaces, end with newline */
void ps() { cout << "\n"; }
template <class... T>
void ps(const T &...t) {
    pr_sep(cout, " ", t...);
    ps();
}
/* debug to cerr */
template <class... T>
void dbg_out(const T &...t) {
    pr_sep(cerr, " | ", t...);
    cerr << endl;
}
void loc_info(int line, str names) {
    cerr << "Line(" << line << ") -> [" << names << "]: ";
}
template <int lev, class T>
void dbgl_out(const T &t) {
    cerr << "\n\n" << ts_sep(ts_lev<lev>(t), "\n") << "\n" << endl;
}
} /* namespace Output */
/*}}}}}}}}}*/

int n, k;
str s;

str get_res(str s) {

    int tmp = 0;
    for(int i=1; i<n; ++i) if((s[i] == '0' && s[i-1] == '1') || (s[i] == '1' && s[i-1] == '0')) ++tmp;

    int cnt=0, min_thng=0;

    int lp=0, rp;
    while(lp < s.sz) {
        int rp = lp+1;
        while(rp<s.sz && s[rp] == '?') ++rp;

        int x = rp-lp-1;
        if(x > 0) {
            if(s[lp] == s[rp]) cnt += (x+1)/2*2;
            if(s[lp] != s[rp]) min_thng+=1, cnt += x/2*2 + 1;
        }

        lp = rp;
    }

    if(min_thng > (k-tmp) || cnt < (k-tmp) || ((k-tmp)-cnt)&1) {
        return "";
    }



    int cur_min_possible=min_thng, cur_max_possible=cnt;


    lp=0;
    while(lp < s.sz) {
        int rp = lp+1;
        while(rp<s.sz && s[rp] == '?') ++rp;


        for(int i=lp+1; i<rp; ++i) {

            // before I add a zero, what is the contribution of this interval of ?'s
            int bx = rp-i;
            int bMv = ((s[i-1] == s[rp]) ? (bx+1)/2*2 : bx/2*2 + 1);
            int bmv = (s[i-1] != s[rp]);

            // if i add a 0, how will it change
            int x = rp-i-1;
            int Mv, mv;
            if(x > 0) {
                Mv = (s[i-1] != '0') + (('0' == s[rp]) ? (x+1)/2*2 : x/2*2+1); // this is how the max acheivable value will change
                mv = (s[i-1] != '0') + ('0' != s[rp]); // this is how the min acheviable value will change
            } else {
                Mv = (s[i-1] != '0') + (s[rp] != '0');
                mv = (s[i-1] != '0') + (s[rp] != '0');
            }

            // ps(bMv, Mv);


            int nMp = cur_max_possible - bMv + Mv;
            int nmp = cur_min_possible - bmv + mv;


            if(nmp <= (k-tmp) && (k-tmp) <= nMp) {
                s[i] = '0';
                cur_max_possible = nMp;
                cur_min_possible = nmp;
            } else s[i] = '1';
        }

        lp = rp;
    }

    return s;
}

void solve() {
    re(n, k, s);


    // for(int i=1; i<n-1; ++i) {
    //     if(s[i-1] == '0' && s[i] == '?' && s[i+1] == '1') s[i] = '0';
    //     if(s[i-1] == '1' && s[i] == '?' && s[i+1] == '0') s[i] = '0';
    // }

    ve<str> cands;

    if(s[0] == '?' && s[n-1] == '?') {
        for(int zv=0; zv<2; ++zv) for(int lv=0; lv<2; ++lv) {
            s[0] = '0'+zv, s[n-1] = '0'+lv;
            cands.pb(s);
        }
    } else if (s[0] == '?') {
        for(int zv=0; zv<2; ++zv) {
            s[0] = '0'+zv;
            cands.pb(s);
        }

    } else if (s[n-1] == '?') {
        for(int lv=0; lv<2; ++lv) {
            s[n-1] = '0'+lv;
            cands.pb(s);
        }
    } else cands.pb(s);


    ve<str> opts;

    for(auto s : cands) {
        str tmp = get_res(s);
        if(tmp != "") opts.pb(tmp);
    }

    if(opts.sz) ps(*min_element(all(opts)));
    else ps("Impossible");

    // any 0?1 and 1?0 will always contribute a +1, and thus all of these should be filled in with 0's
    //  any 0?...?1 and 1?...?0 will contribute at least a +1 and at most a +(2*x - (x&1))
    //
    //  any 0?...?0 and 1?...?1 will contribute at least a +0 and at most a +(2*x - (x&1^1))
    //
    //  for 0?...?1, what are the acheviable values
    //
    //      0?0 -> { 0, 2 }
    //      0?1 -> { 1 }
    //      1?0 -> { 1 }
    //      1?1 -> { 0, 2 }
    //      0??0 -> { 0, 2 }
    //      0??1 -> { 1, 3 }
    //      1??0 -> { 1, 3 }
    //
    //  claim:  0?...?0 and 1?...?1 can acheive all and only even values in the range [0, 2(x - (x&1^1))]
    //          0?...?1 and 1?...?0 can acheive all and only odd values in the range [0, 2x - (x&1)]
    //  proof:  the induction is pretty easy
    //
    // for even x, 0?...?0 -> 00?...?0 changes nothing and should be done
    // for odd x, 0?...?1 -> 00?...?1 changes nothing
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    /* cout << fixed << setprecision(6); */
    int t = 1;
    cin >> t;
    for (int i = 0; i < t; i++) solve();

    return 0;
    // you should actually read the stuff at the bottom
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */

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

详细

Test #1:

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

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: 20ms
memory: 4580kb

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