This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "boxes.h"
#include <bits/stdc++.h>
using namespace std ;
const int N = 1e7 + 4 ;
template <class T>
bool chmax( T& x , const T& y ){
bool f = 0 ;
if ( x < y ) x = y , f = 1 ;
return f ;
}
template <class T>
bool chmin( T &x , const T &y ){
bool f = 0 ;
if ( x > y ) x = y , f = 1 ;
return f ;
}
int dis ( int a , int b , int l ){
if ( a > b ) swap(a,b) ;
return min ( b-a , a+(l-b) ) ;
}
long long delivery(int n, int k, int l, int a[]) {
int ans = 0 , m = N , cnt = 0 , p = 0 , left = 0 ;
for ( int i = 0 ; i < n ; i ++ ){
if ( a[i] <= l/2 ) continue ;
chmin ( m , i ) ;
if ( a[i] == a[i+1] ){
cnt ++ ; continue ;
}
ans += dis(a[i] , p , l ) ;
if ( left > cnt ){
p = a[i] ; cnt = 0 ;
left -= cnt ;
continue ;
}
if ( left == cnt ){
p = 0 ; cnt = 0 ;
left = k ; ans += dis(a[i],0,l) ;
continue ;
}
cnt -= left ;
int x = cnt/k ;
ans += x*(dis(0,a[i],l)*2) ;
cnt %= k ;
left = cnt , p = a[i] ;
} ans += dis(0,p,l) ;
left = k , p = 0 ; cnt = 0 ;
for ( int i = m-1 ; i >= 0 ; i -- ){
if ( i && a[i] == a[i-1] ){
cnt ++ ; continue ;
}
ans += dis(a[i] , p , l ) ;
if ( left > cnt ){
p = a[i] ; cnt = 0 ;
left -= cnt ;
continue ;
}
cout << a[i] << ' ' << i << endl ;
if ( left == cnt ){
p = 0 ; cnt = 0 ;
left = k ;
ans += dis(0,a[i],l) ;
continue ;
}
cnt -= left ;
int x = cnt/k ;
ans += x*(dis(0,a[i],l)*2) ;
cnt %= k ;
left = cnt ; p = a[i] ;
if ( !cnt ){
left = k ;
p = 0 ; ans += dis(0,a[i],l) ;
}
cnt = 0 ;
cout << ans << ' ' << a[i] << endl ;
} ans += dis(0,p,l) ;
return ans ;
}
/*
int a[N] ;
signed main(){
int n , k , l ; cin >> n >> k >> l ;
for ( int i = 0 ; i < n ; i ++ ) cin >> a[i] ;
cout << delivery( n , k , l , a ) ;
}
//*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |