社区讨论

求调

UVA1619感觉不错 Feel Good参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m21aipnp
此快照首次捕获于
2024/10/09 11:08
去年
此快照最后确认于
2024/10/09 16:49
去年
查看原帖
CPP
#include <bits/stdc++.h>

#define int long long

using namespace std;

using i64 = long long;
using i128 = __int128;
using ull = unsigned long long;
using pii = std::pair < int, int >;

#define fi first
#define se second
#define pc putchar
#define pb push_back
#define ma make_pair
#define eb emplace_back
#define all(x) x.begin(), x.end()

const int N = 1e6 + 10;
const int mod = 998244353;
const int inf = 1e18;

void file()
{
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
}

inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')	f=-1; ch=getchar(); }
	while(ch>='0' && ch<='9')	x=x*10+ch-'0',ch=getchar();
	return x*f;
}

void print(int x)
{
	if(x<0)	putchar('-'),x=-x;
	if(x>9) print(x/10);
	putchar(x%10+'0');
	return;
}

int l[N],r[N];
int a[N],s[N];
int n;
stack<int> st;

signed main(void)
{
	// file();
	// cin.tie(0),cout.tie(0)->ios::sync_with_stdio(false);
    bool b=0;
	while(scanf("%lld",&n)!=EOF)
    {
        memset(l,0,sizeof l);
        memset(r,0,sizeof r);
        memset(s,0,sizeof s);
        while(st.size())    st.pop();   
        if(b) pc('\n');
		else b=1;
        for(int i=1;i<=n;i++)   scanf("%lld",a+i);
        for(int i=1;i<=n;i++)   s[i]=s[i-1]+a[i];
        for(int i=n;i>=1;i--)
        {
            while(st.size()&&a[st.top()]>a[i]) st.pop();
            if(st.size())   r[i]=st.top()-1;
            else    r[i]=n;
            st.push(i);
        }
        while(!st.empty())  st.pop();
        for(int i=1;i<=n;i++)
        {
            while(st.size()&&a[st.top()]>a[i]) st.pop();
            if(st.size())   l[i]=st.top()+1;
            else    l[i]=1;
            st.push(i);
        }
        int ans=0;
        int res=0;
        int ansl=0,ansr=0;
        for(int i=1;i<=n;i++)   
        {
            ans=(s[r[i]]-s[l[i]-1])*a[i];
            // cout<<ans<<'\n';
            if(ans>res||(ans==res&&r[i]-l[i]<ansr-ansl))
            {
                res=ans;
                ansl=l[i],ansr=r[i];
            }
        }
        printf("%lld\n%d %d\n",res,ansl,ansr);
    }
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...