UVALive 7146-贪心&数据结构stl-Defeat the Enemy
来源:互联网 发布:javascript写网页 编辑:程序博客网 时间:2024/05/22 03:46
https://vjudge.net/problem/158944/origin
比赛的时候想到了思路,但是没有想到多重集。。
给定m个自己的军队 和n个敌人,
军队有攻击力和 防御力, 敌人也是。
1 军队攻击力 大于等于敌人防御值,干死敌人
2 敌人攻击力大于等于自己防御力,干死自己
如果两种情况都存在,同归于尽。
问你如何安排对战,能够让自己战胜所有敌人并且保存下来的军队最多。
Limits:
1 ≤ T ≤ 100, 测试数据数目
1 ≤ n, m ≤ 105//n是自己军队,m是敌人
,
1 ≤ Attacki
, Defensei
, EAttacki
, EDefensei ≤ 1e9// 攻击力 和防御力 都是int类型的数
贪心,根据敌人 防御力从大到小。
敌人i 设定一个数组,里面都是 攻击力大于他的防御力的。(能杀死他的)
如果里面存在一个防御力也大于他攻击力的 ,那么用它。(要求此人防御力越小越好。)
如果不存在,则删除里面一个 防御力最小的。。
这就是比赛时候的思路,但是考虑到我军已经排好序,每次之后都要重新按照防御力在升序。tle了。(开始的考虑中我真的是用数组,所以删除操作也没想好qwq)
代码改良。查找用二分。
#include <bits/stdc++.h>using namespace std;vector<pair<int,int> >q;vector<pair<int,int> >v;bool cmp1(pair<int,int>a,pair<int,int> b){ return a.first>b.first;//俺们的军队按照攻击力排}bool cmp2(pair<int,int>a,pair<int,int>b){ return a.second>b.second;//敌人按照这样来}multiset<int>st;int main(){ int t; int m,n; int a,b; int tt=1; scanf("%d",&t); while(t--){ q.clear(); v.clear(); st.clear(); scanf("%d%d",&m,&n); for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); q.push_back(make_pair(a,b)); } for(int i=0;i<n;i++){ scanf("%d%d",&a,&b); v.push_back(make_pair(a,b)); } sort(q.begin(),q.end(),cmp1); sort(v.begin(),v.end(),cmp2); int j=0; int sum=0; bool flag=false; for(int i=0;i<n;i++){ while(j<m&&q[j].first>v[i].second){ st.insert(q[j++].second); } if(st.empty()){ flag=true;break; } multiset<int>::iterator s=st.upper_bound(v[i].first); if(s!=st.end()) st.erase(s); else {st.erase(st.begin()); sum++;} } if(flag) printf("Case #%d: -1\n",tt++); else printf("Case #%d: %d\n",tt++,m-sum); } return 0;}
阅读全文
0 0
- UVALive 7146-贪心&数据结构stl-Defeat the Enemy
- UVALive 7146 Defeat the Enemy (贪心+STL)
- UVALive 7146 Defeat the Enemy (贪心)
- UVALive 7146Defeat the Enemy贪心
- UVALive 7146 Defeat the Enemy (贪心)
- UVALive 7146 Defeat the Enemy
- UVALive-7146 Defeat the Enemy (map+贪心)
- UVALive 7146 Defeat the Enemy(模拟)
- UVA 7146 Defeat The Enemy (贪心)
- UVA Live 7146 Defeat the Enemy(贪心+set)
- UVA Live 7146 Defeat the Enemy——STL
- UVA LIVE 7146 Defeat the Enemy
- UVA 7146 Defeat The Enemy 2014 ACM-ICPC Asia shanghai Regional Contest
- 2014年上海赛区现场赛 I 题 Defeat the Enemy
- hdu4145 Cover The Enemy(贪心+枚举)
- UVALive - 3507 Keep the Customer Satisfied 贪心
- Sleeping with the Enemy
- Enemy at the Gateway
- 多台VPS进行Jmeter分布式压力测试
- SPFA以及其优化
- 利用Boost.Python将C++代码封装为Python模块
- 使用高德地图的api获取所在城市
- 浅谈引用<三> C++中指针和引用的区别
- UVALive 7146-贪心&数据结构stl-Defeat the Enemy
- 二叉树实现机制
- HDU6073-Matching In Multiplication
- leetcode[Construct the Rectangle]//待整理多种解法
- Ubuntu相关资料
- C/C++混合编程
- 2.A 张成空间与线性无关
- S3C2451嵌入式简介
- 万能 Makefile 模板