# 50分求一道ACM题SIGSEGV原因 晕了最烦这种错了

mopishv0 2008-04-02 07:25:12
http://acm.hrbeu.edu.cn/index.php?act=problem&proid=5012&cid=0

There are two sequences A and B with N (1<=N<=10000) elements each. All of the elements are positive integers. Given C=A*B, where '*' representing Cartesian product, c = a*b, where c belonging to C, a belonging to A and b belonging to B. Your job is to find the K'th largest element in C, where K begins with 1.

Input

Input file contains multiple test cases. The first line is the number of test cases. There are three lines in each test case. The first line of each case contains two integers N and K, then the second line represents elements in A and the following line represents elements in B. All integers are positive and no more than 10000.

Output

For each case output the K'th largest number.

Sample Input

2
2 1
3 4
5 6
2 3
2 1
4 8

Sample Output

24
8

``````
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <search.h>
using namespace std;

bool cmp ( const double a , const  double b )
{
return b<a;
}

int main()
{
int num;
scanf("%d",&num);
while(num--)
{
int n,k;
scanf("%d%d",&n,&k);
vector<int>a;
for(int i=0;i<n;i++)
{
double xx;
scanf("%lf",&xx);
a.push_back(xx);
}
vector<int>b;
for(int i=0;i<n;i++)
{
double xx;
scanf("%lf",&xx);
b.push_back(xx);
}
sort(a.begin(),a.end(),cmp);
sort(b.begin(),b.end(),cmp);
double max=b[0]*a[0];
int kth=(k+1)/3;
if(k==kth*3-1&&k>1)
max=a[kth]*b[kth-1]>=a[kth-1]*b[kth]?a[kth]*b[kth-1]:a[kth-1]*b[kth];
if(k==kth*3&&k>1)
max=a[kth]*b[kth-1]<a[kth-1]*b[kth]?a[kth]*b[kth-1]:a[kth-1]*b[kth];
if(k==kth*3+1&&k>1)
max=a[kth]*b[kth];
printf("%.0lf\n",max);
}
return 0;
}

``````
...全文
367 18 打赏 收藏 举报

18 条回复

seerjk 2010-09-08
• 打赏
• 举报

dartzm 2008-05-01
• 打赏
• 举报

meiZiNick 2008-05-01
• 打赏
• 举报

soyboydoy 2008-04-21
• 打赏
• 举报

。。。
fallinleave 2008-04-21
• 打赏
• 举报

(因为 P(0, j+1) 是这个子矩阵的最小元素，而 C < P(0, j+1))。

soyboydoy 2008-04-21
• 打赏
• 举报

int findKth(int a[], int s, int e, k){
int p = partition(a, s, e, k);
if(p == k) return a[p];
if(p > k) findKth(a, s, p-1, k);
else findKth(a, p+1, e, k);
}

mopishv0 2008-04-21
• 打赏
• 举报

[Quote=引用 1 楼 mopishv0 的回复:]

[/Quote]
[Quote=引用 11 楼 fhtingtian 的回复:]

1
4 4
9 7 5 3
8 4 2 1

[/Quote]

bfhtian 2008-04-11
• 打赏
• 举报

1
4 4
9 7 5 3
8 4 2 1

mopishv0 2008-04-05
• 打赏
• 举报

10000*10000很大的

oo 2008-04-05
• 打赏
• 举报

mopishv0 2008-04-05
• 打赏
• 举报

mopishv0 2008-04-05
• 打赏
• 举报

hankman 2008-04-03
• 打赏
• 举报

xbt746 2008-04-03
• 打赏
• 举报

ryfdizuo 2008-04-03
• 打赏
• 举报

skineffect 2008-04-03
• 打赏
• 举报

• 打赏
• 举报

mopishv0 2008-04-02
• 打赏
• 举报

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-04-02 07:25

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下