QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578692 | #8524. Weather Forecast | LittleXi# | TL | 119ms | 10824kb | C++20 | 3.9kb | 2024-09-20 20:51:10 | 2024-09-20 20:51:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000100
#define ll long long
#define inf 1000000000
int n, m, cnt, a[MAXN], k, dp[MAXN];
double eps = 1e-6, sum[MAXN], b[MAXN];
struct node
{
int left, right;
double Max, lazy;
node *ch[2];
}pool[2*MAXN], *root;
void buildtree(node *p, int left, int right)
{
p->left = left; p->right = right;
if(left == right) return ;
node *lson = &pool[++cnt], *rson = &pool[++cnt];
p->ch[0] = lson; p->ch[1] = rson;
int mid = (left + right) / 2;
buildtree(lson, left, mid); buildtree(rson, mid+1, right);
}
void init(node *p)
{
p->Max = -inf;
p->lazy = 0;
if(p->left == p->right) return ;
init(p->ch[0]); init(p->ch[1]);
}
void push(node *p)
{
if(p->lazy == 0) return ;
if(p->ch[0]) p->ch[0]->lazy += p->lazy;
if(p->ch[1]) p->ch[1]->lazy += p->lazy;
p->Max += p->lazy;
p->lazy = 0;
}
void change(node *p, int left, int right, double s)
{
if(left > right) return ;
push(p);
if(p->left == left && p->right == right)
{
p->lazy += s;
return ;
}
push(p);
if(p->ch[0]->right >= right) change(p->ch[0], left, right, s);
else if(p->ch[1]->left <= left) change(p->ch[1], left, right, s);
else{
change(p->ch[0], left, p->ch[0]->right, s);
change(p->ch[1], p->ch[1]->left, right, s);
}
if(p->ch[0]) push(p->ch[0]);
if(p->ch[1]) push(p->ch[1]);
p->Max = max(p->ch[0]->Max, p->ch[1]->Max);
}
void ask(node *p, int left, int right, double s)
{
if(left > right) return ;
push(p);
if(p->left == left && p->right == right)
{
p->Max = max(p->Max, s);
return ;
}
push(p);
if(p->ch[0]->right >= right) ask(p->ch[0], left, right, s);
else ask(p->ch[1], left, right, s);
if(p->ch[0]) push(p->ch[0]);
if(p->ch[1]) push(p->ch[1]);
p->Max = max(p->ch[0]->Max, p->ch[1]->Max);
}
int query(node *p, int left, int right)
{
// if(left > right) return -inf;
push(p);
if(p->left == left && p->right == right) return p->Max;
if(p->ch[0]->right >= right) return query(p->ch[0], left, right);
else if(p->ch[1]->left <= left) return query(p->ch[1], left, right);
else{
return max(query(p->ch[0], left, p->ch[0]->right), query(p->ch[1], p->ch[1]->left, right));
}
}
int solve(node *p)
{
push(p);
if(p->left == p->right) return p->left;
if(p->ch[0]) push(p->ch[0]);
if(p->ch[1]) push(p->ch[1]);
p->Max = max(p->ch[0]->Max, p->ch[1]->Max);
if(p->Max < 0) return 0;
if(p->ch[1]->Max >= 0) return solve(p->ch[1]);
else return solve(p->ch[0]);
}
bool check(double mid)
{
for(int i=1;i<=n;i++) b[i] = a[i] - mid, sum[i] = sum[i-1] + b[i];//, printf("%lf ",b[i]);
// cout<<endl;
init(root);
int mx = 0;
for(int i=1;i<=n;i++)
{
change(root, 1, mx, b[i]);
if(sum[i] < 0)
{
dp[i] = 0;
continue;
}
dp[i] = 1;
if(query(root, 1, n) < 0);
else{
dp[i] = solve(root) + 1;
}
// if(dp[i] > mx) change(root, dp[i], dp[i], inf);
mx = max(mx, dp[i]);
ask(root, dp[i], dp[i], 0);
}
// for(int i=1;i<=n;i++) printf("%d ",dp[i]);
// printf("%lf %d\n",mid,dp[n]);
return dp[n] >= k;
}
int main()
{
int mn = inf, mx = -inf;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++) scanf("%d",&a[i]), mn = min(mn, a[i]), mx = max(mx, a[i]);
root = &pool[++cnt];
buildtree(root, 1, n);
double Left = mn, Right = mx, mid, ans = mn;
while(Right-Left >= eps)
{
mid = (Left + Right) / 2;
if(check(mid) == true)
ans = mid, Left = mid;
else Right = mid;
// cout<<mid<<endl;
}
// check(1.687500);
printf("%.8lf\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 10068kb
input:
7 3 1 3 1 2 2 2 1
output:
1.66666603
result:
ok found '1.66667', expected '1.66667', error '0.00000'
Test #2:
score: 0
Accepted
time: 0ms
memory: 5940kb
input:
1 1 1
output:
1.00000000
result:
ok found '1.00000', expected '1.00000', error '0.00000'
Test #3:
score: 0
Accepted
time: 1ms
memory: 10012kb
input:
2 1 2 1
output:
1.50000000
result:
ok found '1.50000', expected '1.50000', error '0.00000'
Test #4:
score: 0
Accepted
time: 0ms
memory: 10020kb
input:
3 2 2 4 4
output:
3.00000000
result:
ok found '3.00000', expected '3.00000', error '0.00000'
Test #5:
score: 0
Accepted
time: 1ms
memory: 10012kb
input:
4 2 6 7 3 12
output:
6.49999952
result:
ok found '6.50000', expected '6.50000', error '0.00000'
Test #6:
score: 0
Accepted
time: 1ms
memory: 10068kb
input:
5 3 17 23 13 12 21
output:
16.49999964
result:
ok found '16.50000', expected '16.50000', error '0.00000'
Test #7:
score: 0
Accepted
time: 1ms
memory: 10012kb
input:
7 4 3 37 46 23 46 6 31
output:
22.99999993
result:
ok found '23.00000', expected '23.00000', error '0.00000'
Test #8:
score: 0
Accepted
time: 1ms
memory: 9956kb
input:
10 5 30 91 36 53 74 91 37 1 76 3
output:
39.49999972
result:
ok found '39.50000', expected '39.50000', error '0.00000'
Test #9:
score: 0
Accepted
time: 0ms
memory: 9940kb
input:
100 50 593 336 577 842 505 78 665 825 990 895 952 782 721 242 421 951 786 994 238 154 356 483 686 143 220 473 920 353 738 690 96 915 913 157 412 882 465 585 963 635 68 72 901 143 50 558 310 504 987 97 588 987 841 829 780 497 758 909 503 585 91 657 912 870 663 606 748 492 175 92 375 768 773 206 676 8...
output:
482.99999960
result:
ok found '483.00000', expected '483.00000', error '0.00000'
Test #10:
score: 0
Accepted
time: 7ms
memory: 10184kb
input:
1000 500 74 796 330 98 801 45 160 90 432 788 873 109 714 307 407 94 360 136 198 912 744 902 549 398 478 590 663 983 956 267 201 332 610 249 698 268 700 755 902 485 327 539 203 397 721 971 951 378 674 159 269 182 473 993 84 832 808 908 73 608 842 411 465 886 348 153 924 871 729 1 279 949 475 71 982 3...
output:
394.99999962
result:
ok found '395.00000', expected '395.00000', error '0.00000'
Test #11:
score: 0
Accepted
time: 119ms
memory: 10824kb
input:
10000 5000 821 298 787 377 804 127 552 321 868 2 375 982 196 201 154 323 49 881 81 182 265 584 179 530 130 213 469 887 667 771 637 634 872 528 560 552 168 299 603 668 244 275 838 524 874 508 751 52 83 224 957 910 349 102 285 236 897 44 797 332 834 978 534 730 260 178 842 877 961 219 378 552 294 796 ...
output:
390.49999951
result:
ok found '390.50000', expected '390.50000', error '0.00000'
Test #12:
score: -100
Time Limit Exceeded
input:
200000 100000 240 455 802 920 682 343 84 855 428 864 623 114 400 668 175 66 376 309 970 367 526 980 47 962 793 90 494 352 721 69 920 233 442 103 812 38 644 987 718 897 756 752 490 436 476 46 690 434 869 179 519 74 833 349 970 328 2 77 964 782 383 536 461 736 540 906 249 296 8 35 259 865 267 831 604 ...