您的位置:首页 > 其它

PAT 1055. The World's Richest (25)

2015-03-16 08:37 363 查看
//1055. The World's Richest (25)
//这道题主要方法很简单,但是很容易超时,所以需要适当的剪枝。
//剪枝的方法是: 另外对age开辟一个数组,由于每次只需要输出给定范围内的最多100个结果,所以对相同age的100个以后的数据不做考虑,建立age在整个数组中的索引。
//acc
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

typedef struct
{
char name[9];
int age;
int worth;
}Student;

bool cmp( Student s1, Student s2)
{
if (s1.worth == s2.worth)
{
if (s1.age == s2.age)
{
return (strcmp(s1.name, s2.name)<0);
}
return (s1.age < s2.age);
}
return s1.worth > s2.worth;
}

Student stu[100001];

int main()
{
int N, K;
cin >> N >> K;

int i;
for (i = 0;i<N; i++)
{
scanf("%s %d %d", &stu[i].name, &stu[i].age, &stu[i].worth);
}

sort(stu, stu+N, cmp);

int num = 0;
int age[201] = {0};
int filter_num[100001];

//剪枝部分begin
for (i = 0;i<N;i++)
{
if (age[stu[i].age] < 100)
{
filter_num[num++] = i;
age[ stu[i].age ]++;
}
}
//剪枝部分end

int j;
int maxperson;
int minage, maxage;
int currperson = 0;
int index = 0;
for ( i = 1;i<=K;i++)
{
scanf("%d %d %d", &maxperson, &minage, &maxage);
printf("Case #%d:\n", i);

currperson = 0;
for (j = 0;j<num;j++)
{
index = filter_num[j];//剪枝尤为注意这个的赋值,相当于建立索引
if (currperson == maxperson)
{
break;
}
if (stu[index].age >= minage && stu[index].age <= maxage)
{
printf("%s %d %d\n", stu[index].name, stu[index].age, stu[index].worth);
currperson++;
}
}

if (currperson == 0 && maxperson!=0)
{
//cout << "None\n";
printf("None\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: