Codeforces 229D Towers
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的更多相关文章
- Towers CodeForces - 229D
The city of D consists of n towers, built consecutively on a straight line. The height of the tower ...
- codeforces 479B Towers 解题报告
题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...
- Codeforces 479B. Towers 暴力
纯暴力..... B. Towers time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- nyoj 1216——整理图书 CF 229D—— Towers——————【dp+贪心】
整理图书 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 小明是图书鹳狸猿,他有很多很多的书堆在了一起摆在了架子上,每摞书是横着放的,而且每摞书是订好的 是一个整体, ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- Codeforces Beta Round #37 A. Towers 水题
A. Towers 题目连接: http://www.codeforces.com/contest/37/problem/A Description Little Vasya has received ...
- Codeforces 478D Red-Green Towers
http://codeforces.com/problemset/problem/478/D 思路:dp:f[i][j]代表当前第i层,用了j个绿色方块的方案数,用滚动数组,还有,数组清零的时候一定要 ...
- 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 ...
- Codeforces 739C Alyona and towers 线段树
Alyona and towers 这个题写起来真的要人命... 我们发现一个区间被加上一个d的时候, 内部的结构是不变的, 改变的只是左端点右端点的值, 这样就能区间合并了. 如果用差分的话会简单一 ...
随机推荐
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
- 关于bootstrap--表单(下拉<select>、输入框<input>、文本域<textare>复选框<checkbox>和单选按钮<radio>)
html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...
- NetAnalyzer笔记 之 九 使用C#对HTTP数据还原
[创建时间:2016-05-12 00:19:00] NetAnalyzer下载地址 在NetAnalyzer2016中加入了一个HTTP分析功能,很过用户对此都很感兴趣,那么今天写一下具体的实现方式 ...
- leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- swing入门例子
// a simple exmple that can show the basis of swing------------------------------------------------- ...
- ListView的Item被点击和其中的Button被点击同时生效
Android开发中在ListView中经常有Button或ImageButton等需要被点击的控件,如果不加一些特殊的限制,有可能 ListView的Item的点击事件或Button的点击事件,其中 ...
- 支持多QQ登录的软件
支持多QQ登录,批量加好友,批量回复QQ消息,当然也能接收 下载链接:多QQ登录软件
- python-操作缓存
参考王智刚同学博客 操作Mmecached 1. 安装API python -m pip install python-memcached 2. 启动memcached memcached -d -u ...
- 使用游标循环进行SQL更新插入的SQL语句
使用SQL中的循环,可以实现许多我们需要的操作,比如SQL更新操作.下面就为您介绍使用游标循环进行SQL更新插入的SQL语句写法,希望对您深入学习SQL更新有所帮助. --开始事务 BEGIN TRA ...
- 私人C#笔记
coust 定义常量 string是密封类,所以不能继承它 namespace默认是按照文件夹的结构命名的,如(System.文件夹.子文件夹),而且namespace是可以手动改的 Arra ...