1830: 【USACO】Race Results(比赛结果)
时间限制: 1.000 Sec 内存限制: 64 MB
提交: 324 解决: 204
[命题人:][下载数据: 70]
提交状态报告
题目描述
The herd has run its first marathon! The N (1 <= N <= 5,000) times have been posted in the form of Hours (0 <= Hours <= 99), Minutes (0 <= Minutes <= 59), and Seconds (0 <= Seconds <= 59). Bessie must sort them (by Hours, Minutes, and Seconds) into ascending order, smallest times first. Consider a simple example with times from a smaller herd of just 3 cows (note that cows do not run 26.2 miles so very quickly): 11:20:20 11:15:12 14:20:14 The proper sorting result is: 11:15:12 11:20:20 14:20:14
/*
奶牛们成功的举行了第一届马拉松大赛!!! 这里有N(1<=N<=5000)只奶牛的成绩,每一个成绩用三个整数表示,即小时数(0 <= 小时数 <= 99)分钟数(0 <= 分钟数 <= 59)和秒数(0 <= 秒数 <= 59).贝茜必须将它们排序(小时,分钟,秒数都要参考),最后的结果应该一个升序的,也就是说,用时最少的在最前面.
看这么一个例子,这里有3只奶牛的成绩(记录显示奶牛们在跑26.2英里时也不是很快^_^):
11:20:20
11:15:12
14:20:14
最后排序的结果应该是:
11:15:12
11:20:20
14:20:14
*/
输入
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains cow i's time as three space-separated integers: Hours, Minutes, Seconds
/*
第1行:一个整数N
第2..N+1行:每一行有三个整数,第i+1行描述的是奶牛i的成绩.
*/
输出
* Lines 1..N: Each line contains a cow's time as three space-separated integers
/*
有N行,每一行三个整数.这是排序后的结果.
*/
样例
输入 复制
3 11 20 20 11 15 12 14 20 14
输出 复制
11 15 12 11 20 20 14 20 14
来源/分类
AC
#include<iostream>
#include<algorithm>
using namespace std;
int h[5001],m[5001],s[5001],sum[5001];
void swap(int &x,int &y) {
int t=x;x=y;y=t;
}
void kp(int a,int b) {
int i=a,j=b,mid=sum[(a+b)/2];
while(i<=j) {
while(sum[i]<mid)i++;
while(sum[j]>mid)j--;
if(i<=j) {
swap(sum[i],sum[j]);
swap(h[i],h[j]);
swap(m[i],m[j]);
swap(s[i],s[j]);
i++;j--;
}
}
if(a<j)kp(a,j);
if(i<b)kp(i,b);
}
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++) {
cin>>h[i]>>m[i]>>s[i];
sum[i]=h[i]*3600+m[i]*60+s[i];
}
kp(1,n);
for(int i=1;i<=n;i++)
cout<<h[i]<<" "<<m[i]<<" "<<s[i]<<"\n";
}