CF-298D(贪心)
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, then0 < 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?
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 mintegers 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 "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise.
3 3 3
2 2 2
1 1 3
YES
4 7 9
5 2 7 3
3 5 2 7 3 8 7
NO
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.
题意:有k种鱼,第i+1种鱼的重量小于等于第i种鱼的重量。两个熊捕鱼,第一个熊捕了m条,第二个熊捕了n条。给出两个熊捕的每条鱼对应的种类。按照i+1种鱼质量大于等于i种鱼的规则给每种鱼分配一个质量,如果第一只熊捕鱼的总质量严格大于第二只熊捕鱼的总质量则第一只熊赢。问有没有一种分配方案让第一只熊赢。
思路:贪心思想。将每只熊捕的鱼按照种类从大到小排列。从头开始比较,一旦有熊一的鱼的种类大于熊二的鱼的种类,则熊一有机会赢。
考虑这种情况:
熊1 num1[i]:9 8 8 8 6 6 1
熊2 num2[i]:10 10 9 8 7 5 3 2 2 2
鱼的重量:9999999 9999999 9999999 9999999 9999999 0.1 0.1 0.1 0.1 0.1
鱼的种类:10 9 8 7 6 5 4 3 2 1
利用贪心思想,种类从大到小分配权值。一一比较。如果num1[i]<=num2[i],则让第num2[i]到num1[i]种鱼的权值一样。这样的目的是尽可能的保证熊一与熊二的总质量差最小。
一旦出现num1[i]>num2[i]。则将num1[i]种鱼分配成一个特别大的值,将num2[i]种分配成一个特别小的值。让这2个差值大到足可以忽略后面的一切鱼的重量差。如上面的分配方式。
因此,排序后一一比较就行。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 int n, m, k; 8 int num1[100010]; 9 int num2[100010]; 10 11 bool cmp(int a, int b){ 12 return a > b; 13 } 14 15 int main() 16 { 17 while(~scanf("%d%d%d", &n, &m, &k)){ 18 for (int i = 0; i < n; i++){ 19 scanf("%d", &num1[i]); 20 } 21 for (int i = 0; i < m; i++){ 22 scanf("%d", &num2[i]); 23 } 24 25 sort(num1, num1 + n, cmp); 26 sort(num2, num2 + m, cmp); 27 28 bool flag = false; 29 for (int i = 0; i < n; i++){ 30 if(num1[i] > num2[i]){//一旦发现熊一的种数大于熊二的种数,则熊一能赢。 31 flag = true; 32 break; 33 } 34 } 35 36 if(flag) printf("YES\n"); 37 else printf("NO\n"); 38 } 39 return 0; 40 }

浙公网安备 33010602011771号