hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression)
2015-06-15 17:47
405 查看
partial sort.
first use std::nth_element to find pivot,
then use std::stable_partition with the pivot to partition the largest k, whose indices are in acsending order, print them in reverse order.
p.s. lambda expression is also used.
STL is powerful.
156 ms for double, vector
140 ms for float, vector
140 ms for double, array
124 ms for float, arry
time for array / time for vector = 90%
first use std::nth_element to find pivot,
then use std::stable_partition with the pivot to partition the largest k, whose indices are in acsending order, print them in reverse order.
p.s. lambda expression is also used.
STL is powerful.
[code]#include <cstdio> #include <vector> #include <algorithm> struct IRPair{ int ind; double rate; }; int main() { //freopen("input.txt","r",stdin); int num_people, num_candidate, num_final, i,j; double tmp; std::vector<IRPair> vec; std::vector<double> ratings; while(scanf("%d%d%d",&num_people,&num_candidate,&num_final)!=EOF) { vec.resize(num_candidate); ratings.clear(); ratings.resize(num_candidate); for(i=0;i<num_people;++i) for(j=0;j<num_candidate;++j) { scanf("%lf",&tmp); ratings[j]+=tmp; } for(i=0;i<num_candidate;++i) { vec[i].ind=i+1; vec[i].rate=ratings[i]; } std::nth_element(&ratings[0],&ratings[num_candidate-num_final],&ratings[num_candidate]); tmp=ratings[num_candidate-num_final]; std::stable_partition(&vec[0],&vec[num_candidate],[tmp](const IRPair &x) { return x.rate>=tmp; }); for(i=num_final-1;i>0;--i) { printf("%d ",vec[i].ind);} printf("%d\n",vec[0].ind); } return 0; }
156 ms for double, vector
140 ms for float, vector
140 ms for double, array
124 ms for float, arry
time for array / time for vector = 90%
相关文章推荐
- 关于U3D贴图格式压缩
- EF数据迁移(当模型改变时更新数据库)
- Freemarker 使用
- Linux网络编程——I/O复用之select详解
- 深入浅出UML类图
- MySQL 中的存储过程和游标
- cocoapod 设置
- C语言初学者编程规范十条
- SpringMVC返回json数据的三种方式
- 设置QTP脚本中每个步骤之间的延时时间
- 正则表达式
- Eclipse Android 项目 以全新AS结构导入Android Studio
- EF有外键的查询
- 随笔2
- 用BeanUtilsDate类型值为空报错的解决方法
- linux系统下nginx+php-fpm报错:502 Bad Gateway解决方法
- oracle系统包——dbms_alert用法
- git学习
- TotoiseSVN的基本使用方法
- 2015推荐的Android框架