This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
pair<int, int> tree[N * 4];
int arr[N];
int vis[N];
int p[N];
int n;
void build(int node = 1, int l = 1, int r = n)
{
if (l == r)
{
tree[node] = {0, 0};
return;
}
int mid = (l + r) / 2;
build(node * 2, l, mid), build(node * 2 + 1, mid + 1, r);
tree[node] = {0, 0};
}
pair<int, int> comb(pair<int, int> x, pair<int, int> y)
{
if (x.first == y.first)
return (x.second > y.second ? y : x);
return (x.first < y.first ? y : x);
}
void update(int idx, int val, int node = 1, int l = 1, int r = n)
{
if (l == r)
{
tree[node] = {val, idx};
return;
}
int mid = (l + r) / 2;
if (idx <= mid)
update(idx, val, node * 2, l, mid);
else
update(idx, val, node * 2 + 1, mid + 1, r);
tree[node] = comb(tree[node * 2], tree[node * 2 + 1]);
}
pair<int, int> query(int ql, int qr, int node = 1, int l = 1, int r = n)
{
if (ql <= l && r <= qr)
return tree[node];
if (qr < l || r < ql)
return {0, 0};
int mid = (l + r) / 2;
return comb(query(ql, qr, node * 2, l, mid), query(ql, qr, node * 2 + 1, mid + 1, r));
}
vector<int> lis()
{
build();
vector<int> ret;
for (int i = n; i >= 1; i--)
{
if (vis[arr[i]])
continue;
pair<int, int> hi = query(arr[i], n);
if (hi.first == 0)
update(arr[i], 1), p[arr[i]] = arr[i];
else
update(arr[i], hi.first + 1), p[arr[i]] = hi.second;
}
pair<int, int> hi = query(1, n);
if (hi.first == 0)
return {};
else
{
while (true)
{
ret.push_back(hi.second);
vis[hi.second] = 1;
if (hi.second == p[hi.second])
break;
hi.second = p[hi.second];
}
return ret;
}
}
int main()
{
cin >> n;
vector<int> loc(n + 1);
for (int i = 1; i <= n; i++)
cin >> arr[i], loc[arr[i]] = i;
int mx = 0;
vector<vector<int>> ans;
while (true)
{
vector<int> hi = lis();
if ((int)hi.size() < mx)
break;
mx = hi.size();
ans.push_back(hi);
}
cout << ans.size() << " " << mx << "\n";
for (auto v : ans)
{
for (auto s : v)
cout << loc[s] << " ";
cout << "\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |