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;}
- Sicily 2000. Toy Shopping
- 2000. Toy Shopping
- 2000. Toy Shopping
- 2000. Toy Shopping 继续是struct
- Sicily2000——Toy Shopping
- Sicily 1916. Toy
- shopping
- shopping
- Shopping
- Shopping
- shopping
- Shopping
- New Toy
- POJ2318 TOY
- os toy
- linux toy
- petsc toy
- Toy Army
- 数据库-管理-WINDOWS平台下搭建ASM
- zz: 深入浅出之正则表达式(一)
- python异常处理
- ORACLE禁用启用触发器
- hdu 1251 统计难题(tire 树 模版题)
- Sicily 2000. Toy Shopping
- 使用 HTML5 开发离线应用
- 二层交换原理
- Django 查询语法
- 二进制格式图片显示
- Sicily 2001. Scavenger Hunt
- 11gR2关于exp-00003问题
- 实习的第三天
- 利用Httponly提升web应用程序安全性