2020.6.13
这道题有点坑的地方可能在于窝太菜了,老是忘了a代表的是下标而不是数值然后还得了42分。
这道题思路特别白板,就是把所有必做的点排序,然后类似于分块预处理那样把点和点之间可能能做的点,即大于左端点但严格小于右端点的值全部拉进手开队列里然后做一遍lis(最长上升子序列)就行了。lis相信我不用多说了,不会有人还不会吧。
代码:
#include <bits/stdc++.h>
using namespace std;
#define limit (500000 + 5)//防止溢出
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f
#define lowbit(i) i&(-i)//一步两步
#define EPS 1e-6
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);
#define ff(a) printf("%d\n",a );
#define pi(a,b) pair<a,b>
#define rep(i, a, b) for(int i = a; i <= b ; ++i)
#define per(i, a, b) for(int i = b ; i >= a ; --i)
#define mint(a,b,c) min(min(a,b), c)
#define MOD 998244353
#define FOPEN freopen("C:\\Users\\tiany\\CLionProjects\\acm_01\\data.txt", "rt", stdin)
typedef long long ll;
typedef unsigned long long ull;
ll read(){
ll sign = 1, x = 0;char s = getchar();
while(s > '9' || s < '0' ){if(s == '-')sign = -1;s = getchar();}
while(s >= '0' && s <= '9'){x = x * 10 + s - '0';s = getchar();}
return x * sign;
}//快读
void write(ll x){
if(x < 0) putchar('-'),x = -x;
if(x / 10) write(x / 10);
putchar(x % 10 + '0');
}
int dp[limit] , lis;
int a[limit], c[limit];
int n,k;
int stk[limit],top = 0;
int main() {
#ifdef LOCAL
FOPEN;
#endif
n = read(),k = read();
rep(i ,1, k){
a[i] = read();
}
sort(a + 1, a + 1 + k);//升序
rep(i ,1, n){
c[i] = read();
}
top = 0;//手开队列
rep(i ,1, a[1]){
if(c[i] < c[a[1]])stk[++top]= c[i];
}
stk[++top] = c[a[1]];
rep(i ,2, k){
if(c[a[i]] <= c[a[i - 1]]){
return 0 * puts("impossible");
}
rep(j ,a[i - 1] , a[i]){
if(c[j] > c[a[i - 1]] && c[j] < c[a[i]]){
stk[++top] = c[j];
}
}
stk[++top] = c[a[i]];
}
rep(i , a[k] , n){
if(c[i] > c[a[k]])stk[++top] = c[i];
}
lis = 1;
dp[1] = stk[1];
rep(i ,2, top){
if(stk[i] > dp[lis]){
dp[++lis] = stk[i];
}else{
int pos = lower_bound(dp + 1, dp + 1 + lis , stk[i]) - dp;
dp[pos] = stk[i];
}
}
write(lis);
return 0;
}