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

Re:同样是暴力,为什么一个500+,一个tle呢? 麻烦大牛解释一下,谢谢啦.

Posted by msclub_acm_13 at 2008-12-30 22:45:35 on Problem 2769
In Reply To:同样是暴力,为什么一个500+,一个tle呢? 麻烦大牛解释一下,谢谢啦. Posted by:ct314171238 at 2008-11-20 18:40:08
> 这个是tle的代码:
> #include <iostream>
> using namespace std;
> 
> int flag[1000000];
> 
> int main() {
> 	int n, g, s[301];
> 	int i, j, tmp;
> 	scanf("%d", &n);
> 	while(n--){
> 		scanf("%d", &g);
> 		for(i=1; i<=g; i++) scanf("%d", s+i);
> 		for(j=1; ;j++){
> 			memset(flag,0,sizeof(flag));  <--这里TLE
> 			for(i=1; i<=g; i++){
> 				tmp = s[i]%j;
> 				if(flag[tmp]) break;
> 				flag[tmp] = 1;
> 			}
> 			if(i>g) break;
> 		}
> 		printf("%d\n",j);
> 	}
> 	return 0;
> }
> 

我一开始跟你一样TLE,后来把memset(flag, 0, sizeof(flag))去掉,每次保存flag[]置1的位置,退出循环前设置那些位置回0。就变成100ms+了。。。

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