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) 收藏的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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) ...

  6. 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) ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 在Spring Data JPA 中使用Update Query更新实体类

    对于 Spring Data JPA 使用的时间不长,只有两年时间.但是踩过坑的却不少. 使用下列代码 @Modifying @Query("update User u set u.firs ...

  2. 强制关闭tomcat

    ps -ef |grep tomcat //找到tomcat的端口号 kill - tomcatpid

  3. commonJS — DOM操作(for DOM)

    for DOM github: https://github.com/laixiangran/commonJS/blob/master/src/forDOM.js 代码 /** * Created b ...

  4. Tiny PXE Server简介

    Tiny PXE Server简介Tiny PXE Server是一款小巧而功能强大的网启软件.支持DHCP TFTP HTTP BINL DNS等多个协议,支持grub4dos,pxelinux,i ...

  5. unity3d WorldComposer1 卫星地图生成地形

    http://blog.csdn.net/myarrow/article/details/42709113 1. 简介 1.1 TerrainComposer(TC) 一个Unity扩展工具,可用于创 ...

  6. MySQL OCP 考试,一个不错的网站

    http://www.aiotestking.com/oracle/category/exam-1z0-883-mysql-5-6-database-administrator/page/10/ 里面 ...

  7. 关于as中的事件与回调函数

    对于Observer模式, 在as中object(被观察者)既可以用事件(event),也可以用回调函数(caller)来通知观察者(observer).那在实际的开发中到底应该选择用event还是用 ...

  8. C/C++中的abort、atexit、exit和_Exit

    这几个函数都在头文件#include <stdlib.h>中声明.exit._Exit与abort函数使程序终止,控制并不返回到这些函数的调用者. exit()函数 void exit(i ...

  9. 3D正方体

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. [bootstrap] 栅格系统和布局

    1.简介 栅格系统(grid systems),也称为“网格系统”,运用固定的格子设计版面布局,风格工整简洁.是从平面栅格系统演变而来. Bootstrap建立在12列栅格系统.布局.组件之上.以规则 ...