警钟肘烂
查看原帖
警钟肘烂
476081
Jeff_赵楼主2024/11/24 15:28

看题时千万不要先想贪心!!!

想了贪心也要立马证明正确性!!!

不然你就会写出这种代码:

#include<bits/stdc++.h>

using namespace std ; 

const int N = 6e6 + 10 ;  
long long dp[ N ][ 10 ] ; 
int dish[ N ] ; 
long long ans = 0 ; 
int T , n ; 

bool cmp( int &a , int &b )
{
	return a < b ; 
}

int main()
{
	
	cin >> T ;
	while( T -- )
	{
		cin >> n ; 
		ans = 0 ; 
		for( int i = 1 ; i <= n ; i ++ )
			scanf("%d" , &dish[ i ] ) ; 
		
		sort( dish + 1 , dish + 1 + n , cmp ) ; 
//		memset( dp i, 0 , sizeof dp ) ; 
		int tot = 0 ; 
		
//		ans += dish[ 1 ] / 4 ; 
//		dish[ 1 ] = dish[ 1 ] % 4 ; 
//		if( dish[ 1 ] >= 2 ) ans += 1 , dish[ 1 ] -= 2 ;
//		tot = 1;  
//		
		for( int i = 2 ; i <= n ; i ++ )
		{
			while( tot < i && dish[ tot ] == 0 ) tot ++ ; 
			
			while( dish[ i ] >= 3 && tot < i ) 
			{
				while( tot < i && dish[ tot ] == 0 ) tot ++ ; 
				ans ++ ; 
				dish[ i ] -= 3 ; 
				dish[ tot ] -= 1 ; 
				if( dish[ tot ] == 0 ) tot ++ ; 
			}
			
//			if( dish[ i ] >= 4 ) ans += dish[ i ] / 4 , dish[ i ] = dish[ i ] % 4 ; 
//			
//			if( dish[ i ] == 2 || dish[ i ] == 3 ) ans += 1 , dish[ i ] -= 2 ; 
//			else if( dish[ i ] == 1 ) continue ;   
		}
		
		while( tot <= n ) 
		{
			if( dish[ tot ] >= 4 ) ans += dish[ tot ] / 4 , dish[ tot ] = dish[ tot ] % 4 ; 
			
			if( dish[ tot ] == 2 || dish[ tot ] == 3 ) ans += 1 , dish[ tot ] -= 2 ; 
			else if( dish[ tot ] == 1 ) ans += 1 , dish[ tot ] -- ;  
			tot ++ ; 
		}
			
		
		cout << ans << endl ; 
	}
	
	return 0 ; 
}

喜提20分!

2024/11/24 15:28
加载中...