求助,大神求解,为什么是TLE?
这有个ACM题:
As a student, we all know set in math. In a set, there are some elements, just like integers,letters,words and so on. Now we have two set A and B. Their elements are integers. Now I want to know the size of the intersection of the Set A and Set B.
Input
For each test case, the first line contains two integers M and N(1<=M<=4000,1<=N<=160000).The following M lines,each line has a integer which is an element in Set A.The next following N lines, each line has a integer which is an element in Set B. The elements in A and B are all non-negative integers(0~2^31).
Output
For each test case, output the size of the intersection of Set A and Set B.
Sample Input
5 5
1
2
3
4
5
2
4
6
8
10
Sample Output
2
大意就是给了两个集合,让看看这两个集合里面有几个共同的元素。这个题里面没说集合里面元素的顺序,所以在我的程序中前面还加了个排序的程序,但这样就TLE了。
各位看看吧~~
#include <iostream>
using namespace std;
void sort(int a[],int N)
{
int i,j,k;
for(i=0;i<N-1;i++)
{
for(j=0;j<N-1-i;j++)
{
if(a[j]>a[j+1])
{
k=a[j];
a[j]=a[j+1];
a[j+1]=k;
}
}
}
}
int main()
{
int a,b;
int s[4000],d[160000];
cin>>a>>b;
for(int i=0;i<a;i++)
{
cin>>s[i];
if(s[i]<0)break;
}
for(int j=0;j<b;j++)
{
cin>>d[j];
if(d[j]<0)break;
}
sort(s,a);
sort(d,b);
int num=0,k=0;
for(int i=0; i<a && k<b; )
{
if( s[i] == d[k] )
{
num++;
k++;
i++;
continue;
}
s[i] > d[k] ? k++ : i++;
}
cout<<num;
return 0;
}
谢谢各位大神了哦~~~
[解决办法]
一时之间想不到特别好的方法,先说一下普通的。对4000的数据排序,再加上一个4000大小的标记数组。然后对160000数据的输入每个都做二分查找,找到标记加一(其实直接设为1也行)。最终统计标记数组中非零的数量,就是交集。
[解决办法]
仅供参考
//随机产生100000000个取值范围为[0~2的32次方减1]的数据,
//然后让用户输入一个数据,判断用户输入的数据是不是包含在前面随机产生的数据中。
//要求:当用户输入完成后,必须在1毫秒(千分之一秒)之内完成判断。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
long int i;
unsigned long ul;
unsigned long *d;
unsigned long ulrand(void) {
return (
(((unsigned long)rand()<<24)&0xFF000000ul)
[解决办法]
(((unsigned long)rand()<<12)&0x00FFF000ul)
[解决办法]
(((unsigned long)rand() )&0x00000FFFul));
}
void main() {
d=(unsigned long *)calloc(1<<(32-5),sizeof(unsigned long));
if (NULL==d) {
printf("Can not calloc(%d,%d)!\n",1<<(32-5),sizeof(unsigned long));
return;
}
srand(time(NULL));
for (i=0;i<100000000;i++) {
while (1) {
ul=ulrand();
if (0==(d[ul>>5]&(1lu<<(ul%32)))) {
d[ul>>5]
[解决办法]
=1<<(ul%32);
break;
}
}
if (0==i%1000000) printf("%09d,%10lu\n",i,ul);
}
while (1) {
printf("\nInput a number:");
fflush(stdout);
rewind(stdin);
if (1==scanf("%lu",&ul)) break;
}
if (d[ul>>5]&(1<<(ul%32))) printf("Include.\n" );
else printf("Not include.\n");
free(d);
}
//000000000,2135468533
//001000000,2465805973
//002000000,3844079964
//003000000,1883232874
//004000000,1204697784
//005000000,4050838287
//006000000,3802081245
//007000000,1586042671
//008000000,3119931368
//009000000, 251096899
//010000000,3491239701
//011000000,3365323844
//012000000,2191846708
//013000000,1879478195
//014000000,1112631457
//015000000,1927301519
//016000000,1717332861
//017000000,2922278240
//018000000, 694854106
//019000000, 273255526
//020000000, 398518467
//021000000,3270756812
//022000000,1500289424
//023000000,1502241936
//024000000,1770380660
//025000000,3668842116
//026000000,3255869879
//027000000,1299184024
//028000000,1072990028
//029000000, 242094712
//030000000,3789344297
//031000000,2599365925
//032000000, 962754138
//033000000,2055075654
//034000000,4083452879
//035000000, 489250842
//036000000, 611455230
//037000000, 277350616
//038000000,1597410795
//039000000,3224173662
//040000000,2291446877
//041000000,2546280575
//042000000,2509145642
//043000000,2371773252
//044000000, 635555963
//045000000,2674538666
//046000000,4253690312
//047000000,2675755514
//048000000,1269320296
//049000000,3172516920
//050000000,1430265210
//051000000, 196156173
//052000000,2470825669
//053000000,2536750977
//054000000,1182829949
//055000000,3202826434
//056000000,2263336265
//057000000, 313302924
//058000000,3630264578
//059000000,1154892716
//060000000,2985304230
//061000000,1252204837
//062000000,1292076720
//063000000, 242249250
//064000000,3999999961
//065000000, 431166416
//066000000,1366947236
//067000000,1414387330
//068000000,2143784481
//069000000,3242175409
//070000000,4158065163
//071000000,1425449573
//072000000,2493600232
//073000000,1316783455
//074000000,3723170478
//075000000,3064111466
//076000000, 408557403
//077000000,3722586955
//078000000,3801652651
//079000000,3788160154
//080000000,3329440047
//081000000,1408976868
//082000000, 471838899
//083000000,2145198260
//084000000,3781081738
//085000000,3439027738
//086000000,1150808750
//087000000,2782578638
//088000000, 85604584
//089000000,2704078162
//090000000, 584840269
//091000000,3854577719
//092000000,2823653537
//093000000, 797877025
//094000000,2248017755
//095000000,1787038685
//096000000,2816548567
//097000000, 489107494
//098000000, 911680090
//099000000,3677777147
//
//Input a number:3677777147
//Include.