#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
template <class T> void minimize (T &t, T f) {if (t > f) t = f;}
template <class T> void maximize (T &t, T f) {if (t < f) t = f;}
const int maxN = 2e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
int n;
int a[maxN], pw[maxN];
pii L[maxN], R[maxN];
struct ST {
vector <pii> st;
void init (int n) {
st.assign (n * 6, {0, 0});
}
void upd (int id, int l, int r, int pos, int val, int cnt) {
if (pos < l || pos > r) {
return;
}
if (l == r) {
if (st[id].fi == val) {
st[id] = {val, (st[id].se + cnt) % mod};
}
else {
st[id] = {val, cnt};
}
return;
}
int mid = (l + r) >> 1;
upd (id * 2, l, mid, pos, val, cnt);
upd (id * 2 + 1, mid + 1, r, pos, val, cnt);
if (st[id * 2].fi == st[id * 2 + 1].fi) {
st[id] = {st[id * 2].fi, (st[id * 2].se + st[id * 2 + 1].se) % mod};
}
else {
st[id] = max (st[id * 2], st[id * 2 + 1]);
}
return;
}
pii get (int id, int l, int r, int u, int v) {
if (v < l || u > r) {
return {0, 0};
}
if (u <= l && r <= v) {
return st[id];
}
int mid = (l + r) >> 1;
auto tL = get (id * 2, l, mid, u, v);
auto tR = get (id * 2 + 1, mid + 1, r, u, v);
if (tL.fi == tR.fi) {
return {tL.fi, (tL.se + tR.se) % mod};
}
return ((tL.fi > tR.fi) ? tL : tR);
}
} T;
void print (int lim) {
for (int j = 1; j <= lim; j ++) {
cout << T.get (1, 1, lim, j, j).fi << ' ';
}
cout << '\n';
}
void solve () {
cin >> n;
pw[0] = 1;
for (int i = 1; i < n * 2; i ++) {
pw[i] = pw[i - 1] * 2;
pw[i] %= mod;
}
vector <int> zip;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
zip.push_back (a[i]);
L[i] = R[i] = {1, 1};
}
sort (all (zip));
zip.erase (unique (all (zip)), zip.end ());
for (int i = 1; i <= n; i ++) {
a[i] = lower_bound (all (zip), a[i]) - zip.begin () + 1;
}
int lim = zip.size () + 2;
T.init (lim);
for (int i = n; i >= 1; i --) {
//print (lim);
//cout << a[i] << '\n';
auto [x, y] = T.get (1, 1, lim, 1, a[i] - 1);
if (L[i].fi <= x + 1) {
L[i].fi = x + 1;
L[i].se = max (y, 1LL);
}
T.upd (1, 1, lim, a[i], L[i].fi, L[i].se);
}
T.init (lim);
for (int i = n; i >= 1; i --) {
auto [x, y] = T.get (1, 1, lim, a[i] + 1, lim);
if (R[i].fi <= x + 1) {
R[i].fi = x + 1;
R[i].se = max (y, 1LL);
}
T.upd (1, 1, lim, a[i], R[i].fi, R[i].se);
}
int res = 0;
int mx = 0;
for (int i = 1; i <= n; i ++) {
auto [tL, cL] = L[i];
auto [tR, cR] = R[i];
int cur = tL + tR - 1;
//cout << tL << ' ' << tR << '\n';
if (cur == mx) {
(res += (cL * cR) % mod * (pw[n - cur]) % mod) %= mod;
}
else if (cur > mx) {
mx = cur;
res = (cL * cR) % mod * (pw[n - cur]) % mod;
}
}
cout << mx << ' ' << res;
return;
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfv
Compilation message (stderr)
zoltan.cpp: In function 'int main()':
zoltan.cpp:147:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
zoltan.cpp:148:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |