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; }
相关文章推荐
- PAT (Advanced) 1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- PAT (Advanced Level) 1055. The World's Richest (25) 年龄区间内最有钱的人,结构体排序
- 【C++】PAT(advanced level)1055. The World's Richest (25)*
- pat 1055. The World's Richest (25)
- pat 1055. The World's Richest (25)
- PAT 1055. The World's Richest (25)
- ZJU-PAT 1055. The World's Richest (25) 浙大2013年上机复试第二题
- PAT: 1055. The World's Richest (25)
- 浙江大学PAT_甲级_1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- A1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- 1055. The World's Richest (25)
- PAT A 1055. The World's Richest (25)
- 1055. The World's Richest (25)