hdu1506(dp减少重复计算)

可以算出以第i个值为高度的矩形可以向左延伸left[i],向右延伸right[i]的长度

那么答案便是 (left[i] + right[i] + 1) * a[i] 的最大值

关键left[i] 和right[i]的计算

如果a[i] > a[i-1]  ,  那么left[i] = 0

如果a[i] <=a[i-1],  那么left[i] = left[i-1] + 1,   但是位置i-1-left[i-1]-1及其往左的元素没有比较过,所以需要继续去比较

同理right[i]的计算也是一样的

 1 #pragma warning(disable:4996)
 2 #pragma comment(linker, "/STACK:1024000000,1024000000")
 3 #include <stdio.h>
 4 #include <string.h>
 5 #include <time.h>
 6 #include <math.h>
 7 #include <map>
 8 #include <set>
 9 #include <queue>
10 #include <stack>
11 #include <vector>
12 #include <bitset>
13 #include <algorithm>
14 #include <iostream>
15 #include <string>
16 #include <functional>
17 #include <unordered_map>
18 typedef __int64 LL;
19 const int INF = 999999999;
20
21 const int N = 100000 + 10;
22 int a[N];
23 LL left[N], right[N];
24 int main()
25 {
26     int n;
27     while (scanf("%d", &n), n)
28     {
29         for (int i = 1;i <= n;++i)
30         {
31             scanf("%d", &a[i]);
32         }
33         left[1] = right[n] = 0;
34         int l,r;
35         for (int i = 2;i <= n;++i)
36         {
37             l = i - 1;
38             left[i] = 0;
39             while (l>=1 && a[i] <= a[l])
40             {
41                 left[i] += left[l] + 1;
42                 l = l - left[l] - 1;
43             }
44         }
45         for (int i = n - 1;i >= 1;--i)
46         {
47             right[i] = 0;
48             r = i + 1;
49             while (r <= n && a[i] <= a[r])
50             {
51                 right[i] += right[r] + 1;
52                 r = r + right[r] + 1;
53             }
54         }
55         LL ans = 0;
56         for (int i = 1;i <= n;++i)
57         {
58             ans = std::max(ans, (left[i] + right[i] + 1)*a[i]);
59         }
60         printf("%I64d\n", ans);
61     }
62     return 0;
63 }
时间: 2024-08-08 16:08:46

hdu1506(dp减少重复计算)的相关文章

如何有效的减少重复的代码

重复的代码一直都是可维护性的大敌,重构的重要任务之一也就是要去除掉重复的代码,有效的减少重复代码,可以大大提高软件的扩展性. 在Android开发中,很容易产生重复的代码.因为Android是组件,模板式开发,每个页面都是独立用Activity或Fragment实现,布局文件都是用XML方式去写,所以很容易造成代码的重复,虽然二个页长的差不多,但毕竟是二个Activity,于是就拷一份出来,改吧改吧就成了. 那么我们如何做才能去掉重复的代码呢? 点击阅读全文

hdu1506 dp

1 //Accepted 1428 KB 62 ms 2 // 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 using namespace std; 7 const int imax_n = 100005; 8 int l[imax_n]; 9 int r[imax_n]; 10 int a[imax_n]; 11 int n; 12 void Dp() 13 { 14 l[1]=1;

ios-QQ界面(利用新浪微博方法实现,消除新浪微博重复计算的缺点)

消除重复计算的缺点,既是将有两个模型,一个数据模型,一个位置模型.位置模型里面包含了数据模型,将计算位置和数据都一起做了. Constant.h文件保存常量 // // Constant.h // QQ聊天 // // Created by YaguangZhu on 15/8/27. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #ifndef QQ___Constant_h #define QQ___Constant_h

利用泛型减少重复,实现简易AOP

利用泛型减少重复,实现简易AOP 一.设计原则之DRY,不要重复自己    在所有设计原则中,DRP(Don't Repeat Yourself)是最基础的原则之一.是由 Andy Hunt 和 Dave Thomas 在 The Pragmatic Programmer 中总结出来的, 成为软件开发和设计的最佳实践基础.开发者认识到,通过好的实践和适当的抽象可以减少重复,使程序的代码变得简洁. 重复是一种浪费.程序中的每一行代码都需要维护,重复就会导致出现bug的机会增多,也增加了系统的复杂度

运算程序,计算玩判断,Y继续,重复计算,N结束

1 #include "stdio.h" 2 void main() 3 { 4 /*定义变量,d1,d2:第一.二个数 fu:符号 p1:接收判断号Y/N 5 p2:接收的p1赋给p1 6 */ 7 int d1,d2; 8 char fu,p1,p2; 9 do 10 { 11 printf("请输入第一个数:"); 12 scanf("%d",&d1); //接收数字 13 fflush(stdin); //清除缓存 14 prin

用泛型减少重复代码,使代码更合理、更优雅

有这样一个场景,需要对接接口,以获取取得数据. 例如获取订单列表 接口请求参数(json格式): 1 { 2 //公共头部 3 "head":{ 4 "method":"getOrders", //接口方法 5 "sign":"xxxx" //签名 6 }, 7 //私有主体 8 "body":{ 9 "userId":"1", //用户ID 10

BZOJ 2726: [SDOI2012]任务安排 [斜率优化DP 二分 提前计算代价]

2726: [SDOI2012]任务安排 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 868  Solved: 236[Submit][Status][Discuss] Description 机器上有N个需要处理的任务,它们构成了一个序列.这些任务被标号为1到N,因此序列的排列为1,2,3...N.这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分批加工,第i个任务单独完成所需的时间是Ti.在每批任务开始前,机器需要启

ANDROID_MARS学习笔记_S01_004dpi、dp(dip)及计算

一.dpi.dp介绍 sp会随着用户在手机中设置字体大小而改变,而dp不会 二.1.dpsp_layout.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent&

在同个类中non-const插入const来减少重复

class A { private: std::string a; public: A(std::string b) :a(b){} const char& operator[](int b)const { std::cout << "const"<<std::endl; return a[b]; } char& operator[](int b) { std::cout << "non-const" <&l