You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
/*
14
3 2 2 3 2 2 3 2 2 2 3 3 3 3
*/
const int N = 110 ;
int n ;
int a [ N ] ;
int res ; // 执行次数
int st [ 10 ] ; // 哪些数字使用过了
int b [ 10 ] ;
// 结构体
struct Node {
int c ;
int count ;
int st , ed ;
const bool operator < ( const Node & t ) const {
if ( count ! = t . count ) return count > t . count ;
if ( st < t . st & & ed > t . ed ) return false ;
return true ;
}
} ;
// 找出未处理的数字中符合条件的那个
Node find ( ) {
memset ( b , 0 , sizeof b ) ;
vector < Node > q ;
for ( int i = 1 ; i < = n ; i + + )
if ( ! st [ a [ i ] ] ) b [ a [ i ] ] + + ; // 个数
for ( int i = 1 ; i < = 9 ; i + + ) { // 遍历所有数字1~9
if ( b [ i ] > 0 ) { // 如果是真实存在的, 有效的数字i
int start , end ; // 找出这个数字的起始位置与结束位置
for ( int j = 1 ; j < = n ; j + + ) // 找开头
if ( a [ j ] = = i ) {
start = j ;
break ;
}
for ( int j = n ; j > = 1 ; j - - ) // 找结尾
if ( a [ j ] = = i ) {
end = j ;
break ;
}
// 记录数字,个数,开始,结束
q . push_back ( { i , b [ i ] , start , end } ) ;
}
}
// 按结构体自定义的顺序进行排序
sort ( q . begin ( ) , q . end ( ) ) ;
// 我们只关心第一个,也就是数量最多的那个
if ( q . size ( ) ) return q [ 0 ] ;
return { 0 , 0 , 0 , 0 } ;
}
int main ( ) {
cin > > n ;
for ( int i = 1 ; i < = n ; i + + ) cin > > a [ i ] ; // 密码数组
for ( res = 0 ; ; res + + ) {
// 找出没有记录过的数字,并且,它的数量最多,如果存在数量一样多的多个数字,就比较谁的区间大,大的优先,如果无法比较谁的区间大,就数小的优先
Node x = find ( ) ;
// 将x这个数字标识为已处理
st [ x . c ] = 1 ;
if ( x . count = = 0 ) break ;
}
cout < < res < < endl ;
return 0 ;
}