暴力就不说了,说说正解吧.
先假定每个区间都没有重复元素,然后得到一个全集的答案.
然后我们考虑,减掉不合法的方案.
记录每种颜色出现的位置,乘法原理即可.
暴力
\(Code:\) #include #include #include #define rint read #define int long longtemplate < class T > inline T read () { T x = 0 , f = 1 ; char ch = getchar () ; while ( ch < '0' || ch > '9' ) { if ( ch == '-' ) f = - 1 ; ch = getchar () ; } while ( ch >= '0' && ch <= '9' ) { x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ; ch = getchar () ; } return f * x ; }const int N = 1e6 + 100 ;int n , v[N] , ans ;bool mk[3005] ;signed main () { n = rint () ; for (int i = 1 ; i <= n ; ++ i) v[i] = rint () ; for (int i = 1 ; i <= n ; ++ i) for (int j = 1 ; j <= i ; ++ j) { for (int k = 1 ; k <= n ; ++ k) mk[k] = false ; for (int k = j ; k <= i ; ++ k) if ( ! mk[v[k]] ) ++ ans , mk[v[k]] = true ; } printf ("%lld\n" , ans ) ; return 0 ;}
此正解思路与上述思路略有不同.
此代码思路是直接统计.
正解
\(Code:\) #include #include #include #include #include #include #include #include #include #include #include