PAT2020甲级“秋季”真题

第一题 - - Panda and PP Milk (20 分)

在这里插入图片描述

PP milk (盆盆奶)is Pandas’ favorite. They would line up to enjoy it as show in the picture. On the other hand, they could drink in peace only if they believe that the amount of PP milk is fairly distributed, that is, fatter panda can have more milk, and the ones with equal weight may have the same amount. Since they are lined up, each panda can only compare with its neighbor(s), and if it thinks this is unfair, the panda would fight with its neighbor.

Given that the minimum amount of milk a panda must drink is 200 ml. It is only when another bowl of milk is at least 100 ml more than its own that a panda can sense the difference.

Now given the weights of a line of pandas, your job is to help the breeder(饲养员)to decide the minimum total amount of milk that he/she must prepare, provided that the pandas are lined up in the given order.

Input Specification:
Each input file contains one test case. For each case, first a positive integer n (≤10
​4
​​ ) is given as the number of pandas. Then in the next line, n positive integers are given as the weights (in kg) of the pandas, each no more than 200. the numbers are separated by spaces.

Output Specification:
For each test case, print in a line the minimum total amount of milk that the breeder must prepare, to make sure that all the pandas can drink in peace.

Sample Input:

10
180 160 100 150 145 142 138 138 138 140

Sample Output:

3000

Hint:
The distribution of milk is the following:

400 300 200 500 400 300 200 200 200 300

AC代码(AC时心态快崩了, 真的)
该题的题解来源:点击链接

#include<iostream>
using namespace std;

const int maxn = 10010;
int n, weight[maxn], attr[maxn], r[maxn] = {0}, l[maxn] = {0}, sum = 0;
int main() {
	cin >> n;
	fill(l, l + n, 2);
	fill(r, r + n, 2);
	for(int i = 0; i < n; i++) cin>> weight[i];
	int pre = weight[n - 1];
	int i = n - 1;
	for(int i = n - 2; i >= 0; i--) {
		if(weight[i] > pre) r[i] = r[i + 1] + 1;
		else if(weight[i] == pre) r[i] = r[i + 1];
		pre = weight[i];
	}
	pre = weight[0];
	for(int i = 1; i < n; i++) {
		if(weight[i] > pre) l[i] = l[i - 1] + 1;
		else if(weight[i] == pre) l[i] = l[i - 1];
		pre = weight[i];
	}
	for(int i = 0; i < n; i++) attr[i] = max(l[i], r[i]);
	for(int i = 0; i < n; i++) sum += attr[i];
	cout << sum * 100;
	return 0;
}

一开始的错误解法

#include<iostream>
using namespace std;

int n, weight[10010], attr[10010], sum = 0;
int main() {
	cin >> n;
	fill(attr, attr + n, 200);
	for(int i = 0; i < n; i++) cin>> weight[i];
	for(int i = n - 1; i > 0; i--)
		if(weight[i - 1] > weight[i]) attr[i - 1] = attr[i] + 100;
	if(weight[n - 1] > weight[n - 2]) attr[n - 1] = attr[n - 2] + 100;
	for(int i = 0;i<n;i++) 
		sum += attr[i];
	cout << sum;
	return 0;
}
// 180 160 100 150 145 142 138 138 140 140 140
// 如果数据的末尾有多个相同的数据,则上述算法出错
// 上述算法得到的结果为
// 400 300 200 500 400 300 200 200 200 200 300
//正确的应该是
// 400 300 200 500 400 300 200 200 300 300 300

第二题 - - How Many Ways to Buy a Piece of Land (25 分)

The land is for sale in CyberCity, and is divided into several pieces. Here it is assumed that each piece of land has exactly two neighboring pieces, except the first and the last that have only one. One can buy several contiguous(连续的) pieces at a time. Now given the list of prices of the land pieces, your job is to tell a customer in how many different ways that he/she can buy with a certain amount of money.

Input Specification:
Each input file contains one test case. Each case first gives in a line two positive integers: N (≤10
​4
​​ ), the number of pieces of the land (hence the land pieces are numbered from 1 to N in order), and M (≤10
​9
​​ ), the amount of money that your customer has.

Then in the next line, N positive integers are given, where the i-th one is the price of the i-th piece of the land.

It is guaranteed that the total price of the land is no more than 10
​9
​​ .

Output Specification:
For each test case, print the number of different ways that your customer can buy. Notice that the pieces must be contiguous.

Sample Input:

5 85
38 42 15 24 9

Sample Output:

11

Hint:
The 11 different ways are:

38
42
15
24
9
38 42
42 15
42 15 24
15 24
15 24 9
24 9

AC代码

#include<iostream>
using namespace std; 

int n, m, piece[10010], count = 0;
int main() {
	cin >> n >> m;
	for(int i = 0;i<n;i++) cin >> piece[i];
	for(int i = 0; i < n; i++) {
		int total = 0;
		for(int j = i; j < n; j++) {
			total += piece[j];
			if(total <= m) {
				count++;
			}
			else break;
		}
	}
	cout << count;
	return 0;
} 

第三题 - - Left-View of Binary Tree (25 分)

The left-view of a binary tree is a list of nodes obtained by looking at the tree from left hand side and from top down. For example, given a tree shown by the figure, its left-view is { 1, 2, 3, 4, 5 }

在这里插入图片描述

Given the inorder and preorder traversal sequences of a binary tree, you are supposed to output its left-view.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤20), which is the total number of nodes in the tree. Then given in the following 2 lines are the inorder and preorder traversal sequences of the tree, respectively. All the keys in the tree are distinct positive integers in the range of int.

Output Specification:
For each case, print in a line the left-view of the tree. All the numbers in a line are separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

8
2 3 1 5 4 7 8 6
1 2 3 6 7 4 5 8

Sample Output:

1 2 3 4 5

AC代码

#include<iostream>
#include<queue>
using namespace std;

int n, in[25], pre[25], maxLayer = -1;
vector<pair<int, int>> num;
struct node{
	int data, layer;
	node *lchild, *rchild;
};
node* create(int preL, int preR, int inL, int inR) {
	if(preL > preR) return NULL;
	node* root = new node;
	root -> data = pre[preL];
	int k = inL;
	while(in[k] != pre[preL]) k++;
	int numLeft = k - inL;
	root -> lchild = create(preL + 1, preL + numLeft, inL, k - 1); 
	root -> rchild = create(preL + numLeft + 1, preR, k + 1, inR);
	return root;
}
void BFS(node* root) {
	queue<node*> q;
	q.push(root);
	while(!q.empty()) {
		node* top = q.front(); 
		if(top->layer > maxLayer) maxLayer = top ->layer;
		q.pop();
		num.push_back(make_pair(top -> layer, top -> data));
		if(top -> lchild) {
			top-> lchild->layer = top->layer + 1;
			q.push(top->lchild);
		}
		if(top->rchild) {
			top->rchild->layer = top->layer + 1;
			q.push(top->rchild);
		}
	}
}

int main() {
	cin >> n;
	for(int i = 0; i < n; i++) cin >> in[i];
	for(int i = 0; i < n; i++) cin >> pre[i];
	node* root = create(0, n - 1, 0, n - 1);
	root -> layer = 0;
	BFS(root);
	int ans = 0, index = 0;
	for(int i = 0; i <= maxLayer; i++) {
		bool flag = true;
		for(; index < num.size(); index++) {
			if(num[index].first == i && flag) {
				if(ans++) cout << " ";
				cout << num[index].second;
				flag = false;
				break;
			}
		}
	} 
	return 0;
}

第四题 - - Professional Ability Test (30 分)

Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is a prerequisite (前置要求) of Level B if one must pass Level A with a score no less than S in order to be qualified to take Level B. At the mean time, one who passes Level A with a score no less than S will receive a voucher(代金券)of D yuans (Chinese dollar) for taking Level B.

At the moment, this PAT is only in design and hence people would make up different plans. A plan is NOT consistent if there exists some test T so that T is a prerequisite of itself. Your job is to test each plan and tell if it is a consistent one, and at the mean time, find the easiest way (with minimum total S) to obtain the certificate of any subject test. If the easiest way is not unique, find the one that one can win the maximum total value of vouchers.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (≤1000) and M, being the total numbers of tests and prerequisite relations, respectively. Then M lines follow, each describes a prerequisite relation in the following format:

T1 T2 S D
where T1 and T2 are the indices (from 0 to N−1) of the two distinct tests; S is the minimum score (in the range (0, 100]) required to pass T1 in order to be qualified to take T2; and D is the value of the voucher (in the range (0, 500]) one can receive if one passes T1 with a score no less than S and plan to take T2. It is guaranteed that at most one pair of S and D are defined for a prerequisite relation.

Then another positive integer K (≤N) is given, followed by K queries of tests. All the numbers in a line are separated by spaces.

Output Specification:
Print in the first line Okay. if the whole plan is consistent, or Impossible. if not.

If the plan is consistent, for each query of test T, print in a line the easiest way to obtain the certificate of this test, in the format:

T0->T1->…->T
However, if T is the first level of some subject test (with no prerequisite), print You may take test T directly. instead.

If the plan is impossible, for each query of test T, check if one can take it directly or not. If the answer is yes, print in a line You may take test T directly.; or print Error. instead.

Sample Input 1:

8 15
0 1 50 50
1 2 20 20
3 4 90 90
3 7 90 80
4 5 20 20
7 5 10 10
5 6 10 10
0 4 80 60
3 1 50 45
1 4 30 20
1 5 50 20
2 4 10 10
7 2 10 30
2 5 30 20
2 6 40 60
8
0 1 2 3 4 5 6 7

Sample Output 1:

Okay.
You may take test 0 directly.
0->1
0->1->2
You may take test 3 directly.
0->1->2->4
0->1->2->4->5
0->1->2->6
3->7

Sample Input 2:

4 5
0 1 1 10
1 2 2 10
3 0 4 10
3 2 5 10
2 0 3 10
2
3 1

Sample Output 2:

Impossible.
You may take test 3 directly.
Error.

AC代码

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值