zoukankan      html  css  js  c++  java
  • 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 k2 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×104), indicating the number of pearls. The second line contains a1,a2,...,an (1 ≤ ai ≤ 109) 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
    ",dp[t]);
    56       }
    57       return 0;
    58 }
     
     
  • 相关阅读:
    PostCSS: 跨浏览器兼容性
    js延迟执行函数
    angularjs怎么做动jq中toggle (ng-toggle in AngularJS )
    elasticsearch权威指南
    docker 运行 elasticsearch + kibana + head 集群
    RabbitMQ 的基本介绍
    docker 安装rabbitMQ
    Docker WARNING: IPv4 forwarding is disabled. Networking will not work.
    nginx 的 负载均衡
    Nginx 的 docker 部署
  • 原文地址:https://www.cnblogs.com/forgot93/p/3977124.html
Copyright © 2011-2022 走看看