HDU5009西安网赛2014


HDU5009

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define PB push_back
#define MP make_pair

typedef double DB;
typedef long long LL;
typedef pair<int, int> PI;
typedef pair<string, string> PS;

const DB eps = 1e-8;
const int N = 5e4 + 7;
const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;


map <int, int> S;
int last[N], next[N], a[N], now[N], f[N];
int main(){
    int n;
    while (scanf("%d", &n) == 1){
        S.clear();
        for (int i = 1; i <= n; i++){
            scanf("%d", &a[i]);
            if (S[a[i]]) last[i] = S[a[i]]; else last[i] = 0;
            S[a[i]] = i;
        }
        S.clear();
        for (int i = n; i >= 1; i--){
            if (S[a[i]]) next[i] = S[a[i]]; else next[i] = n + 1;
            S[a[i]] = i;
        }
        f[0] = 0;
        memset(now, 0, sizeof(now));
        for (int i = 1; i <= n; i++){
            f[i] = i;
            for (int j = 1; j * j <= i; j++){
                if (!now[j]){
                    S.clear();
                    int cnt = 0;
                    for (int k = i; k >= 1; k--){
                        if (!S[a[k]]) cnt++;
                        if (cnt > j){
                            now[j] = k + 1;
                            break;
                        }
                        S[a[k]]++;
                    }
                    if (!now[j]) now[j] = 1;
                    f[i] = min(f[i], f[now[j] - 1] + j * j);
                    continue;
                }
                if (last[i] < now[j]){
                    while (next[now[j]] < i) now[j]++;
                    now[j]++;
                }
                f[i] = min(f[i], f[now[j] - 1] + j * j);
            }
        }
        printf("%d\n", f[n]);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值