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

狂wa啊,有空的帮我看下哦...

Posted by kansas at 2007-07-17 10:39:32 on Problem 3261
// 先是根据数据元素的大小,对下标进行排序
// 然后根据下标,求出每两个相邻元素最长匹配串长
//  然后二分枚举答案...
// 感觉只要不是所有元素全部相同还是可以通过的.
// 算法有问题么?


#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

#define SIZE 20011
int array[SIZE];
struct Idx
{
	int i;
}idx[SIZE];
int n , k;
int eq[SIZE];

int Match (int a , int b)
{
	int cnts = 0;

	while (a < n && b < n && array[a] == array[b])
	{
		cnts++;
		a++;
		b++;
	}
	return cnts;
}

inline bool operator < (const Idx &a , const Idx &b)
{
	return array[a.i] < array[b.i];
}

int main ()
{
#ifndef ONLINE_JUDGE
	freopen ("in.txt","r",stdin);
#endif
	int i;


	while (EOF != scanf ("%d%d",&n , &k))
	{
		for (i = 0;i < n;i++)
		{
			scanf ("%d",&array[i]);
			idx[i].i = i;
		}
		sort (idx , idx + n );
		memset (eq , 0 , sizeof (eq));

		for (i = 1;i < n;i++)
		{
			eq[i] = Match (idx[i].i , idx[i - 1].i);
		}
		
		int left , right , mid;
		int cnts;
		bool can;
		
		left = 0;
		right = n + 1;
		//cout << left << " " << right << endl;
		while (left < right)
		{
			// 二分最长匹配
			mid = (left + right) / 2 + 1;
			
			for (i = 1 , cnts = 1 , can = false;i < n;i++)
			{
				if (eq[i] >= mid)
					cnts++;
				else
					cnts = 1;
				if (cnts >= k)
				{
					can = true;
					break;
				}
			}
			
			if (can)
				left = mid;
			else
				right = mid - 1;
		}	
		
		cout << left << endl;
	}


	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