QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#728359#5983. Pretty Good Proportionrobinyqc27 ✓5011ms24832kbC++173.0kb2024-11-09 15:02:192024-11-09 15:02:20

Judging History

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

  • [2024-11-09 15:02:20]
  • 评测
  • 测评结果:27
  • 用时:5011ms
  • 内存:24832kb
  • [2024-11-09 15:02:19]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

using i64 = long long;
using u32 = unsigned int;
using u64 = unsigned long long;

template<typename T> using vec = vector<T>;

const i64 X = 5e10;

struct PreBIT
{
    int n;
    vec<i64> t;
    PreBIT(int _n): n(_n), t(n + 1, -1e18) { }
    
    void add(int x, i64 v)
    {
        for (++x; x <= n; x += x & -x) {
            t[x] = max(t[x], v);
        }
    }
    i64 ask(int x)
    {
        i64 res = -1e18;
        for (; x; x -= x & -x) {
            res = max(res, t[x]);
        }
        return res;
    }
};

struct SufBIT
{
    int n;
    vec<i64> t;
    SufBIT(int _n): n(_n), t(n + 1, 1e18) { }
    
    void add(int x, i64 v)
    {
        for (x = n - x; x <= n; x += x & -x) {
            t[x] = min(t[x], v);
        }
    }
    i64 ask(int x)
    {
        i64 res = 1e18;
        for (x = n - x; x; x -= x & -x) { // ?
            res = min(res, t[x]);
        }
        return res;
    }
};

void solve(int ca)
{
    int n;
    string rdstr;
    cin >> n >> rdstr;
    i64 y = stod(rdstr) * X;
    
    string s;
    cin >> s;
    vec<int> a(n + 1);
    for (int i = 0; i < n; i++) {
        a[i + 1] = a[i] + s[i] - '0';
    }
    
    vec<i64> w(n + 1);
    for (int i = 0; i <= n; i++) {
        w[i] = (i64)y * i - X * a[i];
    }
    vec<i64> val(w);
    sort(val.begin(), val.end());
    val.erase(unique(val.begin(), val.end()), val.end());
    vec<int> pos(n + 1);
    for (int i = 0; i <= n; i++) {
        pos[i] = lower_bound(val.begin(), val.end(), w[i]) - val.begin();
    }
    
    int vs = val.size();
    
    auto check = [&](i64 m) -> bool
    {
        PreBIT pb(vs);
        SufBIT sb(vs);

        sb.add(pos[n], w[n] - m * n);
        pb.add(pos[n], w[n] + m * n);
        for (int l = n - 1; l >= 0; l--) {
            i64 vu = w[l] - m * l, vd = w[l] + m * l;
            if (sb.ask(pos[l]) <= vu || pb.ask(pos[l]) >= vd) {
                return true;
            }
            sb.add(pos[l], vu);
            pb.add(pos[l], vd);
        }
        
        return false;
    };
    
    i64 l = 0, r = X, mid;
    while (l < r) {
        mid = (l + r) >> 1;
        if (check(mid)) r = mid;
        else l = mid + 1;
    }
    PreBIT pb(vs);
    SufBIT sb(vs);
    
    i64 m = l;
    int ml = n;
    sb.add(pos[n], w[n] - m * n);
    pb.add(pos[n], w[n] + m * n);
    for (int l = n - 1; l >= 0; l--) {
        i64 vu = w[l] - m * l, vd = w[l] + m * l;
        if (sb.ask(pos[l]) <= vu || pb.ask(pos[l]) >= vd) {
            ml = l;
        }
        sb.add(pos[l], vu);
        pb.add(pos[l], vd);
    }
    // cout << ml + 1 << '\n';
    cout << "Case #" << ca << ": " << ml << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) solve(i + 1);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 10ms
memory: 3736kb

input:

100
10 0.827672
0010101011
4 0.932623
0100
1000 0.834002
011001110010111110000110101100010010100101101110110111100010101101111100110001011000110100010100011011000001100001010110111101111010110110000110011000111000011110101100100111111001111011011100111001011101010100111011100011110011100011110010001...

output:

Case #1: 6
Case #2: 1
Case #3: 10
Case #4: 0
Case #5: 0
Case #6: 1
Case #7: 0
Case #8: 0
Case #9: 0
Case #10: 0
Case #11: 0
Case #12: 4
Case #13: 5
Case #14: 564
Case #15: 0
Case #16: 0
Case #17: 0
Case #18: 0
Case #19: 0
Case #20: 0
Case #21: 0
Case #22: 0
Case #23: 0
Case #24: 844
Case #25: 0
Case...

result:

ok 100 lines

Subtask #2:

score: 22
Accepted

Test #2:

score: 22
Accepted
time: 5011ms
memory: 24832kb

input:

100
15 0.333333
000000000011000
10 0.418754
0101100001
2 0.499999
01
3 0.977951
001
2 0.249999
01
10 0.670229
0111011001
1000 0.500001
001101111110110010110000010010110001110010001101110111010011000010100011011101010110011011011010111110011100011000001000101011100011010100101101111110100101011010111...

output:

Case #1: 6
Case #2: 0
Case #3: 0
Case #4: 2
Case #5: 0
Case #6: 0
Case #7: 0
Case #8: 0
Case #9: 0
Case #10: 0
Case #11: 0
Case #12: 0
Case #13: 0
Case #14: 0
Case #15: 0
Case #16: 4333
Case #17: 0
Case #18: 0
Case #19: 123
Case #20: 0
Case #21: 0
Case #22: 0
Case #23: 0
Case #24: 0
Case #25: 0
Case...

result:

ok 100 lines

Extra Test:

score: 0
Extra Test Passed