2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls

传说的SB DP:

题目

Problem Description
Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans to color the pearls to make it more fascinating. He drew his ideal pattern of the string on a paper and asks for your help. 

In each operation, he selects some continuous pearls and all these pearls will be painted to  their target colors. When he paints a string which has k different target colors, Lee will cost k 2 points. 

Now, Lee wants to cost as few as possible to get his ideal string. You should tell him the minimal cost.
 

 

Input
There are multiple test cases. Please process till EOF.

For each test case, the first line contains an integer n(1 ≤ n ≤ 5×10 4), indicating the number of pearls. The second line contains a 1,a 2,...,a n (1 ≤ a i ≤ 10 9) indicating the target color of each pearl.
 

 

Output
For each test case, output the minimal cost in a line.
 

 

Sample Input
3 1 3 3 10 3 4 2 4 4 2 4 3 2 2
 

 

Sample Output
2 7
 
这道题很容易想到方程,但是TLE居多,下面讲讲我的优化!
首先DP方程:DP[J]=MIN(DP[J],DP[I]+SX[I+1,J]^2); I<=J<=N; SX[I,J];表示I-J中有多少个颜色不一样的
我们知道dp[j]最大为J;
所以有假如我们算到DP[I]>=DP[N];那么就可BREAK;
有了这个数据题目就作对了一般,但是遇到全为一样数的数组时,效率不够高
我们需要离散一下。
注意:这份代码在C++ 是TLE ,在G++ 是1S;
对于编译器我无力:
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<math.h>
 5 #include<string>
 6 #include<map>
 7 #include<vector>
 8 using namespace std;
 9 #define N 55555
10 int b[N],a[N];
11 int c[N];
12 int num[N];
13 int tmp[N];
14 int dp[N];
15 vector<int> q;
16 int vis[N];
17 int main()
18 {
19     int n;
20     while  (scanf("%d",&n)!=EOF)
21     {
22          memset(dp,0x3f3f3f,sizeof(dp));
23          for (int i=1;i<=n;i++)
24          scanf("%d",&a[i]);
25          int  t=0;
26          for (int i=1;i<=n;i++)
27          if  (b[t]!=a[i]) b[++t]=a[i];
28          for (int i=1;i<=t;i++) c[i]=b[i];
29          sort(c+1,c+t+1);
30          int tt=0;
31          for (int i=1;i<=t;i++)
32          if (c[i]!=c[i-1])  a[++tt]=c[i];
33          for (int i=1;i<=t;i++)  c[i]=lower_bound(a+1,a+tt+1,b[i])-a;//离散过程这里用STL处理,前面一段都是把数组离散一下
34          
35          
36          dp[t]=t;dp[0]=0;
37          memset(vis,0,sizeof(vis));
38 
39          for (int i=0;i<t;i++)
40          {
41 
42             for (int j=i+1;j<=t;j++)
43             {
44                    if (!vis[c[j]])  vis[c[j]]=1,q.push_back(c[j]);
45                    int k=q.size();
46                    if (k*k+dp[i]>=dp[t]) break;
48                    dp[j]=min(dp[j],dp[i]+k*k);//没有用long long ,因为超了LONG LONG 就break
49                    }
50 
51                 for (int j=0;j<q.size();j++)
52                  vis[q[j]]=0;
53                  q.clear();
54          }
55          printf("%d\n",dp[t]);
56       }
57       return 0;
58 }

 

 
 

转载于:https://www.cnblogs.com/forgot93/p/3977124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值