QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#446529#8523. Puzzle IIucup-team2818WA 1ms3736kbC++145.2kb2024-06-17 12:28:452024-06-17 12:28:47

Judging History

This is the latest submission verdict.

  • [2024-06-17 12:28:47]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 3736kb
  • [2024-06-17 12:28:45]
  • Submitted

answer

#include <iostream>
#include <algorithm>
#include <vector>

typedef long long ll;
typedef double lf;

// #define DEBUG 1
struct IO
{
    #define MAXSIZE (1 << 20)
    #define isdigit(x) (x >= '0' && x <= '9')
    char buf[MAXSIZE], *p1, *p2;
    char pbuf[MAXSIZE], *pp;
    #if DEBUG
    #else
    IO() : p1(buf), p2(buf), pp(pbuf) {}
    ~IO() {fwrite(pbuf, 1, pp - pbuf, stdout);}
    #endif
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) ? ' ' : *p1++)
    #define blank(x) (x == ' ' || x == '\n' || x == '\r' || x == '\t')

    template <typename T>
    void Read(T &x)
    {
        #if DEBUG
        std::cin >> x;
        #else
        bool sign = 0; char ch = gc(); x = 0;
        for (; !isdigit(ch); ch = gc())
            if (ch == '-') sign = 1;
        for (; isdigit(ch); ch = gc()) x = x * 10 + (ch ^ 48);
        if (sign) x = -x;
        #endif
    }
    void Read(char *s)
    {
        #if DEBUG
        std::cin >> s;
        #else
        char ch = gc();
        for (; blank(ch); ch = gc());
        for (; !blank(ch); ch = gc()) *s++ = ch;
        *s = 0;
        #endif
    }
    void Read(char &c) {for (c = gc(); blank(c); c = gc());}

    void Push(const char &c)
    {
        #if DEBUG
        putchar(c);
        #else
        if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
        *pp++ = c;
        #endif
    }
    template <typename T>
    void Write(T x)
    {
        if (x < 0) x = -x, Push('-');
        static T sta[35];
        int top = 0;
        do sta[top++] = x % 10, x /= 10; while (x);
        while (top) Push(sta[--top] ^ 48);
    }
    template <typename T>
    void Write(T x, char lst) {Write(x), Push(lst);}
} IO;
#define Read(x) IO.Read(x)
#define Write(x, y) IO.Write(x, y)
#define Put(x) IO.Push(x)

using namespace std;

const int MAXN = 3e5 + 10;

int n, m;
char str[MAXN];

vector <pair <int, int>> ans;
bool TYPE;
void Print()
{
    cout << ans.size() << '\n';
    if (!TYPE)
    {
        for (auto x : ans) cout << x.first << ' ' << x.second << '\n';
    }
    else
    {
        bool flag = 0;
        for (auto x : ans)
        {
            x.first = (x.first + m - 1) % n + 1, x.second = (x.second + m - 1) % n + 1;
            if (flag) cout << x.second << ' ' << x.first << '\n';
            else cout << x.first << ' ' << x.second << '\n';
            flag ^= 1;
        }
    }
}

struct Node
{
    bool val;
    int pre, nxt;
}f[MAXN << 1];
int a, b, al, ar, bl, br;
int ap, bp;

void NextA() {ap = (ap == n ? 1 : ap + 1); al = f[al].nxt, ar = f[ar].nxt;}
void PrevA() {ap = (ap == 1 ? n : ap - 1); al = f[al].pre, ar = f[ar].pre;}
void NextB() {bp = (bp == n ? 1 : bp + 1); bl = f[bl].nxt, br = f[br].nxt;}
void PrevB() {bp = (bp == 1 ? n : bp - 1); bl = f[bl].pre, br = f[br].pre;}
inline void Swap()
{
    ans.emplace_back(ap, bp);
    int x = f[al].pre, y = f[bl].pre;
    f[al].pre = y, f[bl].pre = x, f[x].nxt = bl, f[y].nxt = al;
    x = f[ar].nxt, y = f[br].nxt;
    f[ar].nxt = y, f[br].nxt = x, f[x].pre = br, f[y].pre = ar;
    swap(al, bl), swap(ar, br);
}

inline void Solve()
{
    int sa = 0, sb = 0, enda = (ap + n - m - 1) % n + 1, endb = (bp + n - m - 1) % n + 1;
    while (ap != enda && f[al].val == 0) NextA(), sa++;
    while (bp != endb && f[bl].val == 1) NextB(), sb++;
    while (ap != enda && bp != endb)
    {
        Swap();
        while (ap != enda && f[al].val == 0) NextA(), sa++;
        while (bp != endb && f[bl].val == 1) NextB(), sb++;
    }
    if (ap == enda)
    {
        int sum = 0;
        for (int x = al; x != f[ar].nxt; x = f[x].nxt) sum += f[x].val;
        if (sum > sb / 2) Swap();
        for (int i = 1; i <= m; i++) PrevA();
        for (int i = 1; i <= m; i++)
        {
            if (f[f[ar].nxt].val == 0) {NextA(); continue;}
            while (f[bl].val == 1) NextB();
            Swap(), NextA(), Swap();
        }
    }
    else
    {
        int sum = 0;
        for (int x = bl; x != f[br].nxt; x = f[x].nxt) sum += !f[x].val;
        if (sum > sa / 2) Swap();
        for (int i = 1; i <= m; i++) PrevB();
        for (int i = 1; i <= m; i++)
        {
            if (f[f[br].nxt].val == 1) {NextB(); continue;}
            while (f[al].val == 0) NextA();
            Swap(), NextB(), Swap();
        }
    }
}

int main()
{
    // freopen("G.in", "r", stdin);
    // freopen("G.out", "w", stdout);
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    Read(n), Read(m);
    Read(str + 1);
    for (int i = 1; i <= n; i++) f[i] = Node{str[i] == 'B', i - 1, i + 1};
    f[1].pre = n, f[n].nxt = 1, a = 1;
    Read(str + 1);
    for (int i = 1; i <= n; i++) f[i + n] = Node{str[i] == 'B', i - 1 + n, i + 1 + n};
    f[n + 1].pre = 2 * n, f[2 * n].nxt = n + 1, b = n + 1;
    if (m > n / 2) m = n - m, TYPE = 1;
    
    for (int i = 1; i <= n; i++)
        if (f[i].val == 0) {ap = al = i, ar = (i + m - 2) % n + 1; break;}
    for (int i = 1; i <= n; i++)
        if (f[i + n].val == 1) {bp = i, bl = i + n, br = (i + m - 2) % n + 1 + n; break;}
    if (!ap) return Print(), 0;
    Solve();
    Print();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 3
BCCBCC
BBCBBC

output:

2
4 3
5 4

result:

ok moves = 2

Test #2:

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

input:

2 1
BC
BC

output:

1
1 2

result:

ok moves = 1

Test #3:

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

input:

2 1
BB
CC

output:

0

result:

ok moves = 0

Test #4:

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

input:

2 1
CC
BB

output:

0

result:

ok moves = 0

Test #5:

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

input:

3 1
CCC
BBB

output:

0

result:

ok moves = 0

Test #6:

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

input:

3 1
CBC
BCB

output:

1
2 2

result:

ok moves = 1

Test #7:

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

input:

3 2
BBB
CCC

output:

0

result:

ok moves = 0

Test #8:

score: -100
Wrong Answer
time: 0ms
memory: 3660kb

input:

3 2
BCB
BCC

output:

3
1 3
1 1
2 1

result:

wrong answer The final sequences are not correct!