QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77302 | #5500. Bars | zhl135 | WA | 290ms | 3456kb | C++17 | 949b | 2023-02-14 00:07:33 | 2023-02-14 00:07:36 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n; cin >> n;
vector<int> p(n); for(int& v : p) cin >> v;
vector<int> stk = {n-1};
auto bad = [&](int i, int j, int k){
// (i,p[i]), etc
int ax = j-i, ay = p[j]-p[i], bx = k-i, by = p[k]-p[i];
return 1ll*ax*by - bx*ay <= 0;
};
for(int i = n-2; i >= 0; i--){
while(stk.size() >= 2 && bad(stk[stk.size()-2],stk.back(),i)) stk.pop_back();
stk.push_back(i);
}
int64_t ans = 0;
for(int i = 0; i+1 < (int)stk.size(); i++){
ans += 1ll*(p[stk[i]]+p[stk[i+1]])*(stk[i]-stk[i+1]);
}
cout << ans << "\n";
}
int main(){
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while(t--) solve();
}
// if we choose bars i_1,i_2,...,i_k,
// then the answer is (p_{i_1}+p_{i_2})(i_2-i_1)+(p_{i_2}+p_{i_3})(i_3-i_2) + ...
// equal to the sum of trapezoid areas between points
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3328kb
input:
2 4 5 2 2 6 5 1 5 4 4 1
output:
33 29
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 290ms
memory: 3456kb
input:
10000 4 5 2 2 6 5 1 5 4 4 1 197 763787596 15221694 898228999 187472305 466351873 822742732 437754202 800092772 843092246 915675776 166265020 346340615 796714085 497548541 182089610 64356048 363276768 181268733 257949015 236568898 752096761 928725929 443146784 114577469 833053207 38120723 14891030 41...
output:
33 29 333879830655 628489725924 491042430052 458946673513 296420749955 716467001512 561025057127 191203468234 225238173690 404422066860 451859366839 257334694966 585094154153 177939107274 336548832140 398831377634 509617567062 496154209275 397687349340 207478011024 805237591861 216290528451 26408806...
result:
wrong answer 3rd lines differ - expected: '382465638565', found: '333879830655'