Sicily 2000. Toy Shopping

来源:互联网 发布:在线查看html源码 编辑:程序博客网 时间:2024/05/12 13:11

简单题,主要用来试下各种算法,尽量减少程序运行的时间。由于固定地要最大的三个数,可以调用STL的sort函数排序后取前三者,代码编写简单,但是做了很多无用功(前三者后面的也排序了),耗时0.03sec。后来写了个选择排序,只排出前三个,耗时0.02sec,暂时没想到更优的方法。
Run Time: 0.02sec
Run Memory: 476KB
Code length: 849Bytes
Submit Time: 2011-12-10 14:59:36

// Problem#: 2000// Submission#: 1059559// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <cstdio>using namespace std;struct Toy{    int index;    int joy, price;};bool cmp( const Toy& t1, const Toy& t2 ) { return (double)t1.joy / t1.price > (double)t2.joy / t2.price; }int main(){    int N;    int i, j, max;    Toy toy[ 25000 ], temp;    scanf( "%d", &N );    for ( i = 0; i < N; i++ ) {        toy[ i ].index = i + 1;        scanf( "%d%d", &toy[ i ].joy, &toy[ i ].price );    }        for ( i = 0; i < 3; i++ ) {        max = i;        for ( j = i + 1; j < N; j++ ) {            if ( cmp( toy[ j ], toy[ max ] ) )                max = j;        }        temp = toy[ i ];        toy[ i ] = toy[ max ];        toy[ max ] = temp;    }    printf( "%d\n", toy[ 0 ].price + toy[ 1 ].price + toy[ 2 ].price );    for ( i = 0; i < 3; i++ )        printf( "%d\n", toy[ i ].index );    return 0;}                                 


 

原创粉丝点击