Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

LIS问题,一下子A了

Posted by zoujing1978 at 2018-08-13 15:23:23 on Problem 3670
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;

template <class T, class BinaryPredicate>
int LIS(T* pi, T* d, int p, BinaryPredicate comp)
{
	int ans = 1;
	d[0] = pi[0];
	for (int i = 1; i < p; i++)
	{
		if (!comp(pi[i], d[ans - 1]))
		{
			d[ans] = pi[i];
			ans++;
		}
		else
		{
			int idx = upper_bound(d, d + ans, pi[i], comp) - d;
			d[idx] = pi[i];
		}
	}
	return ans;
}

int main()
{
	int n = 0;
	while (scanf("%d", &n) == 1 && n > 0)
	{
		int* a = new int[n];
		int* d = new int[n];
		for (int i = 0; i < n; ++i)
		{
			scanf("%d", &a[i]);
		}
		// 考虑升序
		int ans1 = n - LIS(a, d, n, less<int>());
		// 考虑降序
		int ans2 = n - LIS(a, d, n, greater<int>());
		printf("%d\n", min(ans1, ans2));
		delete[] d;
		delete[] a;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator