QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#295236#6338. ChorusMax_s_xaM0 2ms11844kbC++143.4kb2023-12-30 21:27:462023-12-30 21:27:47

Judging History

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

  • [2023-12-30 21:27:47]
  • 评测
  • 测评结果:0
  • 用时:2ms
  • 内存:11844kb
  • [2023-12-30 21:27:46]
  • 提交

answer

#include <iostream>
#include <algorithm>

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 = 1e6 + 10;

int n, m, a[MAXN];
char s[MAXN];

ll d[MAXN], f[MAXN], preans;
int g[MAXN], st[MAXN], head, tail;
inline ll Y(int i, int j) {return f[i] - d[i + 1] - i - f[j] + d[j + 1] + j;}
inline ll X(int i, int j) {return i - j;}
inline bool cmp(int i, int j, int k)
{
    ll x = Y(i, j) * X(j, k) - Y(j, k) * X(i, j);
    return (x > 0 || (x == 0 && g[j] >= g[k]));
}
inline void Check(ll val)
{
    if (preans == val) return;
    preans = val;
    st[head = tail = 1] = 0, f[0] = g[0] = 0;
    if (a[1] > 1) st[++tail] = a[1] - 1, f[a[1] - 1] = -val, g[a[1] - 1] = 1;
    for (int i = a[1], p = 1; i <= n; i++)
    {
        while (p <= i && a[p] < i + 1) p++;
        while (head < tail && Y(st[head], st[head + 1]) > X(st[head], st[head + 1]) * i) head++;
        f[i] = f[st[head]] + (ll)(i + 1) * (p - st[head] - 1) - d[st[head] + 1] + d[p] - val, g[i] = g[st[head]] + 1;
        while (head < tail && cmp(st[tail - 1], st[tail], i)) tail--;
        st[++tail] = i;
    }
}

int main()
{
    #if DEBUG
    #else
    ios::sync_with_stdio(0), cin.tie(0);
    #endif
    Read(n), Read(m), Read(s + 1);
    int nn = 0;
    for (int i = 1; i <= n * 2; i++)
        if (s[i] == 'B') nn++, a[nn] = i + 1 - nn;
    for (int i = n; i >= 1; i--) d[i] = d[i + 1] + a[i];
    ll l = -20, r = 0, mid, ans = 0;
    while (l <= r)
    {
        mid = (l + r) / 2, Check(mid);
        if (g[n] == m) {ans = mid; break;}
        if (g[n] > m) r = mid - 1;
        else ans = mid, l = mid + 1;
    }
    Check(ans);
    Write(f[n] + ans * m, '\n');
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 16
Accepted
time: 2ms
memory: 11844kb

input:

1 1
BA

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

7 5
ABBAAABBABABBA

output:

3

result:

ok 1 number(s): "3"

Test #3:

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

input:

10 3
BABBABAABAABBABBBAAA

output:

26

result:

ok 1 number(s): "26"

Test #4:

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

input:

10 2
AAABBABABBAAABBBAABB

output:

11

result:

ok 1 number(s): "11"

Test #5:

score: -16
Wrong Answer
time: 0ms
memory: 11836kb

input:

10 1
BBBBBBBBBBAAAAAAAAAA

output:

55

result:

wrong answer 1st numbers differ - expected: '100', found: '55'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Skipped

Dependency #1:

0%