hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏
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.
#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%
版权声明:本文为博主原创文章,未经博主允许不得转载。// p.s. If in any way improment can be achieved, better performance or whatever, it will be well-appreciated to let me know, thanks in advance.
hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏的更多相关文章
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Subm ...
- The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏
The 3n + 1 problem Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53927 Accepted: 17 ...
- Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Hdu 1010 Tempter of the Bone 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Hdu 1009 FatMouse' Trade 分类: Translation Mode 2014-08-04 14:07 74人阅读 评论(0) 收藏
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1532 Drainage Ditches 分类: Brush Mode 2014-07-31 10:38 82人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Hdu 1507 Uncle Tom's Inherited Land* 分类: Brush Mode 2014-07-30 09:28 112人阅读 评论(0) 收藏
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏
C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏
three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...
随机推荐
- git的作用和原理(待续)
先选定一个目录作为Git仓库,假定是/srv/sample.git,在/srv目录下输入命令: $ sudo git init --bare sample.git Git就会创建一个裸仓库,裸仓库没有 ...
- double精度问题,数据范围
浮点数在计算机中存储方式 http://www.cnblogs.com/jillzhang/archive/2007/06/24/793901.html 1. double: 1bit(符号位) ...
- neon汇编,C嵌入汇编
1. Neon-intrics.pdf中的 void vst2q_u16(__transfersize(16) uint16_t * ptr, uint16x8x2_t val);// VST2.16 ...
- graph-tool 练习
如何使用graph-tool模块,如何导入?如何使用graph,使用其算法? 如何使用Boost Graph库,安装,测试? 1 创建和操纵图 如何创建空图? g = Graph() 如何精准的创建有 ...
- JavaScript window
window -- window对象是BOM中所有对象的核心 window,中文"窗口" window对象除了是BOM中所有对象的父对象外,还包含一些窗口控制函数 全局的windo ...
- 索引器(C# 编程指南)
原文地址:https://msdn.microsoft.com/zh-cn/library/6x16t2tx(VS.80).aspx 索引器允许类或结构的实例按照与数组相同的方式进行索引.索引器类似于 ...
- 配置electron
配置语句: git clone https://github.com/electron/electron-quick-start 文件夹名字 打开该文件(我用的webstorm)
- Django数据库设置
设置数据库,创建您的第一个模型,得到一个简单介绍 Django的自动生成管理网站. 数据库设置 现在,打开 mysite / settings.py . 这是一个普通的Python模块 模块级变量代表 ...
- Sublime Text 3编辑器安装
一.安装Sublime Text 3 到Sublime Text 3官网下载最新版本:http://www.sublimetext.com/3 一路Next下去即可完成安装.安装完Sublime Te ...
- css背景定位
日期:2015-12-05 背景定位算是才弄明白: background-position:50% 50%; 图片水平和垂直居中.与 background-position:center center ...