QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#633558 | #8242. V-Diagram | cy325 | WA | 88ms | 4196kb | C++20 | 2.3kb | 2024-10-12 15:42:41 | 2024-10-12 15:42:41 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
scanf("%d", &n);
vector<long long> a(n + 2); // 添加两个哨兵
a[0] = LLONG_MAX;
a[n + 1] = LLONG_MAX;
for (int i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
// 找到 V 图的最低点(转折点)
int tip = -1;
for (int i = 2; i <= n - 1; i++) {
if (a[i - 1] > a[i] && a[i] < a[i + 1]) {
tip = i;
break;
}
}
if (tip == -1) {
// 如果没有找到转折点,整个序列可能是单调的,不是 V 图
// 但根据题目保证,a 是 V 图,这种情况不会发生
tip = 1;
}
// 前缀和和后缀和
vector<long long> prefixSum(n + 2, 0), suffixSum(n + 2, 0);
for (int i = 1; i <= n; i++) {
prefixSum[i] = prefixSum[i - 1] + a[i];
}
for (int i = n; i >= 1; i--) {
suffixSum[i] = suffixSum[i + 1] + a[i];
}
// 初始化最大平均值为整个序列的平均值
double maxAvg = (double)prefixSum[n] / n;
// 尝试从左侧删除一些元素
for (int left = 1; left <= tip - 2; left++) {
// 检查剩余的子序列是否仍然是 V 图
if (a[left] > a[left + 1]) {
// 计算子序列的长度和和
int len = n - left + 1;
long long sum = prefixSum[n] - prefixSum[left - 1];
double avg = (double)sum / len;
if (avg > maxAvg) {
maxAvg = avg;
}
} else {
break;
}
}
// 尝试从右侧删除一些元素
for (int right = n; right >= tip + 2; right--) {
if (a[right] > a[right - 1]) {
int len = right;
long long sum = prefixSum[right];
double avg = (double)sum / len;
if (avg > maxAvg) {
maxAvg = avg;
}
} else {
break;
}
}
// 尝试仅保留转折点及其相邻元素
if (n >= 3) {
long long sum = a[tip - 1] + a[tip] + a[tip + 1];
double avg = sum / 3.0;
if (avg > maxAvg) {
maxAvg = avg;
}
}
printf("%.10f\n", maxAvg);
}
int main() {
int t;
scanf("%d", &t);
while (t--) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3808kb
input:
2 4 8 2 7 10 6 9 6 5 3 4 8
output:
6.7500000000 5.8333333333
result:
ok 2 numbers
Test #2:
score: 0
Accepted
time: 88ms
memory: 4060kb
input:
100000 3 948511478 739365502 813471668 3 881046825 27458122 398507422 3 987554257 399092415 924260278 3 984128569 125199021 716360525 3 529589236 45783262 313507287 3 645443456 85994112 226010681 3 914820717 228360911 572267310 3 418958362 56703604 195276041 3 64461646 26764720 26995581 3 914535039 ...
output:
833782882.6666666269 435670789.6666666865 770302316.6666666269 608562705.0000000000 296293261.6666666865 319149416.3333333135 571816312.6666666269 223646002.3333333433 39407315.6666666642 383253737.6666666865 734363638.6666666269 779975824.3333333731 490276408.3333333135 574448414.0000000000 3379802...
result:
ok 100000 numbers
Test #3:
score: -100
Wrong Answer
time: 25ms
memory: 4196kb
input:
10000 4 194123849 79274911 191162487 570110764 86 957917218 915359202 914726017 873273226 867724859 867674150 809652204 805531383 745262007 743835491 727071232 714782071 645394643 639432679 594879540 587173904 583418126 560538589 518721836 469558994 427721766 411582333 404948350 402948978 357228675 ...
output:
258668002.7500000000 516690462.3043478131 495489050.3529411554 513362357.4634146094 472025965.6999999881 543693608.2500000000 543366581.5161290169 254833443.1999999881 413627296.2857142687 502458665.3846153617 564217787.3333333731 479468115.1182795763 466246020.2045454383 551500780.7727272511 537648...
result:
wrong answer 2nd numbers differ - expected: '527118856.7555556', found: '516690462.3043478', error = '0.0197838'