【习题 8-18 UVA - 1619】Feel Good
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
用单调队列求出l[i]和r[i]
分别表示i的左边最近的大于a[i]的数的位置以及i右边最近的大于a[i]的数的位置。
则l[i]+1..r[i]-1就是a[i]这个数作为最小数的最大管辖区间了。
写个前缀和就好。
然后取a[i]*区间l[i]+1..r[i]-1的和 中的最大值。
并不是special judge
多个相同区间。
取区间长度最短的区间。
如果仍有多个答案。
取左端点最小的那个区间。
(全都是0的情况要注意,直接取[1,1]就好,不能取[1..n]
【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int N = 1e5;
int n,a[N+10],l[N+10],r[N+10];
LL pre[N+10];
stack<int> sta;
void solve(){
rep1(i,1,n) cin >> a[i];
pre[0] = 0;
rep1(i,1,n) pre[i] = pre[i-1]+a[i];
l[1] = 0;
while (!sta.empty()) sta.pop();
sta.push(1);
for (int i = 2;i <= n;i++){
while (!sta.empty() && a[sta.top()]>=a[i]) sta.pop();
if (sta.empty())
l[i] = 0;
else
l[i] = sta.top();
sta.push(i);
}
while (!sta.empty()) sta.pop();
sta.push(n);
r[n] = n+1;
for (int i = n-1;i >= 1;i--){
while (!sta.empty() && a[sta.top()]>=a[i]) sta.pop();
if (sta.empty())
r[i] = n+1;
else
r[i] = sta.top();
sta.push(i);
}
long long ans = -1;
int posl,posr;
rep1(i,1,n){
int ll = l[i]+1;int rr = r[i]-1;
if (a[ll]==0 && a[rr]==0){
rr = ll;
}
LL temp = 1LL*a[i]*(pre[rr]-pre[ll-1]);
if (temp>ans){
ans = temp;
posl = ll,posr = rr;
}else if (temp==ans){
if (posr-posl>rr-ll || (posr-posl==rr-ll && ll<posl)){
posl = ll,posr = rr;
}
}
}
cout<<ans<<endl;
cout<<posl<<' '<<posr<<endl;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int kase = 0;
while (cin>>n){
if (kase>0)
cout<<endl;
else
kase++;
solve();
}
return 0;
}
【习题 8-18 UVA - 1619】Feel Good的更多相关文章
- UVA 1619 Feel Good(DP)
Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...
- POJ 2796[UVA 1619] Feel Good
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16786 Accepted: 4627 Case T ...
- uva 1619 - Feel Good || poj 2796 单调栈
1619 - Feel Good Time limit: 3.000 seconds Bill is developing a new mathematical theory for human ...
- [UVa 1619]Feel Good
#include <bits/stdc++.h> using namespace std; const int maxn = 1000010; struct node { int num; ...
- Java50道经典习题-程序18 乒乓球赛
题目:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单.分析: ...
- UVA 1619/POJ2796 滑窗算法/维护一个单调栈
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 12409 Accepted: 3484 Case T ...
- UVA 1619 Feel Good 感觉不错 (扫描法)
Feel Good Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Bill is deve ...
- UVA - 1619 Feel Good(扫描法)
题目: 思路: 预处理出a[i]在哪个范围区间内是最小的,然后直接遍历a数组求答案就可以了. 这个预处理的技巧巧妙的用了之前的处理结果.(大佬tql) 代码: #include <bits/st ...
- POJ 2796 / UVA 1619 Feel Good 扫描法
Feel Good Description Bill is developing a new mathematical theory for human emotions. His recent ...
随机推荐
- 前端学习之路——gulp篇
一.构建gulp环境 1.下载nodejs gulp基于node.js,要通过nodejs的npm安装gulp,所以要先安装node.js环境.(英文官网/中文官网链接). 通过cmd命令窗口确定安装 ...
- SpringCloud学习笔记(8)----Spring Cloud Netflix之负载均衡-Ribbon的负载均衡的策略
一. 内置 负载均衡策略的介绍的 IRule的实现类 2. 通过代码实现负载均衡 在第六节Riddom的使用的工程中,随机策略配置类 package com.wangx.cloud.springclo ...
- 洛谷 P3203 [HNOI2010]弹飞绵羊 分块
我们只需将序列分成 n\sqrt{n}n 块,对于每一个点维护一个 val[i]val[i]val[i],to[i]to[i]to[i],分别代表该点跳到下一个块所需要的代价以及会跳到的节点编号.在 ...
- vue的表格加单选框
https://www.cnblogs.com/calamus/p/8569196.html
- 利用js自带函数 数组去重
<script> ,,]; //原数组 var a=[]; //定义空数组 arr.map(function(x){ //用 map 遍历数组 ){ //如果当前值没有存在空数组中 a.p ...
- du -sh*查看当前目录下的文件夹大小
du -sh*查看当前目录下的文件夹大小 u 命令 用途 概述磁盘使用. 语法 du [ -a | -s ] [ -k ] [ -m ] [ -g ][ -l ] [ -r ] ...
- tar 命令man说明
TAR(1) User Commands TAR(1) NAME tar - manual page for tar 1.26 SYNOPSIS tar [OPTION...] [FILE]... D ...
- 关于数组array_diff(array1, array2)求差集来比较数组是否相等的问题细究
无意中发现很多朋友都喜欢使用array_diff(array1, array2)来判断两个数组是否相等, 我自己也偶尔会这么使用 但是今天我在写代码的过程中无意发现这么做是不准确的. 首先我们来看一下 ...
- C语言修改文件某部分内容
两种方法 1.全部读入内存 修改后重新存入文件 2.边读边写到另一新建文件 要修改的部分修改后存入新建文件 其他部分原封不动写入 写完删掉原先文件 将这个新的改为删掉那个的名字 方法一 读入内存修改 ...
- Ubuntu 10.04 右上角网络管理图标消失的解决的方法
那个显示网络状态的那个图标.叫做:network-manager.假设是有线网络连接的话.是上下两个箭头,假设是无线网络的话.是一个发射信号的形状. sudo gedit /etc/Ne ...