标签:io os ar for 数据 sp on c amp
西安网络赛C题。先对大数据离散化,dp优化
#include<iostream> //G++ #include<vector> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<cmath> using namespace std; const int maxn=51234; struct kind { int id; int val; }; int vis[maxn]; kind newa[maxn]; int n;int a[maxn]; int dp[maxn]; bool myval(kind a,kind b) { return a.val<b.val; } bool myid(kind a,kind b) { return a.id<b.id; } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) scanf("%d",&a[i]); int nn=0; a[n+1]=-1; for(int i=1;i<=n;i++) { if(a[i]!=a[i+1]) { newa[++nn].val=a[i]; newa[nn].id=nn; } } n=nn; sort(newa+1,newa+n+1,myval); int cnt=0; newa[n+1].val=-1; for(int i=1;i<=n;i++) { if(newa[i].val!=newa[i+1].val) newa[i].val=cnt++; else newa[i].val=cnt; } sort(newa+1,newa+1+n,myid); for(int i=0;i<=n;i++) { dp[i]=i; } vector<int>hasvis; for(int i=0;i<n;i++) { if(dp[i]>=dp[i+1])continue; cnt=0; int ii=i; for(int j=i+1;j<=n;j++) { if(vis[newa[j].val]==0) { cnt++; hasvis.push_back(newa[j].val); vis[newa[j].val]++; } if(dp[i]+cnt*cnt>=dp[n])break; if(dp[j]>dp[i]+cnt*cnt) { dp[j]=dp[i]+cnt*cnt; ii=j; } } for(int k=0;k<hasvis.size();k++) vis[hasvis[k]]=0; hasvis.clear(); if(ii!=i) { i=ii-1; } } printf("%d\n",dp[n]); } }
标签:io os ar for 数据 sp on c amp
原文地址:http://blog.csdn.net/u011498819/article/details/39296647