QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#400281#7942. $K$ Subsequencesucup-team3160TL 50ms3844kbC++143.3kb2024-04-27 09:23:132024-04-27 09:23:13

Judging History

你现在查看的是最新测评结果

  • [2024-04-27 09:23:13]
  • 评测
  • 测评结果:TL
  • 用时:50ms
  • 内存:3844kb
  • [2024-04-27 09:23:13]
  • 提交

answer

# include <bits/stdc++.h>

using namespace std ;

int n , m ;
int a[200005] ;
typedef pair< int , int > pii ;
int cnt1 = 0 , cnt2 = 0 ;
int Read()
{
    int num=0,k=1;   //k是正负数标记
    char c=getchar();
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();//注意判断'-'
    if(c=='-')
    {
        k=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        num=(num<<3)+(num<<1)+(c^48);//num<<3相当于num*8,num<<1相当于num*2,
        c=getchar();                //c^48利用位运算优化,相当于c-'0' ,但是一定要加括号
    }
    return num*k;
}
  
void print1(int x)
{
    if(x>9) print1(x/10);  //把这个数一位一位的输出
    putchar(x%10+'0');
}
struct pq1
{
	priority_queue < pii > q1 , q2 ;
	priority_queue < pii , vector < pii > , greater < pii > > q3 , q4 ;
	void init()
	{
		while ( q1.size() ) q1.pop() ;
		while ( q2.size() ) q2.pop() ;
		while ( q3.size() ) q3.pop() ;
		while ( q4.size() ) q4.pop() ;
	 } 
	void push( pii x )
	{
//		printf("push:{%d,%d}\n" , x.first , x.second) ;
		q1.push( x ) ;
		q3.push( x ) ;
	}
	void del( pii x )
	{
//		printf("pop:{%d,%d}\n" , x.first , x.second) ;
		q2.push( x ) ;
		q4.push( x ) ;
	}
	pii maxv()
	{
		while ( q1.size() && q2.size() )
		{
			if ( q1.top() != q2.top() ) return q1.top() ;
			q1.pop() , q2.pop() ;
		}
		return q1.top() ;
	}
	pii minv()
	{
//		puts("qmin:") ;
		while ( q3.size() && q4.size() ) 
		{
//			printf("{%d,%d} {%d,%d}\n" , q3.top().first , q3.top().second , q4.top().first , q4.top().second) ;
			if ( q3.top() != q4.top() ) return q3.top() ;
			q3.pop() , q4.pop() ;
		}
//		printf("{%d,%d}\n" , q3.top().first , q3.top().second) ;
		return q3.top() ;
	}
} q ;
bool check( int v )
{
//	printf("check:%d\n" , v) ;
	q.init() ; 
	for ( int i = 1 ; i <= m ; i++ ) q.push( { 0 , i } ) ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		if ( a[i] == 1 ) 
		{
			auto now = q.minv() ;
//			printf("now:%d %d\n" , now.first , now.second) ;
			q.del( now ) ;
			if ( now.first + 1 > v ) return false ;
			q.push( { now.first + 1 , now.second } ) ;
		}
		else
		{
			auto now = q.maxv() ;
			if ( now.first == 0 ) continue ;
			q.del( now ) ;
			q.push( { now.first - 1 , now.second } ) ; 
		}
	}
	return true ;
}
int ans[200005] ;
void solve()
{
	scanf("%d%d"  , &n , &m) ;
	for ( int i = 1 ; i <= n ; i++ ) a[i] = Read() ;
	int l = 0 , r = n ;
	while ( l < r )
	{
		int mid = ( l + r ) >> 1 ;
		if ( check( mid ) ) r = mid ;
		else l = mid + 1 ;
	}
	q.init() ; 
	for ( int i = 1 ; i <= m ; i++ ) q.push( { 0 , i } ) ;
	for ( int i = 1 ; i <= n ; i++ )
	{
		if ( a[i] == 1 ) 
		{
			auto now = q.minv() ;
//			printf("now:%d %d\n" , now.first , now.second) ;
			q.del( now ) ;
			print1( now.second ) ;
			putchar(' ') ; 
//			printf("%d " , now.second) ;
			q.push( { now.first + 1 , now.second } ) ;
		}
		else
		{
			auto now = q.maxv() ;
			print1( now.second ) ;
			putchar(' ') ;
//			printf("%d " , now.second) ;
			if ( now.first == 0 ) continue ;
			q.del( now ) ;
			q.push( { now.first - 1 , now.second } ) ; 
		}
	}
//	printf("%d\n" , l) ;
	puts("") ;
}

int main()
{
//	freopen("1.in" , "r" , stdin) ;
//	freopen("1.out" , "w" , stdout) ;
	int t ;
	scanf("%d" , &t) ;
	while ( t -- ) solve() ;
//	printf("%d %d\n" , cnt1 , cnt2) ;
	return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3844kb

input:

5
3 2
1 -1 1
4 2
-1 1 1 -1
7 3
1 1 1 1 1 1 1
10 3
1 1 1 1 -1 -1 1 1 1 1
12 4
1 1 1 1 -1 -1 -1 -1 1 1 1 1

output:

1 1 1 
2 1 2 2 
1 2 3 1 2 3 1 
1 2 3 1 1 3 3 1 2 3 
1 2 3 4 4 3 2 1 1 2 3 4 

result:

ok Correct (5 test cases)

Test #2:

score: 0
Accepted
time: 50ms
memory: 3632kb

input:

18434
10 1
-1 1 1 -1 -1 1 -1 -1 1 1
10 2
-1 -1 -1 1 1 -1 1 1 1 1
10 2
1 -1 -1 -1 -1 1 1 -1 1 1
10 7
1 1 -1 1 -1 1 1 -1 -1 1
9 1
-1 1 -1 1 1 -1 1 -1 1
8 1
-1 -1 -1 -1 1 1 -1 -1
10 3
-1 -1 -1 1 1 1 1 -1 -1 -1
9 1
1 -1 -1 1 -1 -1 -1 -1 -1
10 10
-1 1 1 1 1 1 1 1 1 1
10 4
-1 1 -1 1 -1 1 1 -1 1 1
9 3
1 1 ...

output:

1 1 1 1 1 1 1 1 1 1 
2 2 2 1 2 2 2 1 2 1 
1 1 2 2 2 1 2 2 2 1 
1 2 2 2 2 2 3 3 2 2 
1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 
3 3 3 1 2 3 1 1 3 2 
1 1 1 1 1 1 1 1 1 
10 1 2 3 4 5 6 7 8 9 
4 1 1 1 1 1 2 2 2 3 
1 2 2 1 1 1 1 1 3 
1 2 2 2 3 4 4 4 
7 1 2 3 4 5 6 7 7 7 
1 1 6 1 1 6 1 2 2 
1 1 1 1 1 1 1 1 1 
1 ...

result:

ok Correct (18434 test cases)

Test #3:

score: -100
Time Limit Exceeded

input:

1
199996 3
1 -1 1 1 1 1 -1 -1 -1 1 1 -1 1 -1 1 1 -1 -1 1 1 1 1 -1 1 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