description
solution
因为每次蚯蚓长度的变化是对除去剪断的蚯蚓外,所有蚯蚓同时变化的,所以容易想到用一个大根堆维护所有蚯蚓的长度,每次长度变化改为新产生的蚯蚓长度
−
q
-q
− q 但是这样是
O
(
m
l
o
g
n
)
O(mlogn)
O ( m l o g n ) 的,无法通过本题 考虑2个蚯蚓的长度
x
x
x 与
y
y
y ,其中
x
x
x 比
y
y
y 先从堆中取出,故
x
≥
y
x\ge y
x ≥ y ,那么产生的新蚯蚓长度
⌊
p
x
⌋
≥
⌊
p
y
⌋
\lfloor px \rfloor\ge\lfloor py \rfloor
⌊ p x ⌋ ≥ ⌊ p y ⌋ ,且
⌊
(
1
−
p
)
x
⌋
≥
⌊
(
1
−
p
)
y
⌋
\lfloor (1-p)x \rfloor\ge\lfloor (1-p)y \rfloor
⌊ ( 1 − p ) x ⌋ ≥ ⌊ ( 1 − p ) y ⌋ ,所以先产生的第一类蚯蚓一定比后产生的第一类蚯蚓长,先产生的第二类蚯蚓一定比后产生的第二类蚯蚓长 所以可以使用三个队列分别维护原有的蚯蚓以及产生的2类蚯蚓,则后2个队列保证单调,接下来只需要让初始的蚯蚓按长度从大到小插入第一个队列,就可以保证每次操作中选中的蚯蚓一定是三个队列队首中最大的一个,就可以
O
(
1
)
O(1)
O ( 1 ) 完成操作了 总复杂度
O
(
n
l
o
g
n
+
m
)
O(nlogn+m)
O ( n l o g n + m )
code
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N= 1e5 + 10 ;
int n, m, d, del, u, v, t, a[ N] ;
double p;
inline int read ( ) {
int x= 0 , f= 1 ; char ch= getchar ( ) ;
while ( ! isdigit ( ch) ) { if ( ch== '-' ) f= 1 ; ch= getchar ( ) ; }
while ( isdigit ( ch) ) { x= ( x<< 3 ) + ( x<< 1 ) + ( ch^ 48 ) ; ch= getchar ( ) ; }
return x* f;
}
queue< int > q[ 4 ] ;
int main ( ) {
n= read ( ) ; m= read ( ) ; d= read ( ) ; u= read ( ) ; v= read ( ) ; t= read ( ) ;
p= u* 1.0 / v;
for ( int i= 1 ; i<= n; ++ i) a[ i] = read ( ) ;
sort ( a+ 1 , a+ n+ 1 ) ;
for ( int i= n; i>= 1 ; -- i) q[ 1 ] . push ( a[ i] ) ;
for ( int i= 1 ; i<= m; ++ i) {
int ans= - 0x3f3f3f3f , pos= 1 ;
if ( ! q[ 1 ] . empty ( ) && q[ 1 ] . front ( ) > ans) ans= q[ 1 ] . front ( ) , pos= 1 ;
if ( ! q[ 2 ] . empty ( ) && q[ 2 ] . front ( ) > ans) ans= q[ 2 ] . front ( ) , pos= 2 ;
if ( ! q[ 3 ] . empty ( ) && q[ 3 ] . front ( ) > ans) ans= q[ 3 ] . front ( ) , pos= 3 ;
q[ pos] . pop ( ) ; ans+ = del; del+ = d;
q[ 2 ] . push ( floor ( p* ans) - del) ; q[ 3 ] . push ( ans- floor ( p* ans) - del) ;
if ( i% t== 0 ) printf ( "%d " , ans) ;
}
puts ( "" ) ;
for ( int i= 1 ; i<= n+ m; ++ i) {
int ans= - 0x3f3f3f3f , pos= 1 ;
if ( ! q[ 1 ] . empty ( ) && q[ 1 ] . front ( ) > ans) ans= q[ 1 ] . front ( ) , pos= 1 ;
if ( ! q[ 2 ] . empty ( ) && q[ 2 ] . front ( ) > ans) ans= q[ 2 ] . front ( ) , pos= 2 ;
if ( ! q[ 3 ] . empty ( ) && q[ 3 ] . front ( ) > ans) ans= q[ 3 ] . front ( ) , pos= 3 ;
q[ pos] . pop ( ) ;
if ( i% t== 0 ) printf ( "%d " , ans+ del) ;
}
return 0 ;
}