QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#137355 | #2356. Partition of Queries | Vengeful_Spirit# | WA | 1ms | 5600kb | C++20 | 1.5kb | 2023-08-10 11:06:42 | 2023-08-10 11:06:45 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int MN = 1e6+5;
long long n, y;
long long sum[MN], num0[MN], num1[MN];
long long f[MN], F[MN];
deque<int> q;
pair<long, long> slp(int y, int x) {return {num0[y]-num0[x], F[y]-F[x]};}
bool cmp(pair<long long, long long> x, pair<long long, long long> y) {
__int128 one = 1;
__int128 xx = one * x.second * y.first;
__int128 yy = one * x.first * y.second;
return xx > yy;
}
void Add(int id) {
while(q.size() >= 2 && cmp(slp(q[q.size()-1], q[q.size()-2]), slp(id, q[q.size()-1]))) q.pop_back();
q.push_back(id);
}
int Get(long long x) {
// cerr << x << "~~" << endl;
// for(auto x : q) cerr << F[x] << "-" << num0[x] << " ";cerr << "\n";
while(q.size() >= 2 && cmp({1, x}, slp(q[1], q[0]))) q.pop_front();
return q[0];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> y;
string s;
cin >> s;
f[0] = -y;
for(int i = 0; i < n; ++i) {
num0[i+1] = num0[i] + (s[i]=='+');
num1[i+1] = num1[i] + (s[i]=='?');
if(s[i]=='?') sum[i+1] = sum[i] + num0[i];
else sum[i+1]=sum[i];
}
for(int i = 1; i <= n; ++i) {
F[i-1]=f[i-1]+y+1ll*num0[i-1]*num1[i-1]-sum[i-1];
Add(i-1);
int id=Get(num1[i]);
// cerr << id << " " << q.size() << "\n";
f[i]=F[id]+sum[i]-num1[i]*num0[id];
}
// for(int i = 0; i <= n; ++i) cerr << f[i] << "\n";
cout << f[n] << endl;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5600kb
input:
6 5 ++??+?
output:
6
result:
ok single line: '6'
Test #2:
score: 0
Accepted
time: 1ms
memory: 5520kb
input:
6 8 ++??+?
output:
7
result:
ok single line: '7'
Test #3:
score: 0
Accepted
time: 0ms
memory: 5568kb
input:
5 1 +++++
output:
0
result:
ok single line: '0'
Test #4:
score: -100
Wrong Answer
time: 0ms
memory: 5584kb
input:
10 0 ++?+??++??
output:
8
result:
wrong answer 1st lines differ - expected: '0', found: '8'