POJ 2796 Feel Good】的更多相关文章

题目链接:http://poj.org/problem?id=2796 单调栈可以O(n)得到以每个位置为最小值,向左右最多扩展到哪里. #include<cstdio> #include<algorithm> #include<stack> using namespace std; ; int a[maxn]; int l[maxn]; int r[maxn]; long long pre[maxn]; stack< pair<int,int> &g…
http://poj.org/problem?id=2796 题意:给出n个数,问一个区间里面最小的元素*这个区间元素的和的最大值是多少. 思路:只想到了O(n^2)的做法. 参考了http://www.cnblogs.com/ziyi--caolu/archive/2013/06/23/3151556.html的写法,用单调栈可以优化到O(n). 对于每个元素,维护这个元素向前延伸比它大的有多少个,向后延伸比它小的有多少个.即该元素是处于山谷. 那么如何用单调栈维护这个呢? 首先这个栈是单调递…
传送门:http://poj.org/problem?id=2796 题意:给你一串数字,需要你求出(某个子区间乘以这段区间中的最小值)所得到的最大值 例子: 6 3 1 6 4 5 2 当L=3,R=5时这段区间总和为6+4+5=15,最小值为4,所以最后的结果为60. 思路:单调栈的板子题了.没学过单调栈的我只能现学了.单调栈分为单调递增栈,单调递减栈.而这个题呢,要用到单调递增栈.怎么维护单调栈呢?那就是在遇到一个元素大于栈顶元素时,就将该元素加入栈中.(假定即为区间最小值,区间左端点L=…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16786   Accepted: 4627 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
Feel Good   Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently devel…
关于单调栈的性质,和单调队列基本相同,只不过单调栈只使用数组的尾部, 类似于栈. Accepted Code: /************************************************************************* > File Name: 2796.cpp > Author: Stomach_ache > Mail: sudaweitong@gmail.com > Created Time: 2014年07月21日 星期一 22时45…
传送门 Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed ass…
传送门 Time Limit: 3000MS Memory Limit: 65536K Case Time Limit: 1000MS Special Judge   Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people'…
Feel Good 题意:给你一个非负整数数组,定义某个区间的参考值为:区间所有元素的和*区间最小元素.求该数组中的最大参考值以及对应的区间. 比如说有6个数3 1 6 4 5 2 最大参考值为6,4,5组成的区间,区间最小值为4,参考值为4*(6+5+4)=60 数据范围1<=n<=100000:单调栈做法:对于一个区间,我们需要知道区间的最小值,并且是在出栈的时候更新最优解:这时就可以想到当我们维护的是一个单调递增的栈时,栈顶元素由于当前入栈的元素逼栈顶元素小而要出栈时,这个栈顶元素所代表…
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed assigns a non-negat…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8753   Accepted: 2367 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
1619 - Feel Good Time limit: 3.000 seconds   Bill is developing a new mathematical theory for human emotions. His recent investigations are dedi- cated to studying how good or bad days in uent people's memories about some period of life. A new idea B…
单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath…
题目,给定一个数列,n <= 1e5 .要求找出一个区间,使得其内区间最小值 * 区间总和的值最大,要求输出区间. 首先先维护一个单调递增的栈,同时记录一个lef值表示:lef[i]表示当前栈内这个元素能匹配的最左值,什么意思呢?就是在最左边那里,它是最小的.a[lef[i] - 1] < a[lef[i]] 的.这样的话,每次弹出一个元素,我就能知道它在那个区间内最小的,左区间是lef[i],右区间栈顶元素的位置.这样是最优的,因为你匹配多一些大的数字更好. 用simple来说 3 1 6…
题目链接 题意 对于一个长度为\(n\)的非负整数数列\(a_1,a_2,-,a_n\),求\(max_{1≤l≤r≤n}f(l,r)\), 其中 \[f(l,r)=min(a_l,a_{l+1},-,a_r)×(a_l+a_{l+1}+⋯+a_r)\] 思路 显然,最小值必为数列中的某个数,所以题目转化为: 对于数列中的 每个数,找 使其 为区间最小值的 最大的区间,即该点向左向右最远能延伸到的地方 // 是不是和那道找最大矩形面积如出一辙? 法一:dp 用\(l[\ ]\)和\(r[\ ]\…
Feel Good Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people's memories about some period of life. A new idea Bill has recently developed assigns a…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11626   Accepted: 3212 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
POJ 2796 Feel Good HDU 1506 Largest Rectangle in a Histogram 和这两题一样的方法. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; +; int a[maxn]; int L[maxn],R[maxn]; int m,n; int tmp[maxn][maxn],b…
队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调队列和单调栈. 先来看一个问题,给一个长度为N的数列,a1,a2...aN,然后给一个k<=N,求输出b1,b2...bN这N个数,其中 bi=max( aj | j<=i && j>i-k && j>0 ). 比较朴素的想法是用一个Nk复杂度的循环来求…
这几天的知识学习比较多,因为时间不够了.加油吧,为了梦想. 这里写几条简单的单调栈作为题解记录,因为单调栈的用法很简单,可是想到并转化成用这个需要一些题目的积淀. 相关博客参见:https://blog.csdn.net/wubaizhe/article/details/70136174 POJ 3250 Bad Hair Day 题意与分析 题意是这样的:\(n\)个牛排成一列向右看,牛\(i\)能看到牛\(j\)的头顶,当且仅当牛\(j\)在牛\(i\)的右边并且牛\(i\)与牛\(j\)之…
题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了单调栈的题目,用的贼好.算是第一次应用吧. 我们定义$l_i$为左侧比第$i$个数小的数的下标的最大值(没有就是0):$r_i$就是右侧比第$i$个数小的数的下标的最小值(没有就是$n+1$).这样定义完后,我们会发现,$a_i$是$[a_{l_i+1},a_{r_i-1}]$的最小值,也就是siz…
http://www.cnblogs.com/whywhy/p/5066306.html 队列和栈是很常见的应用,大部分算法中都能见到他们的影子. 而单纯的队列和栈经常不能满足需求,所以需要一些很神奇的队列和栈的扩展. 其中最出名的应该是优先队列吧我觉得,然后还有两种比较小众的扩展就是单调队列和单调栈. 先来看一个问题,给一个长度为N的数列,a1,a2...aN,然后给一个k<=N,求输出b1,b2...bN这N个数,其中 bi=max( aj | j<=i && j>i…
这道题貌似可以用滑动窗口或者单调栈做, 但是我都没有用到. 这道题要求连续子序列中和乘上最小值最大, 那么我们就可以求出每一个元素, 以它为最小值的的最大区间的值, 然后取max就ok了.那么怎么求呢? 我可以初始化出一个第一个小于当前元素的的元素的位置, 也就是说初始化出以当前元素为最小值的 左右端点, 求出之后枚举一遍就ok了. 那么关键是怎么求呢? 这里有一点扫描法的味道. 假设当前序列为1243 以求右端点为例, 右端点的位置也就是在右侧第一个小于当前元素的元素的位置. 那么如果序列是递…
#include <cstdio> /* * source poj.2796 * 题目: * 给定一个非负数的数组 其中value[l,r] = sum(l,r) * min (l,r); * 求 最大值和,最大值的位置 * 题解: * 所求的区域的最小值是x的话一定是这个值向左右去延伸至比他大的元素为止 * 而这个问题的求解一般是n^2的问题,但是我们不能接受因此; * 维护这个区间需要我们维护一个stack * 具体操作如下 * (1)元素入栈 * 1,记录值(value, weight)…
1437 迈克步 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 有n只熊.他们站成一排队伍,从左到右依次1到n编号.第i只熊的高度是ai. 一组熊指的队伍中连续的一个子段.组的大小就是熊的数目.而组的力量就是这一组熊中最小的高度. 迈克想知道对于所有的组大小为x(1 ≤ x ≤ n)的,最大力量是多少. Input 单组测试数据. 第一行有一个整数n (1 ≤ n ≤ 2×10^5),表示熊的数目. 第二…
http://poj.org/problem?id=2796 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15375   Accepted: 4252 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investi…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
http://poj.org/problem?id=2796 题意:求n个数的和乘以这n个数中的最小值的积最大的数,以及其范围. 思路:求每一个数两边的比其大的数的和,再乘以这个数.还有一个范围,用单调栈找以及记录. 这个题和2559差不多,就是多了一个对数字的求和. #include <stdio.h> #include <iostream> #include <stack> #define x 100010 using namespace std; stack<…
Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798   Special Judge Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets…