I - Defeat the Enemy(线段树好题)

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
[思路]：显而易见，这题肯定要贪心，如果存在攻击力防御力都大于的情况，那么肯定取最接近怪物的那个英雄

附上代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + 5;
struct NODE{
int flag, att, def;//flag - 0 or 1
//0 - my 1
friend bool operator<(const NODE &a, const NODE &b){
return a.att > b.att;
}
}arr[MAXN];
int n, m;
struct Segtree{
struct NODE{
int l, r, num;
}tree[MAXN << 2];
void build(int root, int l, int r){
tree[root].l = l, tree[root].r = r;
if(l == r) {
tree[root].num = 0;
return ;
}
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
tree[root].num = tree[root << 1].num + tree[root << 1 | 1].num;
}
void update(int root, int cnt, int val){
if(tree[root].l == tree[root].r){
tree[root].num += val;
return ;
}
int mid = (tree[root].l + tree[root].r) >> 1;
if(cnt <= mid){
update(root << 1, cnt, val);
}
else{
update(root << 1 | 1, cnt, val);
}
tree[root].num = tree[root << 1].num + tree[root << 1 | 1].num;
}
int l_query(int root, int l, int r){
int mid = (tree[root].l + tree[root].r) >> 1;
if(tree[root].l == tree[root].r && tree[root].num > 0){
return tree[root].l;
}
else if(tree[root].l == tree[root].r && tree[root].num == 0){
return -1;
}
if(l > mid){
if(tree[root << 1 | 1].num > 0){
return l_query(root << 1 | 1, l, r);
}
else{
return -1;
}
}
else if(r <= mid){
if(tree[root << 1].num > 0){
return l_query(root << 1, l, r);
}
else{
return -1;
}
}
else{
int lnum, rnum;
lnum = rnum = -1;
if(tree[root << 1].num > 0)
lnum = l_query(root << 1, l, mid);
if(tree[root << 1 | 1].num > 0)
rnum = l_query(root << 1 | 1, mid + 1, r);
if(lnum == -1 && rnum == -1){
return -1;
}
else if(lnum == -1){
return rnum;
}
else if(rnum == -1){
return lnum;
}
else if(lnum > 0 && rnum > 0){
return min(lnum, rnum);
}
}
}
}seg;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int T, cases = 0;
cin >> T;
while(T --){
vector<int>vec;
vec.clear();
cin >> n >> m;
for(int i = 0; i < n; i ++){
//scanf("%d%d", &arr[i].att, &arr[i].def);
cin >> arr[i].att >> arr[i].def;
arr[i].flag = 0;
vec.push_back(arr[i].def);
}
for(int i = n; i < n + m; i ++){
cin >> arr[i].att >> arr[i].def;
arr[i].flag = 1;
swap(arr[i].att, arr[i].def);
vec.push_back(arr[i].def);
}
sort(arr, arr + n + m);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
seg.build(1, 1, vec.size() + 5);
bool f = false;
int re = 0;
for(int i = 0; i < n + m; i ++){
if(arr[i].flag == 1){
int x = lower_bound(vec.begin(), vec.end(), arr[i].def) - vec.begin() + 1;
int a = seg.l_query(1, x + 1, vec.size() + 1);
int b = seg.l_query(1, 1, x);
if(a == -1 && b == -1){
cout << "Case #" << ++cases <<": " << -1 << endl;
f = true;
break;
}
else if(a != -1){
seg.update(1, a, -1);
}
else if(a == -1){
re ++;
seg.update(1, b, -1);
}
}
else{
int x = lower_bound(vec.begin(), vec.end(), arr[i].def) - vec.begin() + 1;
seg.update(1, x, 1);
}
}
if(!f){
cout << "Case #" << ++cases <<": " << n - re << endl;
}
}
return 0;
}


posted @ 2019-11-16 23:48  moxin0509  阅读(130)  评论(0编辑  收藏  举报