wangzhaohan2910 @ 2024-12-08 16:23:31
先贴正解:
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> v;
char buf[1 << 20], *p1, *p2;
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++)
inline void read(int &x)
{
register int f = 1;
register char ch = getchar();
x ^= x;
while (ch < 48 || ch > 57)
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch > 47 && ch < 58)
x = x * 10 + (ch ^ 48), ch = getchar();
x *= f;
}
signed main()
{
register int n, x;
vector<int>::iterator y;
read(n);
v.reserve(n);
while (n--)
{
read(x);
y = lower_bound(begin(v), end(v), x);
if (y == end(v))
v.push_back(x);
else
*y = x;
}
printf("%u", v.size());
return 0;
}
注意第31行的 lower_bound
,注意不是 upper_bound
,因为求的是 最长上升子序列,而不是最长不下降子序列。本人就被hack掉了,后来调了不短的时间才过。