标签:
Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. One day, there is another tribe become their target. The strong tribe has decide to terminate them!!! There are m villages in the other tribe. Each village contains a troop with attack power EAttacki, and defense power EDefensei. Our tribe has n troops to attack the enemy. Each troop also has the attack power Attacki, and defense power Defensei. We can use at most one troop to attack one enemy village and a troop can only be 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 are 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 will be destroyed. It’s possible that both troops survive or destroy.
The main target of our tribe is to destroy all the enemy troops. Also, our tribe would like to have most number of troops survive in this war.
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 troops. The next m lines describe the enemy troops. Each line consist of EAttacki and EDefensei, the attack power and defense power of enemy troops
For each test ease, output one line containing ‘Case #x: y’, where x is the test case number (starting from 1) and y is 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,
2
3 2
5 7
7 3
1 2
4 4
2 2
2 1
3 4
1 10
5 6
Case #1: 3
Case #2: -1
解题:贪心策略
将我方部落依战斗力,防御力小大排列,将敌方防御力,战斗力大到小排列。。
如果能打死敌人,看是否存在足够的防御力使得我方不致死,否则,使用能干掉对方的,防御力最低的我方部落
1 #include <bits/stdc++.h> 2 #define pii pair<int,int> 3 using namespace std; 4 multiset< pii,less< pii > >ourSide; 5 multiset< pii,greater< pii > >enemy; 6 int n,m; 7 int main() { 8 int attack,defense,cs = 1,T; 9 scanf("%d",&T); 10 while(T--) { 11 ourSide.clear(); 12 enemy.clear(); 13 scanf("%d %d",&n,&m); 14 for(int i = 0; i < n; ++i) { 15 scanf("%d %d",&attack,&defense); 16 ourSide.insert(make_pair(attack,defense)); 17 } 18 for(int i = 0; i < m; ++i) { 19 scanf("%d %d",&attack,&defense); 20 enemy.insert(make_pair(defense,attack)); 21 } 22 for(auto it = enemy.begin(); it != enemy.end(); ++it) { 23 if(ourSide.empty() || ourSide.rbegin()->first < it->first) { 24 n = -1; 25 break; 26 } 27 auto cur = ourSide.upper_bound(make_pair(it->first,it->second)); 28 if(cur == ourSide.end()) 29 cur = ourSide.lower_bound(make_pair(it->first,0)); 30 if(cur->second <= it->second) n--; 31 ourSide.erase(cur); 32 } 33 printf("Case #%d: %d\n",cs++,n); 34 } 35 return 0; 36 } 37 /* 38 2 39 3 2 40 5 7 41 7 3 42 1 2 43 4 4 44 2 2 45 46 2 1 47 3 4 48 1 10 49 5 6 50 */
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4478337.html