题解 CF1872G Replace With Product
原题链接
场上过了 F 之后的 48min 一直坐牢,然后熄灯后躺在床上想了一会就会了,急了。
思路
把一段和替换为乘积,对于已确定的区间
考虑把整个序列划分为若干个块,每块内是极长的
然而,我们其实不能确定到底舍弃哪些黑块,例如
现在考虑另外一个性质,即区间的乘积充分大的情况(大于
现在序列的乘积应当不超过
时间复杂度
代码
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define db double
#define gc getchar
#define pc putchar
#define fs first
#define sc second
using namespace std;
ll read()
{
ll x=0,f=1;
char ch=gc();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=gc();
}
while(ch>='0'&&ch<='9')
x=x*10+(ch^48),ch=gc();
return x*f;
}
void print(ll x)
{
if(x<0)pc('-'),x=-x;
if(x>9)print(x/10);
pc(x%10+48);
}
const int N=2e5+5;
const ll INF=1e15;
int T,n;
ll a[N],s[N],x[N];
struct block
{
int l,r,op;
}b[N];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
T=read();
while(T--)
{
n=read();
__int128 flg=1;
x[0]=1;
for(int i=1;i<=n;i++)
{
a[i]=read();
s[i]=s[i-1]+a[i],x[i]=x[i-1]*a[i];
flg*=a[i];
if(flg>INF)flg=0;
}
if(!flg)
{
int l=1,r=n;
while(a[l]==1)l++;
while(a[r]==1)r--;
print(l),pc(' '),print(r),pc('\n');
continue;
}
int tot=0,l=1,r=1,op=(a[1]==1);
while(l<=n)
{
while(r<n&&op==(a[r+1]==1))r++;
b[++tot]={l,r,op};
op^=1,l=r+1,r=l;
}
l=1,r=tot;
if(b[1].op)l++;
if(b[tot].op)r--;
if(l>r)
{
print(1),pc(' '),print(1),pc('\n');
continue;
}
ll mx=s[n],ml=1,mr=1;
for(int i=l;i<=r;i+=2)
for(int j=i;j<=r;j+=2)
{
int L=b[i].l,R=b[j].r;
if((s[n]-s[R]+s[L-1])+x[R]/x[L-1]>mx)
{
mx=(s[n]-s[R]+s[L-1])+x[R]/x[L-1];
ml=L,mr=R;
}
}
print(ml),pc(' '),print(mr),pc('\n');
}
return 0;
}