CodeForces - 298D Fish Weight(思维)

It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 ≤ w2 ≤ … ≤ wk holds.

Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she’s caught. Given the type of the fish they’ve caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob’s. In other words, does there exist a sequence of weights wi (not necessary integers), such that the fish caught by Alice has a strictly larger total weight?
Input

The first line contains three integers n, m, k (1 ≤ n, m ≤ 105, 1 ≤ k ≤ 109) — the number of fish caught by Alice and Bob respectively, and the number of fish species.

The second line contains n integers each from 1 to k, the list of fish type caught by Alice. The third line contains m integers each from 1 to k, the list of fish type caught by Bob.

Note that one may have caught more than one fish for a same species.
Output

Output “YES” (without quotes) if it is possible, and “NO” (without quotes) otherwise.
Examples
Input

3 3 3
2 2 2
1 1 3

Output

YES

Input

4 7 9
5 2 7 3
3 5 2 7 3 8 7

Output

NO

Note

In the first sample, if w1 = 1, w2 = 2, w3 = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5.

In the second sample, the fish that Alice caught is a subset of Bob’s. Therefore, the total weight of Bob’s fish is always not less than the total weight of Alice’s fish.

题目链接
参考题解

借用题解博主的解释: 由于是 0 < w1 ≤ w2 ≤ … ≤ wk…当B中排序最后的鱼序号有大于A排序最后的鱼…那么将B的这鱼重量和A当前最大的鱼定为相等…则这两条鱼的重量相抵消 .所以如果这么做…只要A还有鱼…就能保证每次B所有的总重量都不会大于A的总重量…这么抵消下去…一旦A有条鱼的序号大于B所剩的所有鱼…那么A就可以胜利了…因为可以令小于A这条鱼的所有鱼重量无穷小…

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, k, a[maxn], b[maxn];

bool judge()
{
       while (m && n && b[m] >= a[n]) m--,n--;
       if (n && (!m || a[n] > b[m])) return true;
       return false;
}
int main()
{
       int i, j;
       while (~scanf("%d%d%d", &n, &m, &k))
       {
             for (i = 1; i <= n; i++) scanf("%d", &a[i]);
             sort(a + 1, a + 1 + n);
             for (i = 1; i <= m; i++) scanf("%d", &b[i]);
             sort(b + 1, b + 1 + m);
             if (judge()) printf("YES\n");
             else   printf("NO\n");
       }
       return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/84582556