QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#137311#2356. Partition of Queriesckz#WA 1ms3544kbC++201.6kb2023-08-10 10:13:312023-08-10 10:13:34

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-10 10:13:34]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3544kb
  • [2023-08-10 10:13:31]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()
using namespace std;

#define int ll

#define x(j) j
#define y(j) (f[j] - pre[j] + (j - 1) * cnt[j])
const int N = 1e6 + 10;
int n, y, pre[N], cnt[N], f[N];
int head, que[N], p;

void solve()
{
    cin >> n >> y;
    string s;
    cin >> s;
    s = '?' + s;

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

        // cerr << pre[i] << ' ' << cnt[i] << '\n';
    }

    int ans = pre[n];

    // cerr << ans << '\n';

    p = 1;
    que[++head] = 0;
    for(int i = 1; i <= n; i++)
    {
        if(s[i] == '+') continue;
        int k = cnt[i - 1];

        while(p < head && k * (x(que[p + 1])) - x(que[p]) >= y(que[p + 1]) - y(que[p])) p++;

        int j = que[p];
        f[i] = y(j) - x(j) * k + y + pre[i - 1];

        while(p < head && (y(que[head]) - y(que[head - 1])) * (x(i) - x(que[head])) >= (y(i) - y(que[head])) * (x(que[head] - x(que[head - 1])))) head--;
        que[++head] = i;

        ans = min(ans, f[i] + pre[n] - pre[i] - (i - 1) * (cnt[n] - cnt[i]));
        // cerr << pre[n] - pre[i] - (i - 1) * (cnt[n] - cnt[i]) << '\n';

        // cerr << f[i] << ' ' << ans << '\n';
    }

    cout << ans << '\n';
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);cout.tie(nullptr);
    int t = 1;
    //cin >> t;
    while(t--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

6 5
++??+?

output:

6

result:

ok single line: '6'

Test #2:

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

input:

6 8
++??+?

output:

7

result:

ok single line: '7'

Test #3:

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

input:

5 1
+++++

output:

0

result:

ok single line: '0'

Test #4:

score: -100
Wrong Answer
time: 1ms
memory: 3532kb

input:

10 0
++?+??++??

output:

-10

result:

wrong answer 1st lines differ - expected: '0', found: '-10'