General

# Author Problem Lang Verdict Time Memory Sent Judged
31025909 Contestant:
anonymousbuggy
868F - 8 GNU C++14 Time limit exceeded on pretest 8 2000 ms 20356 KB 2017-10-05 12:18:07 2017-10-05 12:18:07

→ Source
#include <iostream>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <string>
#include <climits>
#include <cmath>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <iomanip>

using namespace std;

int a[100005];
long long int dp[100005][25], inf = 100000000000000000;

void compute(int k, int l, int r, int optL, int optR){
if(l > r)return;
int m = (l+r)/2;
int cup = optL;
int cap = min(optR, m-1);
map<int,int> arr;
long long int tmp = 0;
int besti = -1;
long long best = inf;
for(int i = m; i > cap + 1; i--){
tmp += arr[a[i]];
arr[a[i]]++;
}
for(int i = cap; i >= cup; i--){
tmp += arr[a[i+1]];
if(dp[i][k-1] + tmp <= best){
besti = i;
best = dp[i][k-1] + tmp;
}
arr[a[i+1]]++;
}
//cout<<l<<" "<<r<<" "<<optL<<" "<<optR<<" "<<besti<<endl;
dp[m][k] = best;
if(besti != -1){
if(l <= m-1)compute(k, l, m-1, optL, besti);
if(r >= m+1)compute(k, m+1, r, besti, optR);
}
}

int main(){
std::ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin>>n>>k;
for(int i = 1; i <= n; i++){
cin>>a[i];
}
map<int,int> arr;
dp[1][1] = 0;
arr[a[1]]++;
for(int i = 2; i <= n; i++){
dp[i][1] = dp[i-1][1] + arr[a[i]];
arr[a[i]]++;
}
for(int i = 2; i <= k; i++){
compute(i, 1, n, 1, n);
}
/*for(int i = 1; i <= k; i++){
for(int j = 1; j <= n; j++){
cout<<dp[j][i]<<" ";
}
cout<<endl;
}*/
cout<<dp[n][k]<<endl;
}


?
Time: ? ms, memory: ? KB
Verdict: ?
Input
?
Participant's output
?
?
Checker comment
?
Diagnostics
?
Click to see test details