UVALive 7146 Defeat The Enemy

Defeat The Enemy

Time Limit:
 3000MS  Memory Limit:  Unknown
64bit IO Format: %lld & %llu

  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.

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 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

Output

  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,

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

 

解题:贪心策略

将我方部落依战斗力,防御力小大排列,将敌方防御力,战斗力大到小排列。。

如果能打死敌人,看是否存在足够的防御力使得我方不致死,否则,使用能干掉对方的,防御力最低的我方部落

这份代码确实有问题 感谢原封大神的数据

UVALive 7146 Defeat The Enemy
 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 */
View Code

 

这是更正后的代码,感谢原封大大的指点

上面的策略是没错吧,不过不利于编程实现

将我方战斗力大到小排,敌方防御力大到小排

然后每次把我放的战斗力大于地方的防御力的战士的防御力加入到multiset中

查找比敌方攻击力大的最小的防御值,如存在,则用这个干掉敌人,自己能幸存,

否则,用能干掉敌方的防御力最小的士兵,要死一起死。。。。

感谢ACdream群中的各位巨巨的提示

UVALive 7146 Defeat The Enemy
 1 #include <bits/stdc++.h>

 2 using namespace std;

 3 const int maxn = 10100;

 4 struct node {

 5     int attack,defence;

 6 } ourSide[maxn],enemy[maxn];

 7 multiset<int>ms;

 8 int main() {

 9     int T,n,m,cs = 1;

10     scanf("%d",&T);

11     while(T--) {

12         scanf("%d %d",&n,&m);

13         for(int i = 0; i < n; ++i)

14             scanf("%d %d",&ourSide[i].attack,&ourSide[i].defence);

15         for(int i = 0; i < m; ++i)

16             scanf("%d %d",&enemy[i].attack,&enemy[i].defence);

17         sort(ourSide,ourSide+n,[](node &x,node &y)->bool{return x.attack > y.attack;});

18         sort(enemy,enemy+m,[](node &x,node &y)->bool{return x.defence > y.defence;});

19         int k = 0,ret = 0;

20         ms.clear();

21         for(int i = 0; i < m; ++i){

22             while(k < n && ourSide[k].attack >= enemy[i].defence)

23                 ms.insert(ourSide[k++].defence);

24             if(ms.empty()){

25                 ret = -1;

26                 break;

27             }

28             auto cur = ms.upper_bound(enemy[i].attack);

29             if(cur == ms.end()) cur = ms.begin();

30             if(*cur <= enemy[i].attack) ret++;

31             ms.erase(cur);

32         }

33         printf("Case #%d: %d\n",cs++,ret == -1?ret:n - ret);

34     }

35     return 0;

36 }
View Code

 

你可能感兴趣的:(live)