UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

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.











 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <set>
 6 using namespace std;
 8 const int MAXN = 100010;
10 struct Node {
11     int atk, def;
12     void read() {
13         scanf("%d%d", &atk, &def);
14     }
15 };
17 Node a[MAXN], b[MAXN];
18 int n, m, T;
20 int solve() {
21     multiset<int> st;
22     int res = 0;
23     for(int i = 0, j = 0; j < m; ++j) {
24         while(i < n && a[i].atk >= b[j].def)
25             st.insert(a[i++].def);
26         if(st.empty()) return -1;
27         auto it = st.upper_bound(b[j].atk);
28         if(it != st.end()) st.erase(it);
29         else st.erase(st.begin()), res++;
30     }
31     return n - res;
32 }
34 int main() {
35     scanf("%d", &T);
36     for(int t = 1; t <= T; ++t) {
37         scanf("%d%d", &n, &m);
38         for(int i = 0; i < n; ++i) a[i].read();
39         for(int i = 0; i < m; ++i) b[i].read();
40         sort(a, a + n, [](Node x, Node y) {
41             return x.atk > y.atk;
42         });
43         sort(b, b + m, [](Node x, Node y) {
44             return x.def > y.def;
45         });
46         printf("Case #%d: %d\n", t, solve());
47     }
48 }
View Code


posted @ 2015-05-13 20:09  Oyking  阅读(634)  评论(0编辑  收藏