永发信息网

C语言题目,超时了,该如何处理。。

答案:2  悬赏:80  手机版
解决时间 2021-02-28 16:52
http://tieba.baidu.com/p/2875513553

3114: {A}∩{B}

时间限制(普通/Java):1500MS/4000MS
运行内存限制:65536KByte
总提交: 1038 测试通过:294

描述

Given two integer sets A and B
sorted descendingly, you are asked to output the element count of A∩B.

输入

Standard input will contain multiple test cases.
The first line of the
input is a single integrate T (1 <= T <= 50) which is the number of test
cases. then T consecutive test cases followed.
In each test case, there has
four lines.
The first line contains the element count N(1<=N<=100000)
for the first set.
The second line contains N integers in the first set.

The third line contains the element count M(1<=M<=100000) for the
second set.
The fourth line contains M integers in the second set.

NOTE: there will be no duplicate elements in each sets and all the integers
in each set have been sorted descendingly.

输出

For each test case in the
input, there's only one line output that contains the element count of the
intesection of the two sets.

样例输入

1
5
5 4 3 2 1
4
5 3 1 -1

样例输出

3

题目来源

台州学院第二届新生程序设计竞赛

题目上传者

crq

http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3114
具体讲一下!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

#include
int main()
{
int T,n,m,i,j,s,k;
int a[100000],b[100000];
scanf("%d",&k);
while(k--)
{ int l=0;
scanf("%d",&m);
for(i=0;i {
scanf("%d",&a[i]);
}
scanf("%d",&n);
for(j=0;j {
scanf("%d",&b[j]);
for(i=0;i {
if(a[i]==b[j])
l++;
}
}

printf("%d\n",l);
}
}
最佳答案
数量比较大,用简单的线性搜索是不行的啦,题目已经说明了输入的集合是降序排好而且没有重复元素的了,对每个b[j]在a数组中可以使用折半搜索。更进一步的,如果能充分领悟到两个集合都是降序的含义,则可使程序跑得更快。
更快的代码:#include 
#include 
int main()
{
    int i, j, k, n, m, s;
    int a[100001], b[100001];
    scanf("%d", &k);
    while(k--)
    {
        scanf("%d", &n);
        for(i=0; i < n; i++)
            scanf("%d", &a[i]);
        a[n] = INT_MIN;
        scanf("%d", &m);
        for(j=0; j < m; j++)
            scanf("%d", &b[j]);
        b[m] = INT_MIN;

        i = j = s = 0;
        while(1)
        {
            while(a[i] > b[j])
                i++;
            while(b[j] > a[i])
                j++;
            if(i == n || j == m)
                break;
            if(a[i] == b[j])
            {
                s++;
                i++;
                j++;
            }
        }
        printf("%d\s", s);
    }
}
全部回答
本题应采用二分查找。依次从较短的集合中取元素,然后到较长的集合中进行二分查找。时间复杂度将为O(nlogm),n为较短集合的长度,m为较长的集合的长度。 #include int Search(int *s, int first, int last, int key) { int mid, left = first, right = last; while(left <= right) { mid = (left + right) / 2; if(key == s[mid]) return mid; if(key > s[mid]) right = mid - 1; else left = mid + 1; } return 0; } void main( ) { int a[50][100000], b[50][100000], m[50], n[50], case_count, *set_short, *set_long, len_short, len_long, first, last, index, i, j, count; scanf("%d", &case_count); for(i = 0; i < case_count; i++) { scanf("%d", &m[i]); for(j = 1; j <= m[i]; j++) scanf("%d", &a[i][j]); scanf(%d", &n[i]); for(j = 1; j <= n[i]; j++) scanf(“%d", &b[i][j]); } for(i = 0; i < case_count; i++) { count = 0; if(m[i] < n[i]) { set_short = a[i]; set_long = b[i]; len_short = m[i]; len_long = n[i]; } else { set_short = b[i]; set_long = a[i]; len_short = n[i]; len_long = m[i]; } for(j = 1, first = 1, last = len_long; (j <= len_short) && (first <= last); j++) { index = Search(set_long, first, last, set_short[j]); if(index) { count++; first = index + 1; } else first++; } printf("%d\n", count); } }
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
潮流美发沙龙这个地址在什么地方,我要处理点
濮阳市油田一中怎么样,片外的孩子上学要收其
奥迪a6l30fsi比20丅fs燃油大多少
我想包工程
呼斯坦希力地址在什么地方,想过去办事
骑车摔了一跤,膝盖没有淤青,也没有肿胀,但
分子式为c7h6o2的某笨的含氧衍生物,其中能和n
江海龙床垫地址在什么地方,想过去办事
昆山市陆杨水泥制品厂地址在什么地方,想过去
我是18款X5运动版,刚提没多久,用不用改音箱
是不是等级差得多血源就连不了
【平均数的意义】众数、中位数、平均数表示什
中国电信江南大道中营业厅地址在哪,我要去那
耳机插上能说话,为什么话筒不能说话呢
人事做考勤怎么做表格
推荐资讯
我现在才12岁,现在眼睛近视475度,怎么样才
芙蓉兴盛圆圆便利店这个地址在什么地方,我要
什么地叫喊
家长怎么指导亲子阅读什么是亲子共读
怡洺投资地址有知道的么?有点事想过去
急急急!宝宝大便明明是棕黑色的为什么化验出
I don't mind if he (come),but if he (come)
手机京东付款方式只有手机短信验证,怎么改成
淘淘小丸子童装特价店万达店这个地址在什么地
分享:如何在豆瓣找到自己喜欢的资源
美剧越狱里出现一个什么海文监狱
明天我就要考试了,大家说说怎样调整心态?谢
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?