QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#77171 | #5500. Bars | UCSC_Ravioli# | WA | 349ms | 3404kb | C++20 | 1.2kb | 2023-02-13 07:30:15 | 2023-02-13 07:30:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<ll> v(n);
for(auto &d:v) cin >> d;
vector<int> st;
// Optimal solution assuming bars 0 and i are the leftmost and rightmost bars
for(int i=0; i<n; i++){
while(st.size() >= 2){
ll keep = v[end(st)[-1]] * (i - end(st)[-2]);
keep += v[end(st)[-2]] * (end(st)[-1] - end(st)[-2]);
keep += v[i] * (i - end(st)[-1]);
ll rmv = v[end(st)[-2]] * (i - end(st)[-2]);
rmv += v[i] * (i - end(st)[-2]);
// cout << "at bar " << i << endl;
// cout << "stack: ";
// for(int d:st) cout << d << ' ';
// cout << endl;
// cout << "keep: " << keep << endl;
// cout << "rmv: " << rmv << endl;
if(rmv >= keep) st.pop_back();
break;
}
st.push_back(i);
}
int m = st.size();
// cout << "stack: ";
// for(int d:st) cout << d << ' ';
// cout << endl;
ll ans = v[st[0]] * (st[1] - st[0]);
ans += v[end(st)[-1]] * (end(st)[-1] - end(st)[-2]);
for(int i=1; i+1<m; i++){
ans += v[st[i]] * (st[i+1] - st[i-1]);
}
cout << ans << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 3316kb
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: 349ms
memory: 3404kb
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 323902577661 517310514769 442743983454 458946673513 296420749955 684398023821 488908817507 452497461066 225238173573 548995280859 372195572969 228928264291 585094154153 158133693702 336548826266 374347211592 487728425474 465074500144 356621942568 632295317148 733272508643 696364797312 26408806...
result:
wrong answer 3rd lines differ - expected: '382465638565', found: '323902577661'