您的位置:首页 > 其它

TOJ 2199 A+B Problem的无奈 2006-07-21

2006-07-21 11:29 375 查看
You know that the first problem of an online judge system should be the "A+B Problem", but TOJ not. What a terrible! So now it comes...

Given a series of numbers, your task is to find the largest number C in the sequence, which is the sum of two other numbers A and B in the sequence. Please note that A and B may have the same value, but cannot be the same number.

Input

The first line of each test case is an integer N (1 ≤ N ≤ 1000), which is the amount of the numbers, followed by a line of N numbers.

You can assume all the numbers in the sequence are positive and not more than 108.

The input will be terminated by a zero.

Output

Output one line for each test case, indicating the largest number. If there is no such number, output "-1".

这是我要参加的天津大学的ACM网站上的一道题,我专找那些英文少的题,而我发现往往这些题反倒通过率最低.

#include <stdio.h>
int BS(int a[],int x,int right){
int left=0;
while(left<=right){
int middle=(left+right)/2;
if(x==a[middle]) return middle;
if(x>a[middle]) left=middle+1;
else right=middle-1;
}
return -1;
}
int Partition(int a[],int p,int r){
int i=p,j=r+1;
int t;
int x=a[p];
while(true){
while(a[++i]<x);
while(a[--j]>x);
if(i>=j) break;
t=a[i];
a[i]=a[j];
a[j]=t;
}
a[p]=a[j];
a[j]=x;
return j;
}

void QuickSort(int a[],int p,int r){
if(p<r){
int q=Partition(a,p,r);
QuickSort(a,p,q-1);
QuickSort(a,q+1,r);
}
}

void findABC(int a[],int n){
QuickSort(a,0,n);
int i,j,k=-1;
for(i=n-1;i>=2;i--){
for(j=n-2;j>=1;j--){
if((k=BS(a,a[i]-a[j],j-1))!=-1){
printf("%d+%d=%d/n",a[k],a[j],a[i]);
return;
}
}
}
printf("no.../n");
}

int main(){
int a[31]={10,9,8,7,6,5,4,3,1,2,
12,334,43,12,76,34,76,12,35,12,34,
66,21,99,45,67,62,81,100,90,234};
findABC(a,31);
}

我在想这题时候也出现了疏漏,但我已经不在乎了,因为我发现所有的题目差不多都要求时间在1S内,这类题不出错基本没问题,但一涉及到时间...

我用快速排序和2分查找来想办法加快速度,但我也只能测试到31,远不及题目的1000.

谁知道这个会到什么程度.

我昨夜看了这么两道题,还有一道是WORDL CUP 2006,开始我很高兴能够不长时间就有了"清晰的思路".但等到白天一写,发现时间上大大大超标,世界杯的题目要求M,N不超过300,而我用递归到了20,20基本就出不来数了...

看来这考试远比想象的困难,做出来就不容易,还要十分十分的节省时间空间,而且我看人家的代码一般也只有1K多,也就是语句效率非常高.

完蛋了,真的完蛋了.还是考虑考虑后事吧.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: