https://ac.nowcoder.com/acm/contest/12548/L
L题
我觉得是一个很好的题,很符合题意很明确,但没有思路的题。其实仔细想想,会发现这是一个裸的最长下降子序列。只需要求的时候标记一下就行。但是不能用 d p dp dp去写,会超时。做过最长上升序列的应该都知道二分优化。
#include<bits/stdc++.h>
using namespace std;
// clock_t start, end;
// start = clock();
// end = clock();
// cout << (double) (end - start) / CLOCKS_PER_SEC << endl;
//ios::sync_with_stdio(false);
//#define int long long
#define rep(i, x, y) for(int i=(x);i<=(y);++i)
#define dep(i, x, y) for(int i=(x);i>=(y);--i)
#define gcd(a, b) __gcd(a,b)
const long long mod = 1e9 + 7;
const int maxn = 2e6 + 10;
int lowbit(int x) { return x & -x; }
bool ispow(int n) { return (n & (n - 1)) == 0; }//O(1) 判断是否是 2^k(2的k次方)
int a[maxn];
int ans[maxn],len;
int anss[maxn];
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int check(int l,int r,int x){
int mid = (l + r)>>1;
while(l<r){
if(ans[mid]>x)
l = mid + 1;
else r = mid;
mid = (l+r)>>1;
}
return l;
}
signed main() {
int t;
t = read();
while(t--){
int n;
n =read();
for(int i =1;i<=n;i++){
a[i] =read();
}
len = 1,anss[1] = 1,ans[1]=a[1];
for(int i = 2;i<=n;i++){
if(a[i]<ans[len]){
ans[++len] = a[i];
anss[i] = len;
continue;
}
int res =check(1,len,a[i]);
ans[res] = a[i];
anss[i] = res;
}
printf("%d\n",len);
for(int i = 1;i<=n;i++){
printf("%d ",anss[i]);
}
printf("\n");
}
return 0;
}