CSP模拟52联测14 C.天竺葵
CSP模拟52联测14 C.天竺葵
题目大意
给定两个长度为
需要在
输出长度
思路
感觉和
考虑
设
朴素的转移是
70分代码
#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define LL long long
using namespace std;
const int N = 1e6 + 5;
const LL inf = 1e12 + 5;
int n , ans , ans1 , lis[N] , len;
LL f[N] , a[N] , b[N] , min1;
int fans (int l , int r , LL x) {
if (l == r) {
return a[lis[l]] > x ? l : inf;
}
else {
int mid = l + r >> 1;
if (a[lis[mid]] >= x) return min (mid , fans (l , mid , x));
else return fans (mid + 1 , r , x);
}
}
int main () {
freopen ("C.in" , "r" , stdin);
freopen ("C.out" , "w" , stdout);
int flg = 0 , x;
scanf ("%d" , &n);
fu (i , 1 , n) scanf ("%lld" , &a[i]);
fu (i , 1 , n) {
scanf ("%lld" , &b[i]);
if (b[i] != 1) flg = 1;
}
if (!flg) {
lis[len = 1] = 1;
fu (i , 2 , n) {
if (a[lis[len]] < a[i]) {
lis[++len] = i;
continue;
}
x = fans (1 , len , a[i]);
lis[x] = i;
}
printf ("%d" , len);
return 0;
}
ans = 1 , f[1] = a[1];
min1 = a[1];
fu (i , 2 , n) f[i] = 1e12 + 5;
fu (i , 2 , n) {
ans1 = 0;
fu (j , 1 , ans + 1) {
if (a[i] > f[j - 1] * b[j - 1]) {
f[j] = min (f[j] , a[i]);
ans1 = max(ans1 , j);
}
}
min1 = min (min1 , a[i]);
ans = max (ans , ans1);
}
printf ("%d" , ans);
return 0;
}
我们发现是转移的时候太慢了。
假设当前要处理的是
我们把现在的
因为
因为
所以现在要更新的就只有
直接遍历一遍,二分查找
时间复杂度
code
#include <bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define LL long long
using namespace std;
const int N = 1e7 + 5;
const LL inf = 1e12 + 5;
int n , ans , ans1 , lis[N] , len;
__int128 f[N] , a[N] , b[N] , min1;
int fans (int l , int r , LL x) {
if (l == r) {
return a[lis[l]] > x ? l : inf;
}
else {
int mid = l + r >> 1;
if (a[lis[mid]] >= x) return min (mid , fans (l , mid , x));
else return fans (mid + 1 , r , x);
}
}
int main () {
freopen ("C.in" , "r" , stdin);
freopen ("C.out" , "w" , stdout);
LL x;
scanf ("%d" , &n);
fu (i , 1 , n) scanf ("%lld" , &x) , a[i] = x;
fu (i , 1 , n) scanf ("%lld" , &x) , b[i] = x;
int k;
ans = 1 , f[1] = a[1];
min1 = a[1];
fu (i , 2 , n) {
k = lower_bound(f + 1 , f + ans + 1 , a[i]) - f - 1;
if (k == ans) {
if (a[i] > b[ans] * f[ans]) f[++ans] = a[i];
}
else {
if (a[i] > b[k] * f[k]) f[k + 1] = a[i];
}
}
printf ("%d" , ans);
return 0;
}
如果人生会有很长,愿有你的荣耀永不散场
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端