QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#567691 | #9313. Make Max | ksc | TL | 0ms | 3636kb | C++17 | 2.0kb | 2024-09-16 13:32:09 | 2024-09-16 13:32:09 |
Judging History
answer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
struct r {
ll v, c;
int i;
bool operator<(const r& o) const {
return v > o.v;
}
};
vector<r> arr;
vector<int> max_q;
ll max_val, sum;
void get_max(ll val, int t) {
if (val > max_val) {
max_val = val;
max_q.clear();
}
if (val == max_val) max_q.push_back(t);
}
void find(int cnt, int l, int r, int maxi_now, ll max_now) {
if (l > r || maxi_now >= arr.size()) return;
if (l < 0 || r >= arr.size()) return;
while (maxi_now < arr.size() && arr[maxi_now].v == max_now) {
maxi_now++;
}
while (maxi_now < arr.size() && !(arr[maxi_now].i <= r && arr[maxi_now].i >= l)) {
maxi_now++;
}
if (maxi_now < arr.size()) {
sum += cnt * arr[maxi_now].c;
find(cnt + 1, l, arr[maxi_now].i - 1, maxi_now + 1, arr[maxi_now].v);
find(cnt + 1, arr[maxi_now].i + 1, r, maxi_now + 1, arr[maxi_now].v);
}
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
arr.clear();
max_q.clear();
sum = 0;
max_val = 0;
ll cnt = 0, val = 0;
int t = 0;
for (int i = 0; i < n; i++) {
ll key;
cin >> key;
if (val != key) {
if (cnt != 0) {
get_max(val, t);
arr.push_back({ val, cnt, t++ });
}
val = key;
cnt = 1;
}
else cnt++;
}
get_max(val, t);
arr.push_back({ val, cnt, t });
sort(arr.begin(), arr.end());
for (int i = 0; i < max_q.size(); i++) {
int l = i == 0 ? 0 : max_q[i - 1];
int r = i == max_q.size() - 1 ? arr.size() - 1 : max_q[i];
if (l == r) continue;
find(1, l, r, 0, max_val);
}
cout << sum << endl;
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3636kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 3
result:
ok 4 number(s): "1 0 3 3"
Test #2:
score: -100
Time Limit Exceeded
input:
2 198018 875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...