#include <bits/stdc++.h>
using namespace std;
vector<int> LIS(vector<int>& a) {
vector<int> tail(a.size(), 0);
int length = 1;
tail[0] = a[0];
for (size_t i = 1; i < a.size(); i++) {
if (a[i] < tail[0])
tail[0] = a[i];
else if (a[i] > tail[length - 1])
tail[length++] = a[i];
else
tail[lower_bound(tail.begin(), tail.begin() + length, a[i]) - tail.begin()] = a[i];
}
return vector<int>(tail.begin(), tail.begin() + length);
}
int main() {
vector<int> a = {1, 2, 3, 1, 2, 3};
vector<int> b = {2, 1, 3, 3, 2, 1};
map<int, int> indices;
for (int i = 0; i < a.size(); i++)
indices[a[i]] = i + 1;
for (int& x : b)
x = indices[x];
vector<int> lis = LIS(b);
cout << "Length of LIS is " << lis.size() << "\n";
for (int x : lis)
cout << x << " ";
cout << "\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp2ZWN0b3I8aW50PiBMSVModmVjdG9yPGludD4mIGEpIHsKICAgIHZlY3RvcjxpbnQ+IHRhaWwoYS5zaXplKCksIDApOwogICAgaW50IGxlbmd0aCA9IDE7CgogICAgdGFpbFswXSA9IGFbMF07CiAgICBmb3IgKHNpemVfdCBpID0gMTsgaSA8IGEuc2l6ZSgpOyBpKyspIHsKICAgICAgICBpZiAoYVtpXSA8IHRhaWxbMF0pCiAgICAgICAgICAgIHRhaWxbMF0gPSBhW2ldOwogICAgICAgIGVsc2UgaWYgKGFbaV0gPiB0YWlsW2xlbmd0aCAtIDFdKQogICAgICAgICAgICB0YWlsW2xlbmd0aCsrXSA9IGFbaV07CiAgICAgICAgZWxzZQogICAgICAgICAgICB0YWlsW2xvd2VyX2JvdW5kKHRhaWwuYmVnaW4oKSwgdGFpbC5iZWdpbigpICsgbGVuZ3RoLCBhW2ldKSAtIHRhaWwuYmVnaW4oKV0gPSBhW2ldOwogICAgfQoKICAgIHJldHVybiB2ZWN0b3I8aW50Pih0YWlsLmJlZ2luKCksIHRhaWw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