AtCoder-2561 Big Array

AtCoder-2561 Big Array

Problem Statement
There is an empty array. The following N operations will be performed to insert integers into the array. In the i-th operation (1≤i≤N), bi copies of an integer ai are inserted into the array. Find the K-th smallest integer in the array after the N operations. For example, the 4-th smallest integer in the array {1,2,2,3,3,3} is 3.
Constraints

  • 1≤N≤105
  • 1≤ai,bi≤105
  • 1≤K≤b1…+…bn
  • All input values are integers.

Input
Input is given from Standard Input in the following format:
N K
a1 b1
:
aN bN
Output
Print the K-th smallest integer in the array after the N operations.
Sample Input 1
3 4
1 1
2 2
3 3
Sample Output 1
3
The resulting array is the same as the one in the problem statement.
Sample Input 2
10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
Sample Output 2
1

解题思路:首先我们要清楚题目是什么意思,该题就是说,将b[i] 个 a[i] 放进一个数组里,同时给定一个K, 求该数组里第K小的数是多少。这时就很清楚的知道,只需要将a, b分别存入,再进行一个sort排序就行,可以使用结构体,将a和b分别表示为两个元素。
而我使用的是multiset,因为在stl函数中它可以村春重复的数据,并且当数据用pair插入后的自动排序,且默认排序方式为从小到大。(注意因为数据过大,一定要使用long long)
AC代码如下:

#include<bits/stdc++.h>

using namespace std;

int main()
{
	long long n, k;
	long long sum = 0;
	multiset< pair<long long, long long> >m;
	scanf("%lld%lld", &n, &k);
	while(n--)
	{
		long long a, b;
		scanf("%lld%lld", &a, &b);
		m.insert(make_pair(a, b));	
	}
	for(auto it = m.begin();it!=m.end();it++)
	{
		sum += it->second;
		if(sum >= k)
		{
			cout << it->first << endl;
			break;			
		}
	}	
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alan wade

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值