QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#137313 | #2356. Partition of Queries | ckz# | WA | 1ms | 3584kb | C++20 | 1.6kb | 2023-08-10 10:14:21 | 2023-08-10 10:14:23 |
Judging History
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 - cnt[i]) * (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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3528kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3468kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3584kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 3464kb
input:
10 0 ++?+??++??
output:
-10
result:
wrong answer 1st lines differ - expected: '0', found: '-10'