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的更多相关文章

  1. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

  3. HDU 5009 Paint Pearls(西安网络赛C题) dp+离散化+优化

    转自:http://blog.csdn.net/accelerator_/article/details/39271751 吐血ac... 11668627 2014-09-16 22:15:24 A ...

  4. HDU 5009 Paint Pearls 双向链表优化DP

    Paint Pearls Problem Description   Lee has a string of n pearls. In the beginning, all the pearls ha ...

  5. HDU - 5009 Paint Pearls(dp+优化双向链表)

    Problem Description Lee has a string of n pearls. In the beginning, all the pearls have no color. He ...

  6. HDU 5009

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 题意:一个数列,每个点代表一种颜色,每次选一个区间覆盖,覆盖的代价是区间内颜色种类数的平方,直到覆盖整个数 ...

  7. hdu 5009 离散化

    http://acm.hdu.edu.cn/showproblem.php?pid=5009 有一段序列,涂连续一段子序列的代价为该子序列出现不同数字个数的平方,求最小代价涂完整个序列. ai有10^ ...

  8. HDU 1069 dp最长递增子序列

    B - Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. HDU 1160 DP最长子序列

    G - FatMouse's Speed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. poj 1077 Eight(双向bfs)

    题目链接:http://poj.org/problem?id=1077 思路分析:题目要求在找出最短的移动路径,使得从给定的状态到达最终状态. <1>搜索算法选择:由于需要找出最短的移动路 ...

  2. Struts2 学习笔记19 类型转换 Part1

    现在来说一说类型转换,提到类型转换其实我们之前早已经用过了,在url传递参数的时候,我们传递过来的参数其实都是String类型的,在显示的时候都自动转换了,像这种简单的转换很好理解,我们要说的是,转换 ...

  3. iOS键盘遮挡问题解决办法

    iOS开发之“键盘遮挡输入框的解决办法”之一 -----键盘通知之前处理这种问题,总是在触发输入框编辑事件键盘弹出的时候,将当前的View整体向上移动,结束编辑又整体向下移,耗时耗力效率低. 在网上看 ...

  4. Sql缓存依赖--数据库缓存

    •依赖于文件内容CacheDependency cDep = new CacheDependency(filePath); •依赖于数据库内容(轮询机制/通知机制)一:轮询机制 1.在数据库新建版本表 ...

  5. RSA, ACS5.X 集成配置

    目的是RSA和ACS集成,ACS作为RADIUS服务器提供二次验证服务. ①配置RSA SecurID Token Servers   按照如下网址配置: http://www.cisco.com/c ...

  6. 深入浅出—JAVA(6)

    6.认识JAVA的API Arraylist的操作

  7. elk 索引

    zjtest7-redis:/usr/local/logstash-2.3.4/config# cat logstash_agent.conf input { file { type => &q ...

  8. 为了肾六(dp)

    为了肾六 时间限制:4000 ms  |  内存限制:210535 KB 难度:2   描述 最近肾六很流行,goshawk看身边的朋友都用上了apple.自己还用着W年前的Samsung.于是决定去 ...

  9. UItexfile实时验证输入字符

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  10. Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性

    CSDN 对格式支持比較弱.能够到http://user.qzone.qq.com/88285879/blog/1399382878 看一致的内容. Performance Tuning Guide  ...