PAT甲级1028,1029解题报告

 

1028:

1028 List Sorting (25 分)

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 (≤10​5​​) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then Nlines 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 90

Sample Output 3:

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

题目大意:输入总人数和tag,然后每个人的学号,姓名,成绩,要求按照tag的不同用不同方式排序得出排序结果。

解题思路:水题,排序排一下输出一下。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
using namespace std;
struct stu {
	string id;
	string name;
	int grade;
};
bool Cmp1(stu a,stu b){
	return a.id < b.id;
}
bool Cmp2(stu a, stu b) {
	if (a.name < b.name)return true;
	else if (a.name == b.name)return a.id < b.id;
	else return false;
}
bool Cmp3(stu a, stu b) {
	if (a.grade < b.grade)return true;
	else if (a.grade == b.grade)return a.id < b.id;
	else
		return false;
}
vector<stu> cur;
int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		stu tmp;
		cin >> tmp.id >> tmp.name >> tmp.grade;
		cur.push_back(tmp);
	}
	if (k == 1) {
		sort(cur.begin(), cur.end(), Cmp1);
	}
	else if (k == 2) {
		sort(cur.begin(), cur.end(), Cmp2);
	}
	else {
		sort(cur.begin(), cur.end(), Cmp3);
	}
	for (int i = 0; i < cur.size(); i++) {
		cout << cur[i].id << " " << cur[i].name << " " << cur[i].grade << endl;
	}
	return 0;
}

1029:

1029 Median (25 分)

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤2×10​5​​) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.

Output Specification:

For each test case you should output the median of the two given sequences in a line.

Sample Input:

4 11 12 13 14
5 9 10 15 16 17

Sample Output:

13

题目大意:给两串序列(n<=2e5),输出两串序列之间的中序数,每个数据不超出long int。

解题思路:在参考他人博客之前,没做对过,最高16.思路历程:

1.第一眼看到,没管直接STL里面list,因为很像数据结构基础题两个有序链表的合并,16分,gg。内存超出限制。

2.然后发现了题目的long int,第一眼的时候认为long int一定是4位的,因为感觉现代的个人PC,long int 和int是一样的,都是四位,然后是4*4e5的极端情况最后是应该不会超出1.5MB。

3.后来用类似注入的方法。。故意制造死循环。发现PAT系统的机器long int是8位的。。那肯定是超出限制了。也就是暗示我们在数据量极大的情况下,不能把两个数组全部读完。

4.然后一筹莫展的情况下,去参考了他人的题解,大多数的题解都是错误的。直到看到一个考虑到内存因素的。

5.看到用队列维护就有想法了,思路其实很简单,把第一个数组a读完,并在最后设置一个哨兵。不需要用long int存。long int怎么样都是超时的,把long int当成int处理就是了。哨兵为了方便,直接赋值最大的int。可以确定中序数是res=(n1+n2)%2!=0?(n1+n2)/2+1:(n1+n2)/2个;

6.然后维护队列,再读第二个数组b的时候,每读一个,把a,b中小的那个队头pop出去,直到要push第res个时候,把最终的结果输出。

7.考虑到可能是第二个数组比较短。所以要设置一个标志,如果还没有找到这个res结果,那么就继续累加,不断pop出a,b中的表头最小者直到输出。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<list>
#include<climits>
#include<queue>
using namespace std;
queue<int> a, b;
int main()
{
	int n1,n2;
	cin >> n1;
	for (int i = 0; i < n1; i++) {
		int num;
		cin >> num;
		a.push(num);
	}
	a.push(INT_MAX);
	cin >> n2;
	int index=0;
	int res = (n1 + n2) % 2 != 0 ? (n1 + n2) / 2 : (n1 + n2) / 2 - 1;
	int ans;
	bool flag = false;
	for (int i = 0; i < n2; i++) {
		int num;
		cin >> num;
		b.push(num);
		if (index == res) {
			ans=min(a.front(), b.front());
			flag = true;
			break;
		}
		if (a.front() < b.front())
			a.pop();
		else
			b.pop();
		index++;
	}
	if (flag) {
		cout << ans << endl;
	}
	else {
		b.push(INT_MAX);
		for (; index < res; index++) {
			if (a.front() < b.front())
				a.pop();
			else
				b.pop();
		}

		cout << min(a.front(), b.front()) << endl;
	}
	return 0;
}

题目的那个long int是真的坑。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值