思路:按攻击方的攻击力排序,防御方的防御力排序,然后对于每一个防御方肯定是在攻击方找一个攻击力比它防御力高并且防御力比防御方的攻击力稍微大一点点的最优,所以用个multiset搞一下就OK了
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; const int maxn = 1e5+6; struct en { int gongji; int fangyu; }a[maxn],b[maxn]; bool cmp1(en a,en b) { return a.gongji>b.gongji; } bool cmp2(en a,en b) { return a.fangyu > b.fangyu; } int main() { int T,cas=1; scanf("%d",&T); while (T--) { int n,m; scanf("%d%d",&n,&m); for (int i = 0;i<n;i++) scanf("%d%d",&a[i].gongji,&a[i].fangyu); for (int i = 0;i<m;i++) scanf("%d%d",&b[i].gongji,&b[i].fangyu); if (n<m) { printf("Case #%d: -1\n",cas++); continue; } sort(a,a+n,cmp1); sort(b,b+m,cmp2); multiset<int>s; int cnt,pos,flag=1; pos=0; cnt=0; for (int i = 0;i<m;i++) { for (int j = pos;j<n;j++) { if (a[j].gongji>=b[i].fangyu) { s.insert(a[j].fangyu); pos++; } else break; } if (s.empty()) { flag=0; break; } else { multiset<int>::iterator it; it = s.upper_bound(b[i].gongji); if (it != s.end()) { s.erase(it); } else { s.erase(s.begin()); cnt++; } } } if (flag) printf("Case #%d: %d\n",cas++,n-cnt); else printf("Case #%d: -1\n",cas++); } }
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 Input23 25 77 31 24 42 22 13 41 105 6Sample OutputCase #1: 3Case #2: -1