【习题 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;
}

原文地址:https://www.cnblogs.com/AWCXV/p/8457099.html

时间: 2024-10-20 05:47:56

【习题 8-18 UVA - 1619】Feel Good的相关文章

C++ Primer 第四版课后练习解答 习题1.18

注意:本随笔是直接参考<C++Primer(第四版)习题解答(完整版)>中的.此处主要是便于本人以后反复阅读. 习题1.18 编写程序,提示用户输入两个数并将这两个数范围内的每个数写到标准输出. [解答] while循环实现 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int v1, v2, low,up; 7 cout << "Enter tow numbers &quo

UVA 1619 Feel Good(DP)

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

uva 1619 - Feel Good 一个技巧

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 Bil

C++ Primer(第四版) 课后习题4.18

问题: 编写程序,使用指针把一个 编写程序,使用指针把一个 int 型数组的所有元素设置为 0. 代码: 1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 const size_t Size = 5; 8 int arr[Size] = {0,1,2,3,4}; 9 for (int *pbegin=arr, *pend =arr+Size ; pbegin != pend; pbegin++) 10 { 1

UVA 1619/POJ2796 滑窗算法/维护一个单调栈

Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12409   Accepted: 3484 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated

Java50道经典习题-程序18 乒乓球赛

题目:两个乒乓球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已抽签决定比赛名单.有人向队员打听比赛的名单. a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单.分析:如果是人经过逻辑推理可以很快的得到结论.但是计算机处理此问题,不可能立即得出结论,而必须对每一种组合一一验证,找出符合条件的组合. 假设甲队a,b,c的对手分别是i,j,k i,j,k互不相等并且分别都是乙队x,y,z中的一人 利用三重for循环保证i,j,k互不相等 再将题设条件“a说他不和x比,

C++ Primer第四版习题--5.18

#include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string*> spvec; string str; while(cin>>str) { string *sp = new string; *sp = str; spvec.push_back(sp); } vector<string*>::

UVA 1619 Feel Good 感觉不错 (扫描法)

Feel Good Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu 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 a

UVa - 1619 - 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 non-negat