algorithm——sort(eg.The Dragon of Loowater)
来源:互联网 发布:js显示隐藏div切换 编辑:程序博客网 时间:2024/05/17 06:43
algorithm 中 sort函数:
sort函数:sort(first,end,method)//其中第三个参数为可选参数。
参数含义:
first:需要排序数组的起始位置
end:终止位置
method:是一个方法,用来决定sort函数的排序方式
1.当只有两个参数时,则默认排序为从小到大
(应用见例题)
2.当有3个参数时,则要实现method方法。
eg.# include <iostream># include <algorithm>using namespace std;*bool method(int a , int b){ //实现从大到小排序 return a>b;}*int main(){ int A[6] = {3, 5, 9 , 1 ,-3 ,18}; sort(A,A+6,method); for(int i = 0 ; i < 6 ; i++) cout<<A[i]<<" "; return 0;}
例题:有一条n个头的恶龙以及m个骑士,一个能力值为x的骑士可以被雇佣来砍掉恶龙一个直径不超过x的头,且需要支付x个金币。如何雇佣骑士才能砍掉恶龙所有的头,且需要支付的金币最少?注意,一个骑士只能砍掉一个头,且不能被重复雇佣。
[输入格式]
输入包含多组数据。第一行为正整数n和m(1<=n,m<=20000);一下n行每行为一个整数,即恶龙头的直径;以下m行每行一个正整数,即每个骑士的能力值。输入结束的标志为n=m=0。
[输出格式]
对于每组数据,输出最少的花费。如果无解,输出”Loowater is doomed!”。
[样例输入]
2 3
5
4
7
8
4
2 1
5
5
10
0 0
[样例输出]
11
Loowater is doomed!
C++代码:
# include <iostream># include <algorithm>using namespace std;int main(){ int n,m,*dragon,*loowater; while(cin>>n>>m){ dragon = new int[n]; loowater = new int[m]; for(int i = 0 ; i < n ; i ++) cin>>dragon[i]; for(int i = 0 ; i < m ; i ++) cin>>loowater[i]; sort(dragon,dragon+n); sort(loowater,loowater+m); int cur = 0; int cost = 0; for(int i = 0 ; i < m ; i ++){ if(loowater[i] >= dragon[cur]){ cost = cost + loowater[i]; if(++cur == n) break; } } if(cur < n) cout<<"Loowater is doomed!"<<endl; else cout<<cost<<endl; }}
阅读全文
0 0
- algorithm——sort(eg.The Dragon of Loowater)
- The Dragon of Loowater
- The Dragon of Loowater
- The Dragon of Loowater
- The Dragon of Loowater
- The Dragon of Loowater
- The Dragon of Loowater
- ACM——The Dragon of Loowater(UVa 11292)
- uva 11292 ——The Dragon of Loowater
- UVa11292 The Dragon of Loowater(sort排序直接比较)
- UVA11292 The Dragon of Loowater
- UVA11292-The Dragon of Loowater
- HDU The Dragon of Loowater
- poj3646 The Dragon of Loowater
- The Dragon of Loowater(poj3646)
- ZOJ3646 The Dragon of Loowater
- Uva11292 The Dragon of Loowater
- uva11292 The Dragon of Loowater
- 分组排序之后,查询每组排序第一的记录
- CodeForces
- 思维水题:UVa512-Spreadsheet Tracking
- JMeter---ForEach循环控制器实例
- 数据库学习之初见oracle
- algorithm——sort(eg.The Dragon of Loowater)
- luigi 初探
- animeditor-Creating a New Animation Clip
- Android之Socket的基于UDP传输
- CTF/CTF练习平台-welcome to bugkuctf【php://filter及php://input】
- javascript对象创建
- mysql中select查询语句添加自增id
- Android平台对H264视频硬解码
- centos7使用yum安装mariaDB(开源MySQL)无法启动的解决办法