The War&&贪心算法
来源:互联网 发布:手游推荐知乎 编辑:程序博客网 时间:2024/04/30 03:50
Description
A war had broken out because a sheep from your kingdom ate some grasses which belong to your neighboring kingdom. The counselor of your kingdom had to get prepared for this war. There areN (1 <= N <= 2500) unarmed soldier in your kingdom and there areM (1 <= M <= 40000) weapons in your arsenal. Each weapon has a weightW (1 <= W <= 1000), and for soldier i, he can only arm the weapon whose weight is betweenminWi and maxWi ( 1 <= minWi <= maxWi <= 1000). More armed soldier means higher success rate of this war, so the counselor wants to know the maximal armed soldier he can get, can you help him to win this war?
Input
There multiple test cases. The first line of each case are two integers N, M. Then the following N lines, each line contain two integers minWi, maxWi for each soldier. Next M lines, each line contain one integer W represents the weight of each weapon.
Output
Sample Input
3 31 53 75 104892 25 1010 20421
Sample Output
20
思路:按每个人所要求的武器的最大重量从小到大排序,再按给定的武器的重量从小到大排序,然后枚举所有人尽可能多的找满足要求的武器,
AC代码:
#include<iostream>#include<cstdio>#include<algorithm>#include<string.h>#include<string>#include<set>using namespace std;typedef struct{int x;int y;}Node;Node s[2505];bool cmp(Node a,Node b){return a.y<b.y;}int main(){int n,m;while(~scanf("%d%d",&n,&m)){for(int i=0;i<n;++i)scanf("%d%d",&s[i].x,&s[i].y); sort(s,s+n,cmp); multiset<int>Q;for(int i=1;i<=m;++i) {int a;scanf("%d",&a);Q.insert(a);}int ans=0;multiset<int>::iterator it;for(int i=0;i!=n;++i){it=Q.lower_bound(s[i].x);if(it!=Q.end()&&*it<=s[i].y) ans++,Q.erase(it);}printf("%d\n",ans);}return 0;}
- The War&&贪心算法
- zoj3508 The War 贪心
- ZOJ 3508 The War 贪心
- ZOJ 3508 The War 贪心
- ZOJ 3508 The War(贪心)
- zoj 3508 The War 贪心
- ZOJ 3508 The War(贪心)
- ZOJ 3058 The War(简单贪心)
- ZOJ 3508 ——The War 贪心
- ZOJ-#3508 The War(贪心)
- ZOJ3508 The War 贪心,最大流
- uva 11729 Commando War(贪心算法)
- Command War(Uva 11729)贪心算法应用
- Commando War(UVa 11729)贪心算法
- The War
- Commando War-uva 贪心
- 【贪心】【Uva11729】 Commando War
- Commando War uva11729(贪心)
- 关于hudson上的job迁移-(job migration of hudson)
- 【flex4.6将移动开发进行到底】第一章第一节建立手机项目
- Android 类似于windows打开文件对话框的实现
- GridView支持复制与粘贴
- spring3.x中jar包的说明
- The War&&贪心算法
- 程序员必知的八大排序
- SQLHelper助手
- effective java(枚举和注解)
- memcache说明
- SSH框架的dtd文件
- Lucene:基于Java的全文检索引擎简介
- OpenGL ES着色器语言之预处理(preprocessing)
- WIFI热点(将笔记本设置为WIFI热点的方法