CodeForces 160C Find Pair

题目直通车:http://codeforces.com/problemset/problem/160/C

知识要点:

C. Find Pair
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You've got another problem dealing with arrays. Let's consider an arbitrary sequence containing n (not necessarily different) integers a1,a2, ..., an. We are interested in all possible pairs of numbers (aiaj), (1 ≤ i, j ≤ n). In other words, let's consider all n2 pairs of numbers, picked from the given array.

For example, in sequence a = {3, 1, 5} are 9 pairs of numbers: (3, 3), (3, 1), (3, 5), (1, 3), (1, 1), (1, 5), (5, 3), (5, 1), (5, 5).

Let's sort all resulting pairs lexicographically by non-decreasing. Let us remind you that pair (p1q1) is lexicographically less than pair (p2q2) only if either p1 < p2, or p1 = p2 and q1 < q2.

Then the sequence, mentioned above, will be sorted like that: (1, 1), (1, 3), (1, 5), (3, 1), (3, 3), (3, 5), (5, 1), (5, 3), (5, 5)

Let's number all the pair in the sorted list from 1 to n2. Your task is formulated like this: you should find the k-th pair in the ordered list of all possible pairs of the array you've been given.

Input

The first line contains two integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ n2). The second line contains the array containing n integers a1a2, ...,an ( - 109 ≤ ai ≤ 109). The numbers in the array can coincide. All numbers are separated with spaces.

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cincout, streams or the %I64dspecificator instead.

Output

In the single line print two numbers — the sought k-th pair.

Examples
input
2 4
2 1
output
2 2
input
3 2
3 1 5
output
1 3
Note

In the first sample the sorted sequence for the given array looks as: (1, 1), (1, 2), (2, 1), (2, 2). The 4-th of them is pair (2, 2).

The sorted sequence for the array from the second sample is given in the statement. The 2-nd pair there is (1, 3).


题目大意:

大概就是给你几个数去形成pair,然后查找按照升序排列的第几个pair.

例如 输入 2,4 表示两个数字,查找第四个pari。两个数字 为 2,1 那么形成 <1,1>  <1,2>  <2,1> <2,2>四个pair 输出第四个为 《2,2》.

解题思路:

首先先将数字排序,之后确定第一个数字为 a[(k-1)/n+1]。(因为每一个数字都能与其他以及包括自己的数字组成对,所以第一个数字为a[(k-1)/n+1];

之后来确定第二个数字

假如我同样运用方法一来确定那么可能存在问题 例如 当样例为 {1,1,3}时,顺序为<1,1>  <1,1>  <1,3>  <1,1> <1,1><1,3> <3,1> <3.1> <3.3>

而正确的 顺序为<1,1> <1,1> <1,1><1,1> <1,3><1,3><3,1><3,1>

所以说我们采取的方法 

1.计算和第一个数字相同的数字 firstnum

2.减去第一个数字之前的数字个数乘以 n (即表示第一个以firstnum 开头的对)

3.计算lastnum    相当于将整个数组放大 重复次数倍

a.当lastnumloc 小于重复次数时候   lastnum = a[0] 

       b.当lastnumloc 大于重复次数时候 通过取余计算 lastnum

例如 {1,1,3}时

首先计算 fistnum 得 1, 计算出1重复的次数为 2

类似的将数组看作放大 2倍 (其实没有) {1,1,1,1,3,3}

找寻到lastnum.

代码:

#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
int main()
{
	long long  n,k,firstnum,lastnum;
	long long a[100005];
	long long cnt,firstnumloc,lastnumloc;
	int i;
	scanf("%lld%lld",&n,&k);
	for(i=1;i<=n;i++)
	scanf("%lld",&a[i]);
	sort(a+1,a+n+1);
	firstnum=a[(k-1)/n+1];
	cnt=0;
	for(i=1;i<=n;i++)                 
	{
		if(a[i]==firstnum)
		cnt++;
	}
	for(i=1;i<=n;i++)
	{
		if(a[i]==firstnum)
		{
			firstnumloc=i;
			break;
		}
	}
	lastnumloc=k-(firstnumloc-1)*n;	
	if(lastnumloc<=cnt)
	lastnum=a[1];
	else 
	lastnum=a[(lastnumloc-1)/cnt+1];
	printf("%lld %lld\n",firstnum,lastnum);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值