// Make the best become better
// No room for laziness
#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e9;
int n, a[maxN], id[maxN];
int f[maxN], trace[maxN];
int dp[maxN];
pair<int, int> st[4 * maxN];
void ReadInput()
{
cin >> n;
for(int i=1; i<=n; i++)
cin >> a[i];
}
void update(int id, int l, int r, int pos, pair<int, int> val)
{
if(l == r)
{
st[id] = val;
return;
}
int mid = (l + r) / 2;
if(pos <= mid) update(id * 2, l, mid, pos, val);
else update(id * 2 + 1, mid + 1, r, pos, val);
st[id] = max(st[id * 2], st[id * 2 + 1]);
}
pair<int, int> get(int id, int l, int r, int u, int v)
{
if(l > v || r < u) return {-oo, -oo};
if(l >= u && r <= v) return st[id];
int mid = (l + r) / 2;
return max(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
bool chk(int val)
{
memset(dp, -3, sizeof dp);
dp[0] = 0;
vector<int> vc;
vc.pb(0);
for(int i=1; i<=n; i++)
{
int t = id[i];
int l = max(0, i - val), r = i - a[t];
int j = upper_bound(vc.begin(), vc.end(), r) - vc.begin() - 1;
if(j < 0) continue;
j = vc[j];
if(j < l) continue;
dp[i] = dp[j] + 1;
vc.pb(i);
}
return dp[n] == f[n];
}
bool chk1(int val)
{
memset(dp, -3, sizeof dp);
dp[0] = 0;
for(int i=1; i<=n; i++)
{
int t = id[i];
for(int j=i-a[t]+1; j>=max(1, i-val+1); j--)
{
if(dp[i] < dp[j - 1] + 1)
{
dp[i] = dp[j - 1] + 1;
}
}
}
return dp[n] == f[n];
}
void Print(int val)
{
memset(st, -3, sizeof st);
memset(f, -3, sizeof f);
f[0] = 0;
for(int i=1; i<=n; i++)
{
update(1, 1, n, i, {f[i - 1], i});
int t = id[i];
int l = max(1, i - val + 1), r = i - a[t] + 1;
pair<int, int> tmp = get(1, 1, n, l, r);
f[i] = tmp.fi + 1;
trace[i] = tmp.se;
}
cout << f[n] << '\n';
int i = n;
while(i)
{
cout << i - trace[i] + 1 << " ";
for(int j=i; j>=trace[i]; j--)
cout << id[j] << " ";
i = trace[i] - 1;
cout << '\n';
}
}
void Solve()
{
iota(id + 1, id + n + 1, 1);
sort(id + 1, id + n + 1, [](int i, int j)
{
return a[i] < a[j];
});
memset(st, -3, sizeof st);
memset(f, -3, sizeof f);
f[0] = 0;
for(int i=1; i<=n; i++)
{
update(1, 1, n, i, {f[i - 1], i});
int t = id[i];
int l = 1, r = i - a[t] + 1;
pair<int, int> tmp = get(1, 1, n, l, r);
f[i] = tmp.fi + 1;
}
int low = a[id[n]], high = max(n, a[id[n]] + 50), mid;
while(low <= high)
{
mid = (low + high) / 2;
bool ok = false;
if(n <= 5000) ok = chk1(mid);
else ok = chk(mid);
if(ok) high = mid - 1;
else low = mid + 1;
}
Print(low);
}
int32_t main()
{
// freopen("x.inp", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ReadInput();
Solve();
}
Compilation message
tea.cpp: In function 'void Print(int)':
tea.cpp:83:29: warning: 'void* memset(void*, int, size_t)' writing to an object of type 'struct std::pair<int, int>' with no trivial copy-assignment [-Wclass-memaccess]
83 | memset(st, -3, sizeof st);
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tea.cpp:3:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<int, int>' declared here
211 | struct pair
| ^~~~
tea.cpp: In function 'void Solve()':
tea.cpp:113:29: warning: 'void* memset(void*, int, size_t)' writing to an object of type 'struct std::pair<int, int>' with no trivial copy-assignment [-Wclass-memaccess]
113 | memset(st, -3, sizeof st);
| ^
In file included from /usr/include/c++/10/bits/stl_algobase.h:64,
from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tea.cpp:3:
/usr/include/c++/10/bits/stl_pair.h:211:12: note: 'struct std::pair<int, int>' declared here
211 | struct pair
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
39380 KB |
Output is correct |
2 |
Correct |
18 ms |
39436 KB |
Output is correct |
3 |
Correct |
19 ms |
39380 KB |
Output is correct |
4 |
Correct |
18 ms |
39448 KB |
Output is correct |
5 |
Correct |
19 ms |
39496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
39452 KB |
Output is correct |
2 |
Correct |
19 ms |
39376 KB |
Output is correct |
3 |
Correct |
21 ms |
39380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
39440 KB |
Output is correct |
2 |
Correct |
20 ms |
39452 KB |
Output is correct |
3 |
Correct |
20 ms |
39484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
39508 KB |
Output is correct |
2 |
Correct |
47 ms |
39544 KB |
Output is correct |
3 |
Correct |
49 ms |
39528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
39508 KB |
Output is correct |
2 |
Correct |
58 ms |
39544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
41100 KB |
Output is correct |
2 |
Correct |
104 ms |
41004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
41592 KB |
Output is correct |
2 |
Correct |
105 ms |
40880 KB |
Output is correct |
3 |
Correct |
134 ms |
41160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1025 ms |
53596 KB |
Output is correct |
2 |
Correct |
1080 ms |
53592 KB |
Output is correct |
3 |
Correct |
1169 ms |
53648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1523 ms |
58252 KB |
Output is correct |
2 |
Correct |
1273 ms |
61520 KB |
Output is correct |
3 |
Correct |
1300 ms |
58372 KB |
Output is correct |
4 |
Correct |
1409 ms |
56112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1183 ms |
60140 KB |
Output is correct |
2 |
Correct |
470 ms |
58124 KB |
Output is correct |
3 |
Correct |
1311 ms |
58360 KB |
Output is correct |
4 |
Correct |
1689 ms |
58420 KB |
Output is correct |