Uvalive 7146 defeat the Enemy

Source: Internet
Author: User

defeat the Enemy
Time Limit:
3000MS Memory Limit:Unknown 64bit IO Format:%LLD &%llu

Long ago There is a strong tribe living on the earth. They always has wars and eonquer others.One day , there is another tribe become their target. The strong tribe have decide to terminate them!!! there ismVillages in the other tribe. Each village contains a troop with attack powerEattacki, and defense powerEdefensei. Our Tribe haveNtroops to attack the enemy. Each troop also have the attack powerAttacki, and defense powerDefensei. We can use the troop to attack one enemy village and a troop can is only used to attack only one enemy village. Even if a troop survives an attack, it can ' t be used again in another attack.

The battle between 2 troops is really simple. The troops use their attack power to attack against the other troop simultaneously. If A troop ' s defense power is less than or equal to the other troop ' s attack power, it'll be destroyed. It ' s possible that both troops survive or destroy.

The main target of our tribe are to destroy all the enemy troops. Also, our tribe would like to has the most number of the troops survive in the this war.

Input

The first line of the input gives the number of test cases, T. T test Cases follow. Each test case start with 2 numbers n and m., the number of our troops and the number of enemy villages. n lines follow, each with Attacki and Defensei, the attack power and defense power of our Troop S. The next m lines describe the enemy troops. Each line consist of Eattacki and Edefensei, the attack power and defense power of enemy troops

Output

For each test ease, output one line containing ' case #x: y', where x is the test case num ber (starting from 1) and y are the max number of survive troops of our tribe. If it ' s impossible to destroy all enemy troops, output '-1 ' instead.

Limits:

1 ≤ T ≤ 100,

1 ≤ n,m ≤ 105,

1 ≤ attacki,defensei,eattacki,edefensei ≤ 109,

Sample Input

2

3 2

5 7

7 3

1 2

4 4

2 2

2 1

3 4

1 10

5 6

Sample Output

Case #1:3

Case #2:1

Problem solving: Greedy strategy

Will our tribe according to combat effectiveness, the defensive force small large arrangement, will the enemy Defense Force, the combat ability to be small arrangement.

If we can kill the enemy and see if there is enough defense to prevent us from dying, we will use our tribe with the lowest defensive ability.

1#include <bits/stdc++.h>2 #definePII pair<int,int>3 using namespacestd;4multiset< pii,less< PII > >ourside;5multiset< pii,greater< PII > >enemy;6 intn,m;7 intMain () {8     intAttack,defense,cs =1, T;9scanf"%d",&T);Ten      while(t--) { One ourside.clear (); A enemy.clear (); -scanf"%d%d",&n,&m); -          for(inti =0; I < n; ++i) { thescanf"%d%d",&attack,&defense); - Ourside.insert (Make_pair (Attack,defense)); -         } -          for(inti =0; I < m; ++i) { +scanf"%d%d",&attack,&defense); - Enemy.insert (Make_pair (Defense,attack)); +         } A          for(Auto it = Enemy.begin (); It! = Enemy.end (); + +it) { at             if(Ourside.empty () | | ourside.rbegin ()->first < it->First ) { -n =-1; -                  Break; -             } -Auto cur = ourside.upper_bound (Make_pair (it->first,it->second)); -             if(cur = =ourside.end ()) incur = ourside.lower_bound (Make_pair (It->first,0)); -             if(Cur->second <= It->second) n--; to ourside.erase (cur); +         } -printf"Case #%d:%d\n", cs++, n); the     } *     return 0; $ }Panax Notoginseng /* - 2 the 3 2 + 5 7 A 7 3 the 1 2 + 4 4 - 2 2 $  $ 2 1 - 3 4 - 1 Ten the 5 6 - */
View Code

Uvalive 7146 defeat the Enemy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.