http://codeforces.com/problemset/problem/229/D

题意:有n(1<=n<=5,000)座塔排在一条直线上,从左到右每个塔的高度分别为hi(1<=hi<=100,000),每次操作你可以选择一座塔(假设是第i座),用吊车把它吊起来,然后放到与它相邻的一座塔上(可以是第i-1座也可以是第i+1座),这样,新塔的高度为两座塔的和,完成操作后,塔的总数减少一座。问最少需要多少次操作可以使得所有的塔从左到右形成一个非递减序列。

思路:

我们可以这样设计dp:f[i][j]=min(f[j-1][k]+i-j-1)这是n^3的转移

我们发现如果以某个位置为末尾,那么一定是当前"块"里面"块"的高度最小的时候就是最优解,所以我们的转移从n^2变成了n

而枚举状态的效率是O(n),总的就是O(n^2)

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
int n,a[],sum[];
int mn[],pd[],num[];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
int main(){
n=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=n;i++) sum[i]=sum[i-]+a[i];
for (int i=;i<=n;i++){
if (i==){
mn[]=;
num[]=a[];
continue;
}
bool flag=;mn[i]=0x3f3f3f3f;
for (int j=i;j>=;j--){
if (num[j-]<=sum[i]-sum[j-]&&mn[i]>mn[j-]+i-j){
mn[i]=mn[j-]+i-j;
flag=;
num[i]=sum[i]-sum[j-];
}
}
if (flag) continue;
mn[i]=i-;
num[i]=sum[i];
}
return ;
}

Codeforces 229D Towers的更多相关文章

  1. Towers CodeForces - 229D

    The city of D consists of n towers, built consecutively on a straight line. The height of the tower ...

  2. codeforces 479B Towers 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...

  3. Codeforces 479B. Towers 暴力

    纯暴力..... B. Towers time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  4. nyoj 1216——整理图书 CF 229D—— Towers——————【dp+贪心】

    整理图书 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 小明是图书鹳狸猿,他有很多很多的书堆在了一起摆在了架子上,每摞书是横着放的,而且每摞书是订好的 是一个整体, ...

  5. Codeforces 626C Block Towers(二分)

    C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...

  6. Codeforces Beta Round #37 A. Towers 水题

    A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...

  7. Codeforces 478D Red-Green Towers

    http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...

  8. Codeforces Round #274 (Div. 2) B. Towers

    As you know, all the kids in Berland love playing with cubes. Little Petya has n towers consisting o ...

  9. Codeforces 739C Alyona and towers 线段树

    Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...

随机推荐

  1. SQL使用单引号

    SQL> select 'xxxx'oooo' from dual; ERROR: ORA-01756: quoted string not properly terminated SQL> ...

  2. js深入研究之自定义混合Mixin函数

    <script type="text/javascript"> /* 增加函数 */ function augment(receivingClass, givingCl ...

  3. struct2(一)第一个struct程序

    说明:本系列是针对struct2学习过程,主要的目的: 1. 探索针对一个新的开源框架的学习过程. 2. 学习struct2,学习官方对struct2介绍的方法. 3.别把英语忘了. 1. 为了更加清 ...

  4. poj 1466 Girls and Boys(二分匹配之最大独立集)

    Description In the second year of the university somebody started a study on the romantic relations ...

  5. (转)iOS7界面设计规范(5) - UI基础 - 导航

    通过分页控件(page control)来暗示多个条目或内容视图的存在.该控件可以有效的向用户展示内容单元的数量,以及当前所见的这一条在队列中的位置.请参考分页控件一节,了解更多详情. 注意:虽然工具 ...

  6. ZigBee心电传输(一)

    第一次接触模拟的东西哈,也算是一次新的学习旅程以及对ZigBee的再一次探索吧. 首先是方案制定,以及采用芯片AD8232,这样节省了不少时间,把模拟的东西都搬到数字上了,不过还是需要学习不少模电知识 ...

  7. springmvc4+hibernate4分页查询功能

    Springmvc+hibernate成为现在很多人用的框架整合,最近自己也在学习摸索,由于我们在开发项目中很多项目都用到列表分页功能,在此参考网上一些资料,以springmvc4+hibnerate ...

  8. Android基于WIFI实现电脑和手机间数据传输的技术方案研究

    Android手机和电脑间基于wifi进行数据传输,从技术上讲,主要有两种方案: 一种是通过ftp协议实现,Android手机作为数据传输过程中的ftp服务器: 一种是通过http协议实现.Andro ...

  9. 如何在pl/sql developer 7运行到oracle存储过程设置断点的地方

    如何高效调试oracle存储过程,尤其是父子网状调用的存储过程 1,在需要设置断点的oracle存储过程处设置断点         如何设置断点:直接在某行oracle存储过程处单击行首,会在行首显示 ...

  10. hdu find the safest road

    算法:多源最短路(floyd) 题意:每条通路有一个安全系数,求始点到终点的最大的安全系数并输出,如果没有输出What a pity! c++超时啊 Problem Description XX星球有 ...