上机笔记4.1

*4.1 PAT B1015/A1062

宋代史学家司马光在《资治通鉴》中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人。凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人。”

现给出一批考生的德才分数,请根据司马光的理论给出录取排名。

输入格式:

输入第一行给出 3 个正整数,分别为:N(≤105),即考生总数;L(≥60),为录取最低分数线,即德分和才分均不低于 L 的考生才有资格被考虑录取;H(<100),为优先录取线——德分和才分均不低于此线的被定义为“才德全尽”,此类考生按德才总分从高到低排序;才分不到但德分到线的一类考生属于“德胜才”,也按总分排序,但排在第一类考生之后;德才分均低于 H,但是德分不低于才分的考生属于“才德兼亡”但尚有“德胜才”者,按总分排序,但排在第二类考生之后;其他达到最低线 L 的考生也按总分排序,但排在第三类考生之后。

随后 N 行,每行给出一位考生的信息,包括:准考证号 德分 才分,其中准考证号为 8 位整数,德才分为区间 [0, 100] 内的整数。数字间以空格分隔。

输出格式:

输出第一行首先给出达到最低分数线的考生人数 M,随后 M 行,每行按照输入格式输出一位考生的信息,考生按输入中说明的规则从高到低排序。当某类考生中有多人总分相同时,按其德分降序排列;若德分也并列,则按准考证号的升序输出。

输入样例:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

输出样例:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90

About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people’s talent and virtue. According to his theory, a man being outstanding in both talent and virtue must be a “sage(圣人)”; being less excellent but with one’s virtue outweighs talent can be called a “nobleman(君子)”; being good in neither is a “fool man(愚人)”; yet a fool man is better than a “small man(小人)” who prefers talent than virtue.

Now given the grades of talent and virtue of a group of people, you are supposed to rank them according to Sima Guang’s theory.

Input Specification:

Each input file contains one test case. Each case first gives 3 positive integers in a line: N (≤105), the total number of people to be ranked; L (≥60), the lower bound of the qualified grades – that is, only the ones whose grades of talent and virtue are both not below this line will be ranked; and H (<100), the higher line of qualification – that is, those with both grades not below this line are considered as the “sages”, and will be ranked in non-increasing order according to their total grades. Those with talent grades below H but virtue grades not are considered as the “noblemen”, and are also ranked in non-increasing order according to their total grades, but they are listed after the “sages”. Those with both grades below H, but with virtue not lower than talent are considered as the “fool men”. They are ranked in the same way but after the “noblemen”. The rest of people whose grades both pass the L line are ranked after the “fool men”.

Then N lines follow, each gives the information of a person in the format:

ID_Number Virtue_Grade Talent_Grade

where ID_Number is an 8-digit number, and both grades are integers in [0, 100]. All the numbers are separated by a space.

Output Specification:

The first line of output must give M (≤N), the total number of people that are actually ranked. Then M lines follow, each gives the information of a person in the same format as the input, according to the ranking rules. If there is a tie of the total grade, they must be ranked with respect to their virtue grades in non-increasing order. If there is still a tie, then output in increasing order of their ID’s.

Sample Input:

14 60 80
10000001 64 90
10000002 90 60
10000011 85 80
10000003 85 80
10000004 80 85
10000005 82 77
10000006 83 76
10000007 90 78
10000008 75 79
10000009 59 90
10000010 88 45
10000012 80 100
10000013 90 99
10000014 66 60

Sample Output:

12
10000013 90 99
10000012 80 100
10000003 85 80
10000011 85 80
10000004 80 85
10000007 90 78
10000006 83 76
10000005 82 77
10000002 90 60
10000014 66 60
10000008 75 79
10000001 64 90
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct student {
	char id[10];
	int de_score;
	int cai_score;
	int sum;
	int level;
}stu[100010];

bool cmp(student t1, student t2) {
	if (t1.level != t2.level)
		return t1.level < t2.level;
	else if (t1.sum != t2.sum)
		return t1.sum > t2.sum;
	else if (t1.de_score != t2.de_score)
		return t1.de_score > t2.de_score;
	else if (t1.id != t2.id)
		return strcmp(t1.id, t2.id) < 0;
}


int main() {
	int n, l, h;
	scanf("%d %d %d", &n,&l,&h);
	int count = 0;
	for (int i = 0; i < n; i++) {
		scanf("%s %d %d", stu[i].id, &stu[i].de_score, &stu[i].cai_score);
		stu[i].sum = stu[i].de_score + stu[i].cai_score;
		if (stu[i].de_score < l || stu[i].cai_score < l) {
			stu[i].level = 5;
		}
		//德才>h
		else if (stu[i].de_score >= h && stu[i].cai_score >= h) {
			stu[i].level = 1;
			count++;
		}
		//德>h,才<h
		else if (stu[i].de_score >= h && stu[i].cai_score < h) {
			stu[i].level = 2;
			count++;
		}
		//德才<h,德>才
		else if (stu[i].de_score < h && stu[i].cai_score < h && stu[i].de_score >= stu[i].cai_score) {
			stu[i].level = 3;
			count++;
		}
		//德才<h,德才>l
		else if (stu[i].de_score >= l && stu[i].cai_score >= l) {
			stu[i].level = 4;
			count++;
		}
	}
	sort(stu, stu + n, cmp);
	printf("%d\n", count);
	for (int i = 0; i < count; i++) {
		printf("%s %d %d\n", stu[i].id, stu[i].de_score, stu[i].cai_score);
	}
	return 0;
}

? *4.1 PAT A1012

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

struct student {
	int id;
	int grade[4];
}stu[2010];

char course[4] = { 'A','C','M','E' };
int Rank[10000000][4] = { 0 };
int now;

bool cmp(student a, student b) {
	return a.grade[now] > b.grade[now];
}

int main() {
	int n, m;
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d%d%d%d", &stu[i].id, &stu[i].grade[1], &stu[i].grade[2], &stu[i].grade[3]);
		stu[i].grade[0] = stu[i].grade[1] + stu[i].grade[2] + stu[i].grade[3];
	}

	for (now = 0; now < 4; now++) {
		sort(stu, stu + n, cmp);
		Rank[stu[now].id][now] = 1;
		for (int i = 0; i < n; i++) {
			if (stu[i].grade[now] == stu[i - 1].grade[now]) {
				Rank[stu[i].id][now] = Rank[stu[i - 1].id][now];
			}
			else {
				Rank[stu[i].id][now] = i + 1;
			}
		}

	}
	int query;
	for (int i = 0; i < m; i++) {
		scanf("%d", &query);
		if (Rank[query][0] == 0) {
			printf("N/A\n");
		}
		else {
			int k = 0;
			for (int j = 0; j < 4; j++) {
				if (Rank[query][j] < Rank[query][k]) {
					k = j;
				}
			}
			printf("%d %c\n", Rank[query][k], course[k]);
		}
	}
	return 0;
}

? *4.1 PAT A1016

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (MM:dd:HH:mm), and the word on-line or off-line.

For each test case, all dates will be within a single month. Each on-line record is paired with the chronologically next record for the same customer provided it is an off-line record. Any on-line records that are not paired with an off-line record are ignored, as are off-line records not paired with an on-line record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers’ names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:HH:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

Sample Input:

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

Sample Output:

CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int toll[25];

struct Record {
	char name[25];
	int month, dd, hh, mm;
	bool status;//status==true 表示on-line,否则表示off-line
}rec[maxn],temp;

bool cmp(Record a, Record b) {
	int s = strcmp(a.name, b.name);
	if (s != 0)
		return s < 0;
	else if (a.month != b.month)
		return a.month < b.month;
	else if (a.dd != b.dd)
		return a.dd < b.dd;
	else if (a.hh != b.hh)
		return a.hh < b.hh;
	else
		return a.mm < b.mm;
}

void get_ans(int on, int off, int& time, int& money) {
	temp = rec[on];
	while (temp.dd < rec[off].dd || temp.hh < rec[off].hh || temp.mm < rec[off].mm) {
		time++;
		money += toll[temp.hh];
		temp.mm++;
		if (temp.mm >= 60) {
			temp.mm = 0;
			temp.hh++;
		}
		if (temp.hh >= 24) {
			temp.hh = 0;
			temp.dd++;
		}
	}
}

int main() {
	for (int i = 0; i < 24; i++) {
		scanf("%d", &toll[i]);
	}
	int n;
	scanf("%d", &n);
	char line[10];//暂存on-line和off-line
	for (int i = 0; i < n; i++) {
		scanf("%s", rec[i].name);
		scanf("%d:%d:%d:%d", &rec[i].month, &rec[i].dd, &rec[i].hh, &rec[i].mm);
		scanf("%s", line);
		if (strcmp(line, "on-line") == 0) {
			rec[i].status = true;
		}
		else {
			rec[i].status = false;
		}
	}
	sort(rec, rec + n, cmp);
	int on = 0, off, next;
	next = on;
	while (on < n) {
		int needPrint = 0;
		next = on;
		while (next < n && strcmp(rec[next].name, rec[on].name) == 0) {
			if (needPrint == 0 && rec[next].status == true) {
				needPrint = 1;
			}
			else if (needPrint == 1 && rec[next].status == false) {
				needPrint = 2;
			}
			next++;
		}
		if (needPrint < 2) {
			on = next;
			continue;
		}
		int AllMoney = 0;
		printf("%s %02d\n", rec[on].name, rec[on].month);
		while (on < next) {
			while (on < next - 1 && !(rec[on].status == true && rec[on + 1].status == false)) {
				on++;
			}
			off = on + 1;
			if (off == next) {
				on = next;
				break;
			}
			printf("%02d:%02d:%02d ", rec[on].dd, rec[on].hh, rec[on].mm);
			printf("%02d:%02d:%02d ", rec[off].dd, rec[off].hh, rec[off].mm);
			int time = 0, money = 0;
			get_ans(on, off, time, money);
			AllMoney += money;
			printf("%d $%.2f\n", time, money / 100.0);
			on = off + 1;
		}
		printf("Total amount: $%.2f\n", AllMoney / 100.0);
	}
	return 0;
}

*4.1 PAT A1025

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct student {
	char id[15];
	int score;
	int location_number;
	int local_rank;
	int rank;
}stu[30010];

bool cmp(student s1, student s2) {
	if (s1.score != s2.score)
		return s1.score > s2.score;
	else 
		return strcmp(s1.id, s2.id) < 0;
}

int main() {
	int n, k, count = 0;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &k);
		for (int j = 0; j < k; j++) {
			scanf("%s %d", stu[count].id, &stu[count].score);
			stu[count].location_number = i;
			count++;
		}
		sort(stu + count - k, stu + count, cmp);
		stu[count - k].local_rank = 1;
		for (int j = count - k + 1; j < count; j++) {
			if (stu[j].score == stu[j - 1].score) {
				stu[j].local_rank = stu[j - 1].local_rank;
			}
			else {
				stu[j].local_rank = j + 1 - (count - k);
			}
		}
	}
	printf("%d\n", count);
	sort(stu, stu + count, cmp);
	stu[0].rank = 1;
	for (int i = 1; i <= count; i++) {
		if (stu[i].score == stu[i-1].score) {
			stu[i].rank = stu[i-1].rank;
		}
		else {
			stu[i].rank = i + 1;
		}
	}

	for (int i = 0; i < count; i++) {
		printf("%s %d %d %d\n", stu[i].id, stu[i].rank, stu[i].location_number, stu[i].local_rank);
	}

}

*4.1 PAT A1028

Excel can sort records according to any column. Now you are supposed to imitate this function.

Input Specification:

Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).

Output Specification:

For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.

Sample Input 1:

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1:

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2:

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2:

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3:

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 9

Sample Output 3:

000002 James 9
000001 Zoe 60
000007 James 85
000010 Amy 90

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;

struct student {
	char id[10];
	char name[10];
	int grade;
}stu[100010];

bool cmp1(student s1, student s2) {
	return strcmp(s1.id, s2.id) < 0;
}

bool cmp2(student s1, student s2) {
	if(strcmp(s1.name,s2.name)!=0)
		return strcmp(s1.name, s2.name) < 0;
	else
		return strcmp(s1.id, s2.id) < 0;
}

bool cmp3(student s1, student s2) {
	if(s1.grade!=s2.grade)
		return s1.grade < s2.grade;
	else
		return strcmp(s1.id, s2.id) < 0;
}

int main() {
	int n, c;
	scanf("%d %d", &n, &c);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", stu[i].id, stu[i].name, &stu[i].grade);
	}
	自己写的版本
	//if (c == 1) {
	//	sort(stu, stu + n, cmp1);
	//	for (int i = 0; i < n; i++) {
	//		printf("%s %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
	//	}
	//}

	//if (c == 2) {
	//	sort(stu, stu + n, cmp2);
	//	for (int i = 0; i < n; i++) {
	//		printf("%s %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
	//	}
	//}

	//if (c == 3) {
	//	sort(stu, stu + n, cmp3);
	//	for (int i = 0; i < n; i++) {
	//		printf("%s %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
	//	}
	//}
	算法笔记版本,更简洁
	if (c == 1) sort(stu, stu + n, cmp1);
	else if (c == 2) sort(stu, stu + n, cmp2);
	else sort(stu, stu + n, cmp3);
	for (int i = 0; i < n; i++) {
		printf("%s %s %d\n", stu[i].id, stu[i].name, stu[i].grade);
	}
	return 0;
}

*4.1 PAT A1055

Forbes magazine publishes every year its list of billionaires based on the annual ranking of the world’s wealthiest people. Now you are supposed to simulate this job, but concentrate only on the people in a certain range of ages. That is, given the net worths of N people, you must find the M richest people in a given range of their ages.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤105) - the total number of people, and K (≤103) - the number of queries. Then N lines follow, each contains the name (string of no more than 8 characters without space), age (integer in (0, 200]), and the net worth (integer in [−106,106]) of a person. Finally there are K lines of queries, each contains three positive integers: M (≤100) - the maximum number of outputs, and [Amin, Amax] which are the range of ages. All the numbers in a line are separated by a space.

Output Specification:

For each query, first print in a line Case #X: where X is the query number starting from 1. Then output the M richest people with their ages in the range [Amin, Amax]. Each person’s information occupies a line, in the format

Name Age Net_Worth

The outputs must be in non-increasing order of the net worths. In case there are equal worths, it must be in non-decreasing order of the ages. If both worths and ages are the same, then the output must be in non-decreasing alphabetical order of the names. It is guaranteed that there is no two persons share all the same of the three pieces of information. In case no one is found, output None.

Sample Input:

12 4
Zoe_Bill 35 2333
Bob_Volk 24 5888
Anny_Cin 95 999999
Williams 30 -22
Cindy 76 76000
Alice 18 88888
Joe_Mike 32 3222
Michael 5 300000
Rosemary 40 5888
Dobby 24 5888
Billy 24 5888
Nobody 5 0
4 15 45
4 30 35
4 5 95
1 45 50

Sample Output:

Case #1:
Alice 18 88888
Billy 24 5888
Bob_Volk 24 5888
Dobby 24 5888
Case #2:
Joe_Mike 32 3222
Zoe_Bill 35 2333
Williams 30 -22
Case #3:
Anny_Cin 95 999999
Michael 5 300000
Alice 18 88888
Cindy 76 76000
Case #4:
None
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

struct Person {
	char name[10];
	int age;
	int worth;
}per[100010],valid[100010];

bool cmp(Person a,Person b) {
	if (a.worth != b.worth) {
		return a.worth > b.worth;
	}
	else if (a.age != b.age) {
		return a.age < b.age;
	}
	else
	{
		return strcmp(a.name, b.name) < 0;
	}
}
//自己版本
int main() {

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

	sort(per, per + n, cmp);

	int m, low, high;
	int m_real = 100;


	for (int i = 1; i <= k; i++) {
		
		scanf("%d %d %d", &m, &low, &high);
		printf("Case #%d:\n", i);
		//只要前一百个人或者m个人的排名
		if (m < m_real)
			m_real = m;
		int printNum = 0;

		for (int j = 0; j < n && printNum < m_real; j++) {
			if (per[j].age >= low && per[j].age <= high) {
				printf("%s %d %d\n", per[j].name,per[j].age,per[j].worth);
				printNum++;
			}
		}

		if (printNum == 0) {
			printf("None\n");
		}
	}
	return 0;
}
算法笔记版
//int main() {
//
//	int n, k;
//	int Age[100010] = { 0 };
//	scanf("%d %d", &n, &k);
//	for (int i = 0; i < n; i++) {
//		scanf("%s %d %d", per[i].name, &per[i].age, &per[i].worth);
//	}
//
//	sort(per, per + n, cmp);
//
//	int validNum = 0;
//	for (int i = 0; i < n; i++) {
//		if (Age[per[i].age] < 100) {
//			Age[per[i].age]++;
//			valid[validNum++] = per[i];
//		}
//	}
//	int m, low, high;
//	for (int i = 1; i <= k; i++) {
//		scanf("%d %d %d", &m, &low, &high);
//		printf("Case #%d:\n", i);
//		int printNum = 0;
//		for (int j = 0; j < validNum && printNum < m; j++) {
//			if (valid[j].age >= low && valid[j].age <= high) {
//				printf("%s %d %d\n", per[j].name, &per[j].age, &per[j].worth);
//				printNum++;
//			}
//		}
//		if (printNum == 0) {
//			printf("None\n");
//		}
//	}
//	return 0;
//}

? *4.1 PAT A1075

The ranklist of PAT is generated from the status list, which shows the scores of the submissions. This time you are supposed to generate the ranklist for PAT.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive integers, N (≤104), the total number of users, K (≤5), the total number of problems, and M (≤105), the total number of submissions. It is then assumed that the user id’s are 5-digit numbers from 00001 to N, and the problem id’s are from 1 to K. The next line contains K positive integers p[i] (i=1, …, K), where p[i] corresponds to the full mark of the i-th problem. Then M lines follow, each gives the information of a submission in the following format:

user_id problem_id partial_score_obtained

where partial_score_obtained is either −1 if the submission cannot even pass the compiler, or is an integer in the range [0, p[problem_id]]. All the numbers in a line are separated by a space.

Output Specification:

For each test case, you are supposed to output the ranklist in the following format:

rank user_id total_score s[1] ... s[K]

where rank is calculated according to the total_score, and all the users with the same total_score obtain the same rank; and s[i] is the partial score obtained for the i-th problem. If a user has never submitted a solution for a problem, then “-” must be printed at the corresponding position. If a user has submitted several solutions to solve one problem, then the highest score will be counted.

The ranklist must be printed in non-decreasing order of the ranks. For those who have the same rank, users must be sorted in nonincreasing order according to the number of perfectly solved problems. And if there is still a tie, then they must be printed in increasing order of their id’s. For those who has never submitted any solution that can pass the compiler, or has never submitted any solution, they must NOT be shown on the ranklist. It is guaranteed that at least one user can be shown on the ranklist.

Sample Input:

7 4 20
20 25 25 30
00002 2 12
00007 4 17
00005 1 19
00007 2 25
00005 1 20
00002 2 2
00005 1 15
00001 1 18
00004 3 25
00002 2 25
00005 3 22
00006 4 -1
00001 2 18
00002 1 20
00004 1 15
00002 4 18
00001 3 4
00001 4 2
00005 2 -1
00004 2 0

Sample Output:

1 00002 63 20 25 - 18
2 00005 42 20 0 22 -
2 00007 42 - 25 - 17
2 00001 42 18 18 4 2
5 00004 40 15 0 25 -
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 10010;

struct student {
	int id;
	int score[6];
	bool flag;
	int score_all;
	int solve;
}stu[maxn];

int n, k, m;
int full[6];

bool cmp(student s1, student s2) {
	if (s1.score_all != s2.score_all) {
		return s1.score_all > s2.score_all;
	}
	else if (s1.solve != s2.solve) {
		return s1.solve > s2.solve;
	}
	else
		return s1.id < s2.id;
}

void init() {
	for (int i = 1; i <= n; i++) {
		stu[i].id = i;
		stu[i].score_all = 0;
		stu[i].solve = 0;
		stu[i].flag = false;
		memset(stu[i].score, -1, sizeof(stu[i].score));
	}
}

int main() {
	scanf("%d %d %d", &n, &k, &m);
	init();
	for (int i = 1; i <= k; i++) {
		scanf("%d", &full[i]);
	}
	int u_id, p_id, score_obtain;
	for (int i = 0; i < m; i++) {
		scanf("%d%d%d", &u_id, &p_id, &score_obtain);
		if (score_obtain != -1) {
			stu[u_id].flag = true;
		}
		if (score_obtain == -1 && stu[u_id].score[p_id] == -1) {
			stu[u_id].score[p_id] = 0;
		}
		if (score_obtain == full[p_id] && stu[u_id].score[p_id] < full[p_id]) {
			stu[u_id].solve++;
		}
		if (score_obtain > stu[u_id].score[p_id]) {
			stu[u_id].score[p_id] = score_obtain;
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= k; j++) {
			if (stu[i].score[j] != -1) {
				stu[i].score_all += stu[i].score[j];
			}
		}
	}

	sort(stu + 1, stu + n + 1, cmp);
	int r = 1;

	for (int i = 1; i <= n && stu[i].flag == true; i++) {
		if (i > 1 && stu[i].score_all != stu[i - 1].score_all) {
			r = i;
		}
		printf("%d %05d %d", r, stu[i].id, stu[i].score_all);
		for (int j = 1; j <= k; j++) {
			if (stu[i].score[j] == -1) {
				printf(" -");
			}
			else {
				printf(" %d", stu[i].score[j]);
			}
		}
		printf("\n");
	}
	return 0;
}

*4.1 PAT A1083

Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.

Input Specification:

Each input file contains one test case. Each case is given in the following format:

N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2

where name[i] and ID[i] are strings of no more than 10 characters with no space, grade[i] is an integer in [0, 100], grade1 and grade2 are the boundaries of the grade’s interval. It is guaranteed that all the grades are distinct.

Output Specification:

For each test case you should output the student records of which the grades are in the given interval [grade1, grade2] and are in non-increasing order. Each student record occupies a line with the student’s name and ID, separated by one space. If there is no student’s grade in that interval, output NONE instead.

Sample Input 1:

4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100

Sample Output 1:

Mike CS991301
Mary EE990830
Joe Math990112

Sample Input 2:

2
Jean AA980920 60
Ann CS01 80
90 95

Sample Output 2:

NONE
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

struct student {
	char name[15];
	char id[15];
	int grade;
}stu[10010];

bool cmp(student s1, student s2) {
	return s1.grade > s2.grade;
}


int main() {
	int n, low, high;
	scanf("%d%d%d", &n, &low, &high);
	for (int i = 0; i < n; i++) {
		scanf("%s %s %d", stu[i].name, stu[i].id, &stu[i].grade);
	}
	scanf("%d %d", &low, &high);
	int count = 0;
	sort(stu, stu + n, cmp);
	for (int i = 0; i < n; i++) {
		if (stu[i].grade >= low && stu[i].grade <= high) {
			printf("%s %s\n", stu[i].name, stu[i].id);
			count++;
		}
	}
	if (count == 0)
		printf("NONE");
	return 0;
}

? *4.1 PAT A1080

It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.

Each applicant will have to provide two grades: the national entrance exam grade G**E, and the interview grade G**I. The final grade of an applicant is (G**E+G**I)/2. The admission rules are:

  • The applicants are ranked according to their final grades, and will be admitted one by one from the top of the rank list.
  • If there is a tied final grade, the applicants will be ranked according to their national entrance exam grade G**E. If still tied, their ranks must be the same.
  • Each applicant may have K choices and the admission will be done according to his/her choices: if according to the rank list, it is one’s turn to be admitted; and if the quota of one’s most preferred shcool is not exceeded, then one will be admitted to this school, or one’s other choices will be considered one by one in order. If one gets rejected by all of preferred schools, then this unfortunate applicant will be rejected.
  • If there is a tied rank, and if the corresponding applicants are applying to the same school, then that school must admit all the applicants with the same rank, even if its quota will be exceeded.

Input Specification:

Each input file contains one test case.

Each case starts with a line containing three positive integers: N (≤40,000), the total number of applicants; M (≤100), the total number of graduate schools; and K (≤5), the number of choices an applicant may have.

In the next line, separated by a space, there are M positive integers. The i-th integer is the quota of the i-th graduate school respectively.

Then N lines follow, each contains 2+K integers separated by a space. The first 2 integers are the applicant’s G**E and G**I, respectively. The next K integers represent the preferred schools. For the sake of simplicity, we assume that the schools are numbered from 0 to M−1, and the applicants are numbered from 0 to N−1.

Output Specification:

For each test case you should output the admission results for all the graduate schools. The results of each school must occupy a line, which contains the applicants’ numbers that school admits. The numbers must be in increasing order and be separated by a space. There must be no extra space at the end of each line. If no applicant is admitted by a school, you must output an empty line correspondingly.

Sample Input:

11 6 3
2 1 2 2 2 3
100 100 0 1 2
60 60 2 3 5
100 90 0 3 4
90 100 1 2 0
90 90 5 1 3
80 90 1 0 2
80 80 0 1 2
80 80 0 1 2
80 70 1 3 2
70 80 1 2 3
100 100 0 2 4

Sample Output:

0 10
3
5 6 7
2 8

1 4
#include<cstdio>
#include<algorithm>
using namespace std;

struct Student{
	int GE, GI, sum;
	int rank, id;
	int cho[6];
}stu[40010];

struct school{
	int quota;
	int stuNum;
	int id[40010];
	int lastAdmit;
}sch[110];

bool cmpStu(Student s1, Student s2) {
	if (s1.sum != s2.sum)
		return s1.sum > s2.sum;
	else
		return s1.GE > s2.GE;
}

bool cmpID(int a,int b) {
	return stu[a].id < stu[b].id;
}

int main() {
	int n, m, k;//考生数,学校数,每个学生可以申请的学校数
	scanf("%d%d%d", &n, &m, &k);

	for (int i = 0; i < m; i++) {
		scanf("%d", &sch[i].quota);
		sch[i].stuNum = 0;
		sch[i].lastAdmit = -1;
	}

	for (int i = 0; i < n; i++) {
		stu[i].id = i;
		scanf("%d %d", &stu[i].GE, &stu[i].GI);
		stu[i].sum = stu[i].GE + stu[i].GI;
		for (int j = 0; j < k; j++) {
			scanf("%d", &stu[i].cho[j]);
		}
	}

	sort(stu, stu + n, cmpStu);

	for (int i = 0; i < n; i++) {
		if (i > 0 && stu[i].sum == stu[i - 1].sum && stu[i].GE == stu[i - 1].GE) {
			stu[i].rank = stu[i - 1].rank;
		}
		else {
			stu[i].rank = i;
		}
	}

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < k; j++) {
			int choice = stu[i].cho[j];
			int num = sch[choice].stuNum;
			int last = sch[choice].lastAdmit;

			if (num < sch[choice].quota || (last != -1 && stu[i].rank == stu[last].rank)) {
				sch[choice].id[num] = i;
				sch[choice].lastAdmit = i;
				sch[choice].stuNum++;
				break;
			}
		}
	}

	for (int i = 0; i < m; i++) {
		if (sch[i].stuNum > 0) {
			sort(sch[i].id, sch[i].id + sch[i].stuNum, cmpID);
			for (int j = 0; j < sch[i].stuNum; j++) {
				printf("%d", stu[sch[i].id[j]].id);
				if (j < sch[i].stuNum - 1) {
					printf(" ");
				}
			}
		}
		printf("\n");
	}
	return 0;
}

? *4.1 PAT A1095

Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (≤104), the number of records, and K (≤8×104) the number of queries. Then N lines follow, each gives a record in the format:

plate_number hh:mm:ss status

where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. Each in record is paired with the chronologically next record for the same car provided it is an out record. Any in records that are not paired with an out record are ignored, as are out records not paired with an in record. It is guaranteed that at least one car is well paired in the input, and no car is both in and out at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:

For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

Sample Input:

16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00

Sample Output:

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值