HDU 5009 paint pearls (Xi 'an cyber game c) DP + discretization + Optimization

Source: Internet
Author: User

From: http://blog.csdn.net/accelerator_/article/details/39271751

Vomit blood ac...

11668627 22:15:24 Accepted 5009 1265 Ms 1980 K 2290 B G ++

Czy

 

 
Paint pearlsTime Limit: 4000/2000 MS (Java/others) memory limit: 65536/65536 K (Java/others) total submission (s): 1473 accepted submission (s): 466Problem 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 paintedTheir 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 shoshould 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 input31 3 3103 4 2 4 2 4 3 2 2 Sample output27 source 2014 ACM/ICPC Asia Regional Xi 'an online recommendhujie | we have carefully selected several similar problems for you: 5017 5016 5014 5013 5011

 

From: http://blog.csdn.net/accelerator_/article/details/39271751

Question: Given a target color, you can select an interval for dyeing. The cost of dyeing is the square of the number of different colors in the interval.

Idea: first pre-processing, merge a segment of the same color into a vertex, then discretize the color, and then perform DP and DP [I] to indicate the cost of dyeing to the position I, next, transfer the data. The transfer process records different numbers so that the data can be transferred. Note that if the answer is greater than DP [N], you do not need to transfer the data later.

 

Ah, the idea of DP is still very confusing. If you have time, you have to do this well...

 

  1 #include<iostream>  2 #include<cstring>  3 #include<cstdlib>  4 #include<cstdio>  5 #include<algorithm>  6 #include<cmath>  7 #include<queue>  8 #include<map>  9 #include<string> 10  11 #define N 50005 12 #define M 15 13 #define mod 10000007 14 #define p 10000007 15 #define mod2 100000000 16 #define ll long long 17 #define LL long long 18 #define maxi(a,b) (a)>(b)? (a) : (b) 19 #define mini(a,b) (a)<(b)? (a) : (b) 20  21 using namespace std; 22  23 int n,k,s; 24 int a[N]; 25 int b[N]; 26 map<int,int>c; 27 int vis[N]; 28 int dp[N]; 29 int cou; 30 vector<int>save; 31  32 void ini() 33 { 34     //memset(vis,0,sizeof(vis)); 35     memset(dp,0x3f3f3f3f,sizeof(dp)); 36     c.clear(); 37     k=0; 38     int i; 39     scanf("%d",&a[1]); 40     k=1; 41     b[1]=a[1]; 42     for(i=2;i<=n;i++){ 43        scanf("%d",&a[i]); 44        if(a[i]!=a[i-1]){ 45           k++; 46           b[k]=a[i]; 47        } 48     } 49     s=0; 50     for(i=1;i<=k;i++){ 51        if(c[ b[i] ]==0){ 52          // vis[ b[i] ]=1; 53           s++; 54           c[ b[i] ]=s; 55        } 56     } 57  58     for(i=1;i<=k;i++){ 59        b[i]=c[ b[i] ]; 60       // dp[i]=i; 61     } 62    // for(i=1;i<=k;i++){ 63    //    printf(" i=%d b=%d\n",i,b[i]); 64     //} 65  66 } 67  68 void solve() 69 { 70     int i,j; 71     dp[0]=0; 72     dp[k]=k; 73     for(i=0;i<k;i++){ 74         cou=0; 75        // vis[ b[i] ]=1; 76         //save.push_back(b[i]); 77         for(j=i+1;j<=k;j++){ 78            // if(cou*cou>=k) break; 79             if(vis[ b[j] ]==0 ){ 80                 vis[ b[j] ]=1; 81                 save.push_back(b[j]); 82                 cou++; 83             } 84             if (dp[i] + cou * cou >= dp[k]) break; 85            // printf("  i=%d j=%d dpj=%d cou=%d dp=%d ",i,j,dp[j],cou,dp[i]+cou*cou); 86             dp[j]=min(dp[j],dp[i]+cou*cou); 87            // printf("   dpj=%d\n",dp[j]); 88         } 89         for(vector<int>::iterator it=save.begin();it!=save.end();it++){ 90             vis[*it]=0; 91         } 92         save.clear(); 93     } 94 } 95  96 void out() 97 { 98     //for(int i=1;i<=k;i++){ 99     //    printf(" i=%d dp=%d\n",i,dp[i]);100     //}101     printf("%d\n",dp[k]);102 }103 104 int main()105 {106     //freopen("data.in","r",stdin);107     //freopen("data.out","w",stdout);108     //scanf("%d",&T);109     //for(int cnt=1;cnt<=T;cnt++)110    // while(T--)111     while(scanf("%d",&n)!=EOF)112     {113         ini();114         solve();115         out();116     }117 118     return 0;119 }

 

HDU 5009 paint pearls (Xi 'an cyber game c) DP + discretization + Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.