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;
} 

猜你喜欢

转载自blog.csdn.net/qq_43704563/article/details/99617797