P1439 【模板】最长公共子序列
对于这道题,我们可以把
然后我们把这个映射用于
那么我们就将问题转化为了,如何求映射后的
树状数组优化
max 有两个限制,一个是
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
namespace oi{
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using db = double;
using pll = pair<ll, ll>;
#define gc getchar
#define pc putchar
#define endl '\n'
inline ll read() {
char ch = gc(); ll fu = 0, s = 0;
while(!isdigit(ch)) fu |= (ch == '-'), ch = gc();
while(isdigit(ch)) s = (s << 1) + (s << 3) + (ch ^ 48), ch = gc();
return fu ? -s : s;
}
template <typename T>
inline void write(T x, char ch) {
if(x < 0) pc('-'), x = -x;
static int stk[30];
int tt = 0;
do stk[++tt] = x % 10, x /= 10; while(x);
while(tt) pc(stk[tt--] ^ 48);
pc(ch);
}
template <typename T>
inline void write(T x) {
if(x < 0) pc('-'), x = -x;
static int stk[30];
int tt = 0;
do stk[++tt] = x % 10, x /= 10; while(x);
while(tt) pc(stk[tt--] ^ 48);
}
};
using namespace oi;
const int MAXN = 1e5 + 10;
int n, f[MAXN], a, b[MAXN], t[MAXN];
int c[MAXN];
inline int lowbit(int x) {
return x & -x;
}
int ask(int x) {
int res = 0;
for (;x; x -= lowbit(x)) res = max(res, c[x]);
return res;
}
void modify(int x, int y) {
for (; x <= n; x += lowbit(x)) c[x] = max(c[x], y);
}
void solve() {
n = read();
for (int i = 1; i <= n; i++)
a = read(), t[a] = i;
for (int i = 1; i <= n; i++)
b[i] = t[read()];
int ans = 0;
for (int i = 1; i <= n; i++) {
f[i] = ask(b[i] - 1) + 1;
modify(b[i], f[i]);
ans = max(ans, f[i]);
}
write(ans, '\n');
}
int main() {
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
int T = 1;
// T = read();
while(T--) solve();
return 0;
}
二分
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
namespace oi{
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using db = double;
using pll = pair<ll, ll>;
#define gc getchar
#define pc putchar
#define endl '\n'
inline ll read() {
char ch = gc(); ll fu = 0, s = 0;
while(!isdigit(ch)) fu |= (ch == '-'), ch = gc();
while(isdigit(ch)) s = (s << 1) + (s << 3) + (ch ^ 48), ch = gc();
return fu ? -s : s;
}
template <typename T>
inline void write(T x, char ch) {
if(x < 0) pc('-'), x = -x;
static int stk[30];
int tt = 0;
do stk[++tt] = x % 10, x /= 10; while(x);
while(tt) pc(stk[tt--] ^ 48);
pc(ch);
}
template <typename T>
inline void write(T x) {
if(x < 0) pc('-'), x = -x;
static int stk[30];
int tt = 0;
do stk[++tt] = x % 10, x /= 10; while(x);
while(tt) pc(stk[tt--] ^ 48);
}
};
using namespace oi;
const int MAXN = 1e5 + 10, INF = 0x3f3f3f3f;
int n, f[MAXN], a, b[MAXN], t[MAXN];
int c[MAXN], cnt;
void solve() {
n = read();
for (int i = 1; i <= n; i++) a = read(), t[a] = i;
for (int i = 1; i <= n; i++) b[i] = t[read()];
c[0] = -INF;
for (int i = 1; i <= n; i++) {
if (c[cnt] < b[i]) c[++cnt] = b[i];
else {
int l = 1, r = cnt;
while (l < r) {
int mid = l + r >> 1;
if (c[mid] > b[i]) r = mid;
else l = mid + 1;
}
c[l] = b[i];
}
}
write(cnt, '\n');
}
int main() {
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
int T = 1;
// T = read();
while(T--) solve();
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!