QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#513081 | #9142. Uniting Amoebas | Sorting | RE | 43ms | 3660kb | C++20 | 2.0kb | 2024-08-10 16:51:10 | 2024-08-10 16:51:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
const int MAXN = 2e5 + 7 ;
int n ;
ll a[ MAXN ] ;
set < int > s ;
multiset < pair < ll , int > > cands ;
void solve ( ) {
cin >> n ;
s.clear ( ) ; cands.clear ( ) ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> a[ i ] ;
s.insert ( i ) ;
if ( i < n ) {
cands.insert ( { min ( a[ i ] , a[ i + 1 ] ) , i } ) ;
}
else {
cands.insert ( { min ( a[ i ] , a[ 1 ] ) , i } ) ;
}
}
int ctr = n - 1 ;
ll ans = 0 ;
while ( ctr -- ) {
auto it1 = cands.begin ( ) ;
int wh = it1->second ;
auto it = s.find ( wh ) ;
int prv , nxt , hh ;
if ( it != s.begin ( ) ) {
-- it ;
prv = *it ;
}
else {
it = s.end ( ) ;
-- it ;
prv = *it ;
}
it = s.find ( wh ) ;
++ it ;
if ( it == s.end ( ) ) { it = s.begin ( ) ; }
nxt = *it ;
++ it ;
if ( it == s.end ( ) ) { it = s.begin ( ) ; }
hh = *it ;
s.erase ( wh ) ;
ans += min ( a[ wh ] , a[ nxt ] ) ;
cands.erase ( { min ( a[ prv ] , a[ wh ] ) , prv } ) ;
cands.erase ( { min ( a[ wh ] , a[ nxt ] ) , wh } ) ;
cands.erase ( { min ( a[ nxt ] , a[ hh ] ) , nxt } ) ;
a[ wh ] += a[ nxt ] ;
s.erase ( nxt ) ;
cands.insert ( { min ( a[ prv ] , a[ wh ] ) , prv } ) ;
cands.insert ( { min ( a[ wh ] , a[ hh ] ) , wh } ) ;
}
cout << ans << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
input:
3 3 1 1 1 4 0 1 0 2 2 100 42
output:
2 1 42
result:
ok 3 number(s): "2 1 42"
Test #2:
score: 0
Accepted
time: 43ms
memory: 3648kb
input:
100000 2 653035426 456936267 2 409660907 250295986 2 347384960 200356123 2 283557883 840123319 2 17610671 987402881 2 136895857 909667348 2 248911778 780375903 2 275329617 285342631 2 561813712 698064200 2 400903421 742043963 2 345893112 519641162 2 14593307 406479738 2 616391850 347632216 2 1973087...
output:
456936267 250295986 200356123 283557883 17610671 136895857 248911778 275329617 561813712 400903421 345893112 14593307 347632216 19730879 421626272 56181583 370600905 308183211 258362299 586922640 40159075 198139063 142754581 51756812 119730671 81065481 143568225 273158312 49357396 14434997 396080587...
result:
ok 100000 numbers
Test #3:
score: -100
Runtime Error
input:
10000 17 682602980 427465994 308629394 109001734 104676154 428796022 186854021 449599165 926341494 518520271 848737248 161898549 472666174 717817465 55045664 497142577 155911030 17 175921543 574867131 81827354 668974927 924162354 915237015 544633396 979579177 69438027 7405300 106382140 464417239 180...