作者:zoudan的世界94129433 | 来源:互联网 | 2023-10-17 10:04
题目链接:ProductBo思想很好,挺有趣的题,在床上突然会做的。#include<bitsstdc++.h>usingnamespacestd;typede
题目链接:Product Bo
思想很好,挺有趣的题,在床上突然会做的。
using namespace std ;
typedef long long LL ;
typedef pair <int , int > pii ;
const int MAXN = 200005 ;
struct Node {
LL val ;
int l1 , m1 , r1 ;
int l2 , m2 , r2 ;
int f ;
Node () {}
Node ( LL v , int l1 , int m1 , int r1 , int l2 , int m2 , int r2 )
: val ( v ) , l1 ( l1 ) , m1 ( m1 ) , r1 ( r1 ) , l2 ( l2 ) , m2 ( m2 ) , r2 ( r2 ) , f ( 0 ) {}
} ;
queue q ;
int n , m , k ;
int pos[MAXN] , c1 ;
int neg[MAXN] , c2 ;
LL sum1[MAXN] , sum2[MAXN] ;
int check ( int n , int m , int k ) {
if ( n - m <m ) m = n - m ;
LL ans = 1 ;
for ( int i = 1 ; i <= m ; ++ i ) {
ans *= n ;
ans /= i ;
if ( ans >= k ) return -1 ;
-- n ;
}
if ( ans >= k ) return -1 ;
return ans ;
}
int check2 ( int c1 , int c2 , int m , int k ) {
int ans = 0 ;
for ( int i = 0 ; i <= c2 && i <= m ; i += 2 ) if ( m - i <= c1 ) {
int x1 = check ( c1 , m - i , k ) , x2 = check ( c2 , i , k ) ;
if ( x1 == -1 || x2 == -1 || 1LL * x1 * x2 >= k ) return -1 ;
ans += x1 * x2 ;
if ( ans >= k ) return -1 ;
}
return ans ;
}
int cal ( int c1 , int c2 , int m , int k ) {
int ans = 0 ;
for ( int i = 1 ; i <= c2 && i <= m ; ++ i ) if ( m - i <= c1 ) {
int x1 = check ( c1 , m - i , k ) , x2 = check ( c2 , i , k ) ;
if ( x1 == -1 || x2 == -1 || 1LL * x1 * x2 >= k ) return -1 ;
ans += x1 * x2 ;
if ( ans >= k ) return -1 ;
}
return ans ;
}
int cmp ( const int& a , const int& b ) {
return a > b ;
}
void solve () {
c1 = c2 = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
char op[2] ;
int x ;
scanf ( "%s" , op ) ;
if ( op[0] == '+' ) {
scanf ( "%d" , &x ) ;
pos[++ c1] = x ;
}
if ( op[0] == '-' ) {
scanf ( "%d" , &x ) ;
neg[++ c2] = x ;
}
}
//printf ( "%d %d\n" , c1 , c2 ) ;
int tot = check2 ( c1 , c2 , m , k ) ;
//printf ( "%d\n" , tot ) ;
if ( tot == -1 ) {
sort ( pos + 1 , pos + c1 + 1 , cmp ) ;
sort ( neg + 1 , neg + c2 + 1 , cmp ) ;
sum1[0] = sum2[0] = 0 ;
for ( int i = 1 ; i <= c1 ; ++ i ) {
sum1[i] = sum1[i - 1] + pos[i] ;
}
for ( int i = 1 ; i <= c2 ; ++ i ) {
sum2[i] = sum2[i - 1] + neg[i] ;
}
int cnt = 0 ;
LL l = -1e15 , r = 1e15 ;
while ( l LL lim = l + r + 1 >> 1 ;
cnt = 0 ;
while ( !q.empty () ) q.pop () ;
for ( int i = 0 ; i <= c2 && i <= m ; i += 2 ) {
if ( m - i <= c1 ) {
if ( sum2[i] + sum1[m - i] >= lim ) {
q.push ( Node ( sum2[i] + sum1[m - i] , m - i - 1 , m - i , c1 + 1 , i - 1 , i , c2 + 1 ) ) ;
}
}
}
while ( !q.empty () ) {
Node u = q.front () , tmp ;
q.pop () ;
++ cnt ;
if ( cnt >= k ) break ;
if ( !u.f && u.m1 ) {
if ( u.m1 + 1 tmp = u ;
tmp.val = u.val - pos[u.m1] + pos[u.m1 + 1] ;
tmp.m1 = u.m1 + 1 ;
if ( tmp.val >= lim ) q.push ( tmp ) ;
}
if ( u.l1 && u.l1 + 1 tmp = u ;
tmp.val = u.val - pos[u.l1] + pos[u.l1 + 1] ;
tmp.l1 = u.l1 - 1 ;
tmp.m1 = u.l1 + 1 ;
tmp.r1 = u.m1 ;
if ( tmp.val >= lim ) q.push ( tmp ) ;
}
}
if ( u.m2 ) {
if ( u.m2 + 1 tmp = u ;
tmp.val = u.val - neg[u.m2] + neg[u.m2 + 1] ;
tmp.m2 = u.m2 + 1 ;
tmp.f = 1 ;
if ( tmp.val >= lim ) q.push ( tmp ) ;
}
if ( u.l2 && u.l2 + 1 tmp = u ;
tmp.val = u.val - neg[u.l2] + neg[u.l2 + 1] ;
tmp.l2 = u.l2 - 1 ;
tmp.m2 = u.l2 + 1 ;
tmp.r2 = u.m2 ;
tmp.f = 1 ;
if ( tmp.val >= lim ) q.push ( tmp ) ;
}
}
}
if ( cnt 1
;
else l = lim ;
}
printf (
"+ %I64d\n" , l ) ;
return ;
}
int zero = cal ( c1 + c2 , n - c1 - c2 ,
m , k ) ;
if ( zero == -
1 || tot + zero >= k ) {
printf (
"0\n" ) ;
return ;
}
tot += zero ;
k -= tot ;
sort (
pos +
1 ,
pos + c1 +
1 ) ;
sort ( neg +
1 , neg + c2 +
1 ) ;
sum1[
0] = sum2[
0] =
0 ;
for (
int i =
1 ; i <= c1 ; ++ i ) {
sum1[i] = sum1[i -
1] +
pos[i] ;
}
for (
int i =
1 ; i <= c2 ; ++ i ) {
sum2[i] = sum2[i -
1] + neg[i] ;
}
int cnt =
0 ;
LL l = -
1e15 , r =
1e15 ;
while ( l
LL lim = l + r >> 1 ;
cnt = 0 ;
while ( !q.empty () ) q.pop () ;
for ( int i = 1 ; i <= c2 && i <= m ; i += 2 ) {
if ( m - i <= c1 ) {
if ( sum2[i] + sum1[m - i] <= lim ) q.push ( Node ( sum2[i] + sum1[m - i] , m - i - 1 , m - i , c1 + 1 , i - 1 , i , c2 + 1 ) ) ;
}
}
while ( !q.empty () ) {
Node u = q.front () , tmp ;
q.pop () ;
//printf ( "%d: %I64d %d %d\n" , cnt + 1 , u.val , u.m1 , u.m2 ) ;
++ cnt ;
if ( cnt >= k ) break ;
if ( !u.f && u.m1 ) {
if ( u.m1 + 1 tmp = u ;
tmp.val = u.val - pos[u.m1] + pos[u.m1 + 1] ;
tmp.m1 = u.m1 + 1 ;
if ( tmp.val <= lim ) q.push ( tmp ) ;
}
if ( u.l1 && u.l1 + 1 tmp = u ;
tmp.val = u.val - pos[u.l1] + pos[u.l1 + 1] ;
tmp.l1 = u.l1 - 1 ;
tmp.m1 = u.l1 + 1 ;
tmp.r1 = u.m1 ;
if ( tmp.val <= lim ) q.push ( tmp ) ;
}
}
if ( u.m2 ) {
if ( u.m2 + 1 tmp = u ;
tmp.val = u.val - neg[u.m2] + neg[u.m2 + 1] ;
tmp.m2 = u.m2 + 1 ;
tmp.f = 1 ;
if ( tmp.val <= lim ) q.push ( tmp ) ;
}
if ( u.l2 && u.l2 + 1 tmp = u ;
tmp.val = u.val - neg[u.l2] + neg[u.l2 + 1] ;
tmp.l2 = u.l2 - 1 ;
tmp.m2 = u.l2 + 1 ;
tmp.r2 = u.m2 ;
tmp.f = 1 ;
if ( tmp.val <= lim ) q.push ( tmp ) ;
}
}
}
//printf ( "%d %I64d %I64d\n" , cnt , l , r ) ;
if ( cnt 1 ;
else r = lim ;
}
printf ( "- %I64d\n" , l ) ;
}
int main () {
while ( ~scanf ( "%d%d%d" , &n , &m , &k ) && ( n || m || k ) ) solve () ;
return 0 ;
}