HDU 5009 DP+双端链表
2015-08-30 12:47
375 查看
Paint Pearls
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
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
西汉网络赛的题,dp思路好想,主要是对dp的优化上,这里用了一个双端链表,再求dp[i]时,链表里只会存不同的元素,相同的元素会被删除。然后只需沿着链表走就能知道前j个的最优解。
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2655 Accepted Submission(s): 863
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
西汉网络赛的题,dp思路好想,主要是对dp的优化上,这里用了一个双端链表,再求dp[i]时,链表里只会存不同的元素,相同的元素会被删除。然后只需沿着链表走就能知道前j个的最优解。
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <map> #include <algorithm> #include <cmath> using namespace std; int a[50010]; int pre[50010]; int nxt[50010]; int dp[50010]; int main() { // freopen("data.in","r",stdin); int n; while(scanf("%d",&n)!=EOF){ int i,j,k = 2; int N = n; scanf("%d",&a[1]); pre[0] = -1; pre[1] = 0; nxt[1] = 2; for(i = 2;i<=n;i++){ int tem; scanf("%d",&tem); if(tem != a[k-1]){ a[k] = tem; pre[k] = k-1; nxt[k] = k+1; k++; } else N--; } memset(dp,0x3f,sizeof(dp)); dp[0] = 0; map<int ,int >G; for(i = 1;i<=N;i++){ if(!G.count(a[i])) G[a[i]] = i; else{ int id = G[a[i]]; nxt[pre[id]] = nxt[id]; pre[nxt[id]] = pre[id]; G[a[i]] = i; } int cnt = 0; for(j = pre[i];j!=-1;j = pre[j]){ cnt++; dp[i] = min(dp[i],dp[j]+cnt*cnt); if(cnt*cnt>i) break; } } printf("%d\n",dp ); } return 0; }
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow
- zoj3605 Find the Marble(三维dp)
- Word Break I,II, Triangle,Palindrome Partitioning 动态规划 DP