2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP:
题目
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.
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.
1 3 3
10
3 4 2 4 4 2 4 3 2 2
7
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<map>
#include<vector>
using namespace std;
#define N 55555
int b[N],a[N];
int c[N];
int num[N];
int tmp[N];
int dp[N];
vector<int> q;
int vis[N];
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
memset(dp,0x3f3f3f,sizeof(dp));
for (int i=;i<=n;i++)
scanf("%d",&a[i]);
int t=;
for (int i=;i<=n;i++)
if (b[t]!=a[i]) b[++t]=a[i];
for (int i=;i<=t;i++) c[i]=b[i];
sort(c+,c+t+);
int tt=;
for (int i=;i<=t;i++)
if (c[i]!=c[i-]) a[++tt]=c[i];
for (int i=;i<=t;i++) c[i]=lower_bound(a+,a+tt+,b[i])-a;//离散过程这里用STL处理,前面一段都是把数组离散一下 dp[t]=t;dp[]=;
memset(vis,,sizeof(vis)); for (int i=;i<t;i++)
{ for (int j=i+;j<=t;j++)
{
if (!vis[c[j]]) vis[c[j]]=,q.push_back(c[j]);
int k=q.size();
if (k*k+dp[i]>=dp[t]) break;
dp[j]=min(dp[j],dp[i]+k*k);//没有用long long ,因为超了LONG LONG 就break
} for (int j=;j<q.size();j++)
vis[q[j]]=;
q.clear();
}
printf("%d\n",dp[t]);
}
return ;
}
2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls的更多相关文章
- hdu 5016 点分治(2014 ACM/ICPC Asia Regional Xi'an Online)
Mart Master II Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 2014 ACM/ICPC Asia Regional Xi'an Online
03 hdu5009 状态转移方程很好想,dp[i] = min(dp[j]+o[j~i]^2,dp[i]) ,o[j~i]表示从j到i颜色的种数. 普通的O(n*n)是会超时的,可以想到o[]最大为 ...
- HDU 5010 Get the Nut(2014 ACM/ICPC Asia Regional Xi'an Online)
思路:广搜, 因为空格加上动物最多只有32个那么对这32个进行编号,就能可以用一个数字来表示状态了,因为只有 ‘P’ 'S' 'M' '.' 那么就可以用4进制刚好可以用64位表示. 接下去每次就 ...
- 2014 ACM/ICPC Asia Regional Xi'an Online(HDU 5007 ~ HDU 5017)
题目链接 A题:(字符串查找,水题) 题意 :输入字符串,如果字符串中包含“ Apple”, “iPhone”, “iPod”, “iPad” 就输出 “MAI MAI MAI!”,如果出现 “Son ...
- HDU 5000 2014 ACM/ICPC Asia Regional Anshan Online DP
Clone Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) Total Submiss ...
- HDU 5029 Relief grain(离线+线段树+启发式合并)(2014 ACM/ICPC Asia Regional Guangzhou Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5029 Problem Description The soil is cracking up beca ...
- HDU 5000 Clone(离散数学+DP)(2014 ACM/ICPC Asia Regional Anshan Online)
Problem Description After eating food from Chernobyl, DRD got a super power: he could clone himself ...
- 2014 ACM/ICPC Asia Regional Shanghai Online
Tree http://acm.hdu.edu.cn/showproblem.php?pid=5044 树链剖分,区间更新的时候要用on的左++右--的标记方法,要手动扩栈,用c++交,综合以上的条件 ...
- 2014 ACM/ICPC Asia Regional Guangzhou Online
Wang Xifeng's Little Plot http://acm.hdu.edu.cn/showproblem.php?pid=5024 预处理出每个点八个方向能走的最远距离,然后枚举起点,枚 ...
随机推荐
- 使用WIF实现单点登录Part II —— Windows Identity Foundation基本原理
在上一篇文章中,我们已经使用WIF构建了一个基于MVC4的简单的身份验证程序,在这篇文章里,我们将探讨一下到底什么是WIF,以及它的工作原理.然后在下一篇文章开始,我们将实际操作,实现单点登录功能. ...
- 实战Django:官方实例Part1
[写在前面] 撰写这个实战系列的Django文章,是很久之前就有的想法,问题是手头实例太少,一旦开讲,恐有"无米下锅"之忧. 随着对Django学习的深入,渐渐有了些心得,把这些心 ...
- StyleCop学习笔记——自定义规则
本文将简单的一步一步的指导这可能有助于学习如何创建自己的规则 1.创建一个项目. Visual Studio创建一个新的类库项目.NET3.5 2.引用两个DLL,StyleCop.dll和Style ...
- DrawerLayout带有侧滑功能的布局类(1)
DrawerLayout: DrawerLayout顾名思义就是一个管理布局的.使用方式可以与其它的布局类类似. DrawerLayout带有滑动的功能.只要按照drawerLayout的规定布局方式 ...
- Linux开机启动程序详解
Linux开机启动程序详解我们假设大家已经熟悉其它操作系统的引导过程,了解硬件的自检引导步骤,就只从Linux操作系统的引导加载程序(对个人电脑而言通常是LILO)开始,介绍Linux开机引导的步骤. ...
- iOS App Launch Option
iOS 程序启动时总会调用application:didFinishLaunchingWithOptions:,其中第二个参数launchOptions为NSDictionary类型的对象,里面存储有 ...
- 11.Warning (332060): Node: pi_fck3p was determined to be a clock but was found without an associated clock assignment.
解释及措施:(1):这个信号是不是你期望的时钟信号?还是被综合器误将普通信号综合成了时钟信号?有没有在代码中用过这个信号的上升沿/下降沿? (2):如果是期望的时钟信号,那么是否有可能调整管脚位置约束 ...
- QWidget设置为模态问题
设置QWidget的Qt::WindowModality属性为Qt::WindowModal和Qt::ApplicationModal,发现窗体仍然不会模态,网上查了一下,有人说改属性只对window ...
- Unity3D定制新建C#文件的头描述
1. 修改模板内容如下: MAC:Unity.app/Contents/Resources/ScriptTemplates/81-C# Script-NewBehaviourScript.cs.txt ...
- 寻找idea...
域名:tianhuangdilao.com 天荒地老 现在闲置,寻求好的idea...