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

贴一下俺的代码

Posted by cavatina2016 at 2017-09-30 17:44:43 on Problem 1743
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <algorithm>
using namespace std;
#define MAX_N	20000


int n;
int notes[MAX_N];
int ranks[16][MAX_N];
int rankscount[16];
int pos[16][MAX_N];
int len;
int prevlen;
int level;
int prevlevel;
int minlevel;


bool Less(int a, int b)
{
	if (pos[prevlevel][a] < pos[prevlevel][b]) return true;
	if (pos[prevlevel][a] > pos[prevlevel][b]) return false;
	if (pos[prevlevel][a + len - prevlen] < pos[prevlevel][b + len - prevlen]) return true;
	if (pos[prevlevel][a + len - prevlen] > pos[prevlevel][b + len - prevlen]) return false;
	if (notes[a] - notes[a + len - prevlen] < notes[b] - notes[b + len - prevlen]) return true;
	if (notes[a] - notes[a + len - prevlen] > notes[b] - notes[b + len - prevlen]) return false;
	return a < b;
}


bool Equal(int a, int b)
{
	if (pos[prevlevel][a] != pos[prevlevel][b]) return false;
	if (pos[prevlevel][a + len - prevlen] != pos[prevlevel][b + len - prevlen]) return false;
	return notes[a] - notes[a + len - prevlen] == notes[b] - notes[b + len - prevlen];
}


bool search(bool type = false)
{
	if (len == 1) {
		return true;
	}

	bool flag = false;

	for (int j = 0; j + len <= n; ++j) {
		pos[level][j] = -1;
	}

	rankscount[level] = 0;
	for (int j = 0; j < rankscount[prevlevel]; ++j) {
		int k = ranks[prevlevel][j];
		if (k + len > n) {
			continue;
		}
		if (pos[prevlevel][k] == -1 || pos[prevlevel][k + len - prevlen] == -1) {
			continue;
		}
		ranks[level][rankscount[level]++] = k;
	}

	{
		int start = 0;
		int prev = pos[prevlevel][ranks[level][start]];
		for (int k = 1; k < rankscount[level]; ++k) {
			int x = ranks[level][k], y = pos[prevlevel][x];
			if(y != prev) {
				sort(ranks[level] + start, ranks[level] + k, Less);
				start = k;
				prev = y;
			}
		}

		sort(ranks[level] + start, ranks[level] + rankscount[level], Less);
	}


	int idx = 0, start = ranks[level][0];

	pos[level][ranks[level][0]] = idx;
	for (int k = 1; k < rankscount[level]; ++k) {
		int x = ranks[level][k], y = ranks[level][k - 1];
		if (Equal(x, y)) {
			pos[level][x] = idx;
		}
		else {
			pos[level][x] = ++idx;
			if (y == start) {
				pos[level][start] = -1;
			}
			else if (y - start >= len) {
				flag = true;
				if (type) return flag;
			}
			start = x;
		}
	}

	int y = ranks[level][rankscount[level] - 1];
	if (y - start >= len) {
		flag = true;
	}

	return flag;
}


void init()
{
	for (int i = 0; i < n; ++i) {
		pos[0][i] = 0;
		ranks[0][i] = i;
	}
	rankscount[0] = n;

	len = 2, prevlen = 1;
	level = 1, prevlevel = 0;
	minlevel = 0;

	for (; len <= n / 2; ++level, len *= 2) {

		if (!search()) return;

		prevlevel = level;
		prevlen = len;
		minlevel = level;
	}
}


int calc2()
{
	if (n == 1) return 0;

	init();

	int a = 1 << minlevel, b = min(n / 2, a * 2 - 1), res = 0;
	prevlen = a;
	prevlevel = minlevel;

	while (a <= b) {
		int c = (a + b) / 2;

		len = c;
		level = 15;

		if (search(true)) {
			if (c > res) res = c;
			a = c + 1;
		}
		else {
			b = c - 1;
		}
	}

	if (res < 5) res = 0;
	return res;
}




int main()
{
	while (true) {
		scanf_s("%d", &n);
		if (!n) break;

		for (int i = 0; i < n; ++i) {
			scanf_s("%d", &notes[i]);
		}
		printf("%d\n", calc2());
	}
	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