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 xebec at 2009-05-11 15:34:48 on Problem 1068
Problem:		1068
Memory:		336K
Time:		0MS
Language:	GCC
Code Length:	686B

#include <stdio.h>

static int stack[255];
static int top = 0;

int main() {
	int n_case, n, v, pv, i, j, k;

	scanf("%d\n", &n_case);
	for(i=0; i<n_case; ++i) {
		scanf("%d\n", &n);

		pv = 0;
		top = 1;
		stack[0] = 0;

		for(j=0;j<n;++j) {
			scanf("%d", &v);

			if(j != 0) printf(" ");
			if(v > pv + 1) {
				for(k=0; k<v-pv-2; ++k) {
					stack[top++] = 0;
				}

				stack[top++] = 1;

				printf("1");
				
				pv = v;
			}
			else if(v > pv) {
				stack[top-1] += 1;
				printf("1");
				
				pv = v;
			}
			else {
				v = stack[--top] + 1;
				printf("%d", v);

				stack[top-1] += v;
			}
		}
		printf("\n");
	}
	return 0;
}

/********************************************/
我是新手,请高手指点一下为何内存占用会那么大?
栈分配了那么多的空间让我很苦恼,不过即便这样也不该占那么多的内存啊
256 * 4 也只有 1K 而已

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