You are given two arrays A and B consisting of integers, sorted in non-decreasing order. Check whether it is possible to choose knumbers in array A and choose m numbers in array B so that any number chosen in the first array is strictly less than any number chosen in the second array.
The first line contains two integers nA, nB (1 ≤ nA, nB ≤ 105), separated by a space — the sizes of arrays A and B, correspondingly.
The second line contains two integers k and m (1 ≤ k ≤ nA, 1 ≤ m ≤ nB), separated by a space.
The third line contains nA numbers a1, a2, ... anA ( - 109 ≤ a1 ≤ a2 ≤ ... ≤ anA ≤ 109), separated by spaces — elements of array A.
The fourth line contains nB integers b1, b2, ... bnB ( - 109 ≤ b1 ≤ b2 ≤ ... ≤ bnB ≤ 109), separated by spaces — elements of array B.
Print "YES" (without the quotes), if you can choose k numbers in array A and m numbers in array B so that any number chosen in array Awas strictly less than any number chosen in array B. Otherwise, print "NO" (without the quotes).
3 3 2 1 1 2 3 3 4 5
YES
3 3 3 3 1 2 3 3 4 5
NO
5 2 3 1 1 1 1 1 1 2 2
YES
In the first sample test you can, for example, choose numbers 1 and 2 from array A and number 3 from array B (1 < 3 and 2 < 3).
In the second sample test the only way to choose k elements in the first array and m elements in the second one is to choose all numbers in both arrays, but then not all the numbers chosen in A will be less than all the numbers chosen in B:
.
题意自己翻译,水题,取a数组k个,取最小的,b数组取m个,取最大的,直接比较新数组a1的最大值和新数组b1的最小值,这两个值的大小关系。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5;
int main()
{
int na,nb;
int k,m;
int a[N+5],b[N+5];
cin>>na>>nb;
cin>>k>>m;
for(int i=0;i<na;i++)
cin>>a[i];
for(int i=0;i<nb;i++)
cin>>b[i];
sort(a,a+na);
sort(b,b+nb);
reverse(b,b+nb);
if(a[k-1]<b[m-1])
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}
数组选择判断
本文介绍了一个简单的算法问题,即如何从两个已排序的整数数组中分别选择k个和m个元素,使得第一个数组中的所有选中元素严格小于第二个数组中的所有选中元素。通过实例演示了如何快速解决这个问题。
878

被折叠的 条评论
为什么被折叠?



