【习题 8-18 UVA - 1619】Feel Good
2018-02-21 19:42
465 查看
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
用单调队列求出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+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; }
相关文章推荐
- UVA 1619(p256)----Feel Good
- 习题 8-18 UVA - 1619 Feel Good 感觉不错 (容斥定理)
- uva 1619 - Feel Good
- uva1619-feel good
- UVa 1619 Feel Good
- UVa 1619:Feel Good(单调栈)
- UVa - 1619 - Feel Good
- UVa - 1619 - Feel Good
- Feel Good - UVa 1619 dp
- POJ 2796 / UVA 1619 Feel Good 扫描法
- 习题4-10 洪水 UVa815
- 章三习题1——UVA 1585
- 章三习题4——UVA 455
- 习题3-1 得分 UVa1585
- 习题3-6 纵横字谜的答案 UVa232
- 习题10-3 UVA 1643 Angle and Squares 角度和正方形
- 习题 4-8 特别困的学生 uva12108
- UVA 10118 紫书习题9—2
- 【习题 5-2 UVA-1594】Ducci Sequence
- 【习题5-5 UVA-10391】Compound Words