HDU 5009 DP
2014 ACM/ICPC
Asia Regional Xi'an Online
对于N个数 n(1 ≤ n ≤ 5×104),
把N个数分成随意个区间,每一个区间的值是该区间内不同数字个数的平方和,答案使和最小
DP思路,首先要对数据合并相连同样数字,然后离散化。
数据太弱了。。。。
。
然后直接做N*N的DP居然能AC。
。
。。比赛时候想到了。。
。不敢写。
。。
G++AC C++TLE
#include "stdio.h"
#include "string.h"
#include "queue"
#include "iostream"
#include "algorithm"
using namespace std; const int inf = 0x3f3f3f3f; struct node
{
int x,id,v;
}a[50000];
int dp[50010],vis[50010];
vector<int>q;
bool cmpx(node a,node b)
{
return a.x<b.x;
} bool cmpid(node a,node b)
{
return a.id<b.id;
} int Min(int a,int b)
{
if (a<b) return a;
else return b;
} int main()
{
int n,m,i,j,temp,x,cnt;
while (scanf("%d",&n)!=EOF)
{
m=1;
scanf("%d",&a[1].x);
a[1].id=1;
for (i=2;i<=n;i++)
{
scanf("%d",&x);
if (x!=a[m].x)
{
a[++m].x=x;
a[m].id=m;
}
} // 合并相邻同样数字
n=m;
sort(a+1,a+1+n,cmpx);
temp=1;
a[1].v=1;
for (i=2;i<=n;i++)
{
if (a[i].x!=a[i-1].x) temp++;
a[i].v=temp;
} // 离散化 sort(a+1,a+1+n,cmpid); memset(dp,inf,sizeof(dp));
dp[0]=0;
dp[n]=n;
memset(vis,0,sizeof(vis));
for (i=0;i<n;i++)
{
if (dp[i]>dp[i+1]) continue;
cnt=0;
for (j=i+1;j<=n;j++)
{
if (vis[a[j].v]==0)
{
cnt++;
q.push_back(a[j].v);
vis[a[j].v]=1;
}
if (dp[i]+cnt*cnt>=dp[n]) break; // 小优化,大于DP[n] 直接退出
dp[j]=Min(dp[j],dp[i]+cnt*cnt);
}
for (j=0;j<q.size();j++)
vis[q[j]]=0;
q.clear();
}
printf("%d\n",dp[n]);
}
return 0;
}
HDU 5009 DP的更多相关文章
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
- HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化
转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...
- HDU 5009 Paint Pearls 双向链表优化DP
Paint Pearls Problem Description Lee has a string of n pearls. In the beginning, all the pearls ha ...
- HDU - 5009 Paint Pearls(dp+优化双向链表)
Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...
- HDU 5009
http://acm.hdu.edu.cn/showproblem.php?pid=5009 题意:一个数列,每个点代表一种颜色,每次选一个区间覆盖,覆盖的代价是区间内颜色种类数的平方,直到覆盖整个数 ...
- hdu 5009 离散化
http://acm.hdu.edu.cn/showproblem.php?pid=5009 有一段序列,涂连续一段子序列的代价为该子序列出现不同数字个数的平方,求最小代价涂完整个序列. ai有10^ ...
- HDU 1069 dp最长递增子序列
B - Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- HDU 1160 DP最长子序列
G - FatMouse's Speed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- Flex XML搜索、匹配
- <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx=" ...
- makefile 里面 := 和 = 的区别
此文版权属于作者所有,任何人.媒体或者网站转载.借用都必须征得作者本人同意! 参考:What is the colon equals sign ( := ) in makefiles? Makefil ...
- [置顶] js综合应用:表格的四则运算
在做调查问卷的过程中,遇到一个表格的统计问题,算是需要些js方面的综合知识,所以记录下来. 在上次完成了基本的求和的基础上,添加了基本的加减乘除四则运算. 基本需求简化后如下: 对应的htm了为: & ...
- 使用LINQ的几个小技巧
这里总结了这些技巧.介绍如何使用LINQ来: 初始化数组 在一个循环中遍历多个数组 生成随机序列 生成字符串 转换序列或集合 把值转换为长度为1的序列 遍历序列的所有子集 如果你在LINQ方面有心得也 ...
- 密封关键字sealed
在两种情况下使用: ·不想让别人继承:例如public sealed class Person{}; ·不想让子类重写自己的方法 例如: public class Person{ public vis ...
- js常用几种类方法实现
js定义类方法的常用几种定义 1 定义方法,方法中包含实现 function createCORSRequest() { var xhr = new XMLHttpRequest(); xhr.onl ...
- openrisc 之 Wishbone总线学习笔记——接口信号定义
这部分内容就是copy下来的,网上到处都有.先看看接口啥样子,在详细说明 接口定义copy http://blog.csdn.net/ce123/article/details/6929897.百度文 ...
- 在 Windows Azure 网站 (WAWS) 上对 Orchard CMS 使用 Azure 缓存
编辑人员注释: 本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 撰写. 如果您当前的 OrchardCMS 网站在 Windows Azure 网站 ...
- Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64044 Accepted: 2471 ...
- 1354 - IP Checking(水题)
1354 - IP Checking PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB An I ...