-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path020-replace-with-product.cpp
More file actions
62 lines (61 loc) · 1.59 KB
/
Copy path020-replace-with-product.cpp
File metadata and controls
62 lines (61 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Replace With Product [1872G]
* Problem: https://codeforces.com/problemset/problem/1872/G
* Verdict: ACCEPTED Solved: 2023-12-22
* Language: C++20 (GCC 11-64)
* Runtime: 61 ms Memory: 2900 KB
* Tags: brute force, greedy, math
* Author: BidoTeima
* Source: https://codeforces.com/contest/1872/submission/238430370
*/
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
ll prod = 1;
bool overflow = 0;
vector<int>v;
for(int i = 0; i < n; i++){
cin>>a[i];
if(!overflow && prod > 1e18 / a[i]){
overflow = 1;
}
else if(!overflow) prod *= a[i];
if(a[i] > 1){
v.push_back(i);
}
}
if(overflow){
cout<<v.front()+1<<' '<<v.back()+1<<'\n';
continue;
}
int bestL = 1, bestR = 1;
ll diff = 0;
for(int l = 0; l < (int)v.size(); l++){
for(int r = l; r < (int)v.size(); r++){
ll prod = 1, sum = 0;
for(int i = v[l]; i <= v[r]; i++){
prod *= a[i];
sum += a[i];
}
if(prod - sum > diff){
diff = prod - sum;
bestL = v[l] + 1;
bestR = v[r] + 1;
}
}
}
cout<<bestL<<' '<<bestR<<'\n';
}
return 0;
}