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 2762 Going from u to v or from v to u? (Tarjan) - from lanshui_Yang

    Description In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has ...

  2. Drying(贪心)

    Drying Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11512   Accepted: 2977 Descripti ...

  3. [置顶] Asp.Net底层原理(一、浏览器和服务器的交互原理)

    …… 一.浏览器和服务器的交互原理 二.写自己的"迷你"Asp.net框架 三.Asp.Net的请求与响应过程 1.在此之前,首先简单的模拟一下我们去请求一个网址的时候,浏览器和服 ...

  4. Objective-c 类接口 (@interface) (类定义)

    在Objective-c中如何定义一个类呢?我们可以使用下面的格式进行表示: @interface 类名:父类名{ 变量定义; } 方法定义: @end; 下面给出一个实例: @interface P ...

  5. linux下TUN/TAP虚拟网卡的使用

    转载:http://wushank.blog.51cto.com/3489095/1306849 tun/tap 驱动程序实现了虚拟网卡的功能,tun表示虚拟的是点对点设备,tap表示虚拟的是以太网设 ...

  6. xampp中mysql设置密码

    发现网上的解决办法都比较过时.嗯,解决办法很简单. 打开浏览器localhost:[port]/phpmyadmin/ 点击用户账户选项 选择用户名为root,Host name为localhost也 ...

  7. C#编辑基础笔记

    目录 1.     .NET .NET Framework是一种多语言的平台,一种技术. 而c#是基于其上面的一种语言.    1 2.     Winform 桌面应用程序[从.net平台上面开发的 ...

  8. 解决了clang: error: linker command failed with exit code 1 (use -v to see invocation) 解决方法

    1.”Build Settings”->”Enable Bitcode”设置为NO 2.TARGETS -->  Build Settings --> Architectures - ...

  9. ansible变量

    ansible变量 (部分内容摘自互联网,非官方,描述不一定正确) 变量种类: 内置变量facts: 自定义变量: 命令行传递的变量 roles传递的变量 主机变量 组变量 内置变量facts: 由远 ...

  10. dubbo 服务化

    当网站变大后,不可避免的需要拆分应用进行服务化,以提高开发效率,调优性能,节省关键竞争资源等. 当服务越来越多时,服务的URL地址信息就会爆炸式增长,配置管理变得非常困难,F5硬件负载均衡器的单点压力 ...