ZROI#985

ZROI#985

暴力就不说了,说说正解吧.
先假定每个区间都没有重复元素,然后得到一个全集的答案.
然后我们考虑,减掉不合法的方案.
记录每种颜色出现的位置,乘法原理即可.
暴力(Code:)

#include <iostream>
#include <cstdlib>
#include <cstdio>
#define rint read<int>
#define int long long

template < 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
" , ans ) ;
    return 0 ;
}

此正解思路与上述思路略有不同.
此代码思路是直接统计.
正解(Code:)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define one first
#define two second
#define rint read<int>
#define int long long
#define pb push_back

using std::queue ;
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;

template < 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 pre[N] , last[N] , n , v[N] , ans ;

signed main (int argc , char * argv[]) {
    n = rint () ; rep ( i , 1 , n ) v[i] = rint () ;
    rep ( i , 1 , n ) { pre[i] = last[v[i]] ; last[v[i]] = i ; }
    rep ( i , 1 , n ) ans += ( i - pre[i] ) * ( n - i + 1 ) ;
    printf ("%lld
" , ans ) ;
    return 0 ;
}
May you return with a young heart after years of fighting.
原文地址:https://www.cnblogs.com/Equinox-Flower/p/11523838.html