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 ...
随机推荐
- HDU 2178 猜数字
题解:设猜到的最大的数是h,在1到h间,你最多只要猜log2(h)+1(取整)次,所以易知==>h=2^m-1.即猜m次,能猜到的最大的数为2^m-1. #include <cstdio& ...
- hpu校赛--雪人的高度(离散化线段树)
1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...
- poj 2263&& zoj1952 floyd
Fiber Network Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2725 Accepted: 1252 Des ...
- Ext JS学习第十七天 事件机制event(二)
此文仅有继续学习笔记: 昨天说了三种邦定事件的方法,今天说一下自定义事件 假设现在又这样的情景一个自定义的事件 没有用到事件处理的场景 母亲问孩子和不饿-> ...
- sql 和 nosql 说明
在传统的数据库中, 数据库的格式是由表(table).行(row).字段(field)组成的.表有固定的结构,规定了每行有哪些字段,在创建时被定义,之后修改很困难.行的格式是相同的,由若干个固定的字段 ...
- C# 模拟提交带附件(input type=file)的表单
今天调用某API时,对于文档中的传入参数:File[] 类型,感觉很陌生,无从下手! 按通常的方式在json参数中加入file的二进制数据提交,一直报错(参数错误)!后来经过多方咨询,是要换一种 表单 ...
- 项目中js调用service和procedure的办法
Ajax.js /**通用ajax服务的定义对象 * services可以是单个服务对象,也可以是service服务数组 * 具体服务的定义请参考appendServices成员函数 */ funct ...
- C# DES对称加密解密
/// <summary> /// 加密 /// </summary> /// <param name="str"></param> ...
- github每次push都需要密码以及用户名的解决办法
git remote set-url origin git@github.com:你的账户/项目名称.git就可以直接git push origin master了.
- 无法更新 EntitySet“GuigeInfo”,因为它有一个 DefiningQuery,而 <ModificationFunctionMapping> 元素中没有支持当前操作的 <InsertFunction> 元素。
1:实体中必须有主键 2:删除创建的模型重新创建