C. Game on Permutation

链接:https://codeforces.com/problemset/problem/1860/C
洛谷链接:https://www.luogu.com.cn/problem/CF1860C
相关知识点复习:LIS最长上升子序列
链接:https://blog.csdn.net/lxt_Lucia/article/details/81206439
关键:

这题的思路在于找到LIS长度为2的点,
比如1 3 2 5 4那么显然3,2是,4,5不是,虽然3是,但不影响2是,所以我们可以看出判断是不是的通用办法:把lis插入的位置判断:如果是在第二个位置插入就ans++。
代码:

#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<cmath>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;
const int N = 3e5 + 10;
int lst[N];
int a[N];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--)
	{
		memset(a, 0, sizeof(a));
		int n; cin >> n; int ptr = 0;
		for (int i = 0; i < n; i++)cin >> lst[i];
		a[ptr] = lst[0];
		int ans = 0;
		for (int i = 1; i < n; i++)
		{
			if (lst[i] > a[ptr])//特判
			{
				ptr++;
				a[ptr] = lst[i];
				if(ptr==1)ans++;//here!
			}
			else
			{
				int l = 0, r = ptr;
				while (l < r)
				{
					int mid = l + (r - l) / 2;
					if (a[mid] > lst[i])r = mid;
					else l = mid + 1;
				}
				if (l == 1)ans++;//here:采用的是0开头
				a[l] = lst[i];
			}
		}
		cout << ans << '\n';
		
	}
	return 0;
}

刚开始读错题了,可恶!

posted on 2024-05-05 19:10  WHUStar  阅读(5)  评论(0编辑  收藏  举报