题意: 让我们从一堆运动员中挑选四个运动员去参加接力跑。接力跑这项运动的第一棒对快速起跑的能力要求较高,因为后面几棒都是边跑边交换接力棒的。题目给了我们每个运动员跑第一棒的成绩和跑后几棒的成绩。让我们输出最优成绩和挑选的运动员。
思路: 一开始用的是dfs,超时。后来想了想直接按跑后几棒的成绩对运动员排序然后枚举一下就好了。枚举第一棒的可能运动员,然后剩下三棒的运动员按照排好序的顺序选就行。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
struct Person{
char name[25];
double t1, t2;
Person(){}
Person(char n[25], double a, double b){
strcpy(name, n);
t1 = a;
t2 = b;
}
bool operator < (const Person& p)const{
return t2 < p.t2;
}
};
int n;
Person p[510];
double ans;
int ansname[5];
int main()
{
//freopen("in.txt", "r", stdin);
while(scanf("%d", &n) == 1){
ans = 1000000;
for(int i=0; i<n; i++){
scanf("%s%lf%lf", p[i].name, &p[i].t1, &p[i].t2);
}
sort(p, p+n);
double tmpans;
int nm[5];
for(int i=0; i<n; i++){
nm[0] = i;
tmpans = p[i].t1;
for(int j=0,k=0; j<3; j++,k++){
if(k!=i){
nm[j+1] = k;
tmpans += p[k].t2;
}
else{
j--;
}
}
if(tmpans < ans){
ans = tmpans;
for(int j=0; j<4; j++){
ansname[j] = nm[j];
}
}
}
printf("%lf\n", ans);
for(int i=0; i<4; i++){
printf("%s\n", p[ansname[i]].name);
}
}
return 0;
}