PAT甲级1044,1047解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1044 Shopping in Mars (25 分)

Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:

  1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
  2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
  3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).

Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.

If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤10​5​​), the total number of diamonds on the chain, and M (≤10​8​​), the amount that the customer has to pay. Then the next line contains N positive numbers D​1​​⋯D​N​​ (D​i​​≤10​3​​ for all i=1,⋯,N) which are the values of the diamonds. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print i-j in a line for each pair of i ≤ j such that Di + ... + Dj= M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i.

If there is no solution, output i-j for pairs of i ≤ j such that Di + ... + Dj >Mwith (Di + ... + Dj −M) minimized. Again all the solutions must be printed in increasing order of i.

It is guaranteed that the total value of diamonds is sufficient to pay the given amount.

Sample Input 1:

16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13

Sample Output 1:

1-5
4-6
7-8
11-11

Sample Input 2:

5 13
2 4 5 7 9

Sample Output 2:

2-4
4-5

题目大意:就是给一个数组,一个目标值,求哪些连续区间和是等于这个目标值的,如果没有相等的,那就找最接近的。

解题思路:我是用树状数组和二分查找实现了。。后来查了一下不用树状数组也行的。算了,就当回顾了一下。

先讲树状数组,主要作用是对给定区间的求和与维护。利用一种像树一样的数据结构树状数组来实现。具体可以自行百度学习,这里简单讲就是把数组下标看成二进制。用一个数组记录一段和的值,比如c[6]记录的就是6-lowbit(6)到6的和,核心在于把数字看成二进制,这里的lowbit是返回最后一位在二进制表示中1的位置。比如6表示出110,然后lowbit就是2,然后就是记录第4个和第5个的和。然后就解决了求和的问题,然后在是二分查找,查找到一个比i大而且第一次比M大的索引位置,然后后退一个,如果正好是M。那么查找出所有是M位置的数就行了。如果不是那么就查找当前这个和就行了。目前不知道什么效率更高的算法了。然后我查了一下。。不用树状数组用普通的求和,就每次读入记录一下和也行。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
int TreeArr[100005];
int lowbit(int x) {
	return x&(-x);
}
int sum(int x, int *c) {
	int res = 0;
	while (x > 0) {
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}
void update(int x, int data, int *c,int size) {
	while (x <= size) {
		c[x] += data;
		x += lowbit(x);
	}
}
int BinarySort(int l, int r, int g) {
	int left = l;
	int right = r;
	int mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (g < sum(mid,TreeArr)) {
			right = mid;
		}
		else {
			left = mid + 1;
		}
	}
	return left;
}

int main()
{
	int N;
	int M;
	cin >> N >> M;
	memset(TreeArr, 0, sizeof(int)*N);
	for (int i = 1; i <= N; i++) {
		int cur;
		cin >> cur;
		update(i, cur, TreeArr, N);
	}
	int goal=99999999;
	
	for (int i = 1; i <= N; i++) {
		int j = BinarySort(1, N + 1, sum(i - 1, TreeArr)+M);
		if (sum(j - 1, TreeArr) - sum(i - 1, TreeArr) == M) {
			goal = M;
			break;
		}
		else if(j<=N&&sum(j,TreeArr)-sum(i-1,TreeArr)<goal){
			goal = sum(j, TreeArr) - sum(i - 1, TreeArr);
		}
	}
	for (int i = 1; i <= N; i++) {
		int j = BinarySort(1, N + 1, sum(i - 1, TreeArr) + goal);
		if (sum(j - 1, TreeArr) - sum(i - 1, TreeArr) == goal) {
			cout << i << "-" << j -1<< endl;
		}
	}
	return 0;
}

1047 Student List for Course (25 分)

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤40,000), the total number of students, and K (≤2,500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (≤20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.

Sample Input:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

题目大意:和以前的选课类似,这次输出以课程为主线序的表。

解题思路:照常求解有最后一个样例是过不去的,其实有点ACM味道,就是卡printfscanf和cout,cin的时间差,所以全部改成scanf和printf就过了,因为前者耗时短。有人会问C++string怎么读入,其实很简单的,string有个c_str函数变成char,然后有个利用copy函数可以把char里的内容复制到string里面。所以完全都是可以改成printf和scanf的,因为平时练习碰到了,所以以后尽量还是少用cin和cout。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
map<int, vector<string>> a;
bool cmp(string a, string b) { return a < b; }
int main()
{
	int n; int k;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		string cur;
		cin >> cur;
		int cu;
		scanf("%d", &cu);
		for (int j = 0; j < cu; j++) {
			int tp;
			scanf("%d", &tp);
			a[tp].push_back(cur);
		}
	}
	for (int i = 0; i < k; i++) {
		printf("%d %d\n", i + 1, a[i + 1].size());
		sort(a[i + 1].begin(), a[i + 1].end(),cmp);
		for (int j = 0; j < a[i + 1].size(); j++) {
			printf("%s\n", a[i + 1][j].c_str());
		}
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值