QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#591762 | #7079. Array | Afterlife# | WA | 205ms | 10008kb | C++20 | 3.1kb | 2024-09-26 17:39:33 | 2024-09-26 17:39:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int n ;
int a[1000005];
typedef long long ll;
typedef pair<int,int> pii;
ll S(int x) {
return 1LL * x * (x + 1) / 2;
}
struct bit {
ll c[1000005];
int n;
void init(int _n) {
n = _n;
for(int i = 1;i <= n;i++) c[i] = 1e18;
}
void upd(int x,ll y) {
while(x <= n) {
c[x] = min(c[x] , y);
x += (x & -x);
}
}
ll qry(int x) {
ll ans = 1e18;
while(x) {
ans = min(ans , c[x]) ;
x -= (x & -x);
}
return ans;
}
}b[2];
mt19937 rnd(0);
vector<int> pos[1000005];
int ap[1000005];
void solv() {
cin >> n;
vector<int> v;
for(int i = 1;i <= n;i++) {
cin >> a[i];
// a[i] = rnd()%1000000000 + 1;
v.push_back(a[i]) ;
}
sort(v.begin() , v.end()) ;
v.erase(unique(v.begin() , v.end()) , v.end()) ;
for(int i = 1;i <= v.size();i++) pos[i].clear() ;
ll ans = 0;
set<int> st;
int cc = 0;
for(int i = 1;i <= n;i++) {
st.insert(a[i]) ;
ap[i] = lower_bound(v.begin() , v.end() , a[i]) - v.begin() + 1;
if(pos[ap[i]].size() == 0) cc++ ;
pos[ap[i]].push_back(i) ;
ans += 1LL * i * cc ;
}
vector<pii> p;
for(int i = 1;i <= v.size() ; i++) {
p.push_back({pos[i][0] , v[i - 1]}) ;
}
sort(p.begin() , p.end()) ;
st.clear() ;
ll sol = 0;
auto qmin = [&](int x) ->ll {
if(st.size() == 0) return 1e18;
auto it = st.lower_bound(x) ;
ll ans = 1e18;
if(it != st.end()) ans = min(ans , (ll)abs(x - (*it))) ;
if(it != st.begin()) {
it-- ;
ans = min(ans , (ll)abs(x - (*it))) ;
}
return ans;
};
for(int i = 0;i < p.size();i++) {
auto &v = pos[ap[p[i].first]] ;
int b2;
if(v.size() == 1) b2 = n + 1;
else b2 = v[1];
ll dec = S(b2 - 1) - S(v[0] - 1) ;
ll cur = qmin(p[i].second) - dec ;
sol = min(sol , cur) ;
st.insert(p[i].second) ;
}
b[0].init(v.size()) ;
b[1].init(v.size()) ;
for(int i = p.size() - 1 ; i >= 0;i--) {
auto &vt = pos[ap[p[i].first]] ;
int b2 , b1 = p[i].first;
if(vt.size() == 1) b2 = n + 1;
else b2 = vt[1];
int bpos = lower_bound(v.begin() , v.end() , p[i].second) - v.begin() + 1;
/// if a > b , a - b - S(b2-1) + S(a1)
sol = min(sol , -S(b2 - 1) - p[i].second + b[0].qry(v.size() - bpos + 1)) ;
// assert((int)v.size() - bpos + 1 > 0) ;
// assert(bpos > 0);
b[0].upd((int)v.size() - bpos + 1 , p[i].second + S(b1)) ;
/// if a < b , b - a - S(b2-1) + S(a1)
sol = min(sol , p[i].second - S(b2 - 1) + b[1].qry(bpos)) ;
b[1].upd(bpos , -p[i].second + S(b1)) ;
}
cout << ans + sol << '\n' ;
return ;
}
int main() {
ios::sync_with_stdio(false) ; cin.tie(0) ;
int t;cin >> t;
while(t--) solv() ;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9728kb
input:
1 4 1 2 3 4
output:
22
result:
ok "22"
Test #2:
score: 0
Accepted
time: 142ms
memory: 10008kb
input:
100000 10 873324878 873324878 873324878 873324878 891656676 891656676 615245360 873324878 873324878 873324878 10 560723194 560723194 797429144 797429144 560723194 797429144 819647695 560723194 797429144 560723194 10 750627649 746781323 756277046 756277046 750627649 750627649 756277046 750627649 9142...
output:
134 141 180 168 109 95 181 185 144 149 202 158 161 107 210 210 255 104 210 109 82 158 149 201 154 109 206 158 161 161 158 134 206 198 161 109 143 152 183 156 171 201 149 104 210 161 181 152 152 250 185 243 206 158 152 128 147 203 225 143 203 198 206 201 158 109 114 100 183 161 162 183 154 222 181 14...
result:
ok 100000 tokens
Test #3:
score: 0
Accepted
time: 196ms
memory: 10004kb
input:
39978 23 512481863 596944631 624383245 441725511 441725511 594496544 441725511 624383245 698754670 596944631 182448912 636350614 596944631 391310300 624383245 391310300 698754670 596944631 441725511 182448912 826649520 351713941 596944631 24 789886131 679943285 874352131 191233114 214841280 21484128...
output:
2322 2416 4226 1974 3362 1631 2373 2521 3618 4540 2589 2104 2692 1407 2757 2394 3938 2106 2769 3209 3418 3828 2339 3954 5404 3191 1734 4260 2178 1354 2829 2552 2820 2385 3102 3442 3459 3027 3631 2399 1682 4564 4494 4217 3756 4176 3290 4109 1702 3740 4220 4117 2118 1840 4223 3499 3834 2472 5465 1636 ...
result:
ok 39978 tokens
Test #4:
score: -100
Wrong Answer
time: 205ms
memory: 9672kb
input:
10000 100 70652501 126219335 870011044 503878453 331807482 42366188 570696778 892481058 11179909 898060545 596710776 892481058 892481058 126219335 938507063 540380652 869222706 898060545 380041360 643567581 977928808 655190742 75768776 126219335 386687451 513015608 898060545 540380652 798597796 7794...
output:
156439 177539 142728 105896 98255 145550 158303 139241 142500 123767 164463 173494 162837 133032 161874 108825 159157 176582 143686 146818 161864 166700 93293 121994 166017 96652 176532 106752 141748 145107 147984 139577 153293 146263 112899 130505 178781 186526 162804 138862 106517 170352 136899 13...
result:
wrong answer 959th words differ - expected: '140167', found: '140202'