上机笔记4.4

本文介绍了PAT(A1033, A1037, A1067, A1038)四道算法题目,涉及最优化问题和排序。A1033要求找出从Hangzhou出发到目的地的最低油费路线;A1037涉及魔法优惠券最大收益计算;A1067求解排序数组所需的最小交换次数;A1038则需要从数字段中构建最小数字。" 58889,4432,VB.NET实现MD5加密详解,"['VB.NET', '加密算法', '数据安全', 'Windows应用开发', '编程实践']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

? *4.4 PAT A1033

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (≤ 100), the maximum capacity of the tank; D (≤30000), the distance between Hangzhou and the destination city; Davg (≤20), the average distance per unit gas that the car can run; and N (≤ 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: P**i, the unit gas price, and D**i (≤D), the distance between this station and Hangzhou, for i=1,⋯,N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print The maximum travel distance = X where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:

50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300

Sample Output 1:

749.17

Sample Input 2:

50 1300 12 2
7.10 0
7.00 600

Sample Output 2:

The maximum travel distance = 1200.00
#include<stdio.h>	
#include<algorithm>
using namespace std;

const int maxn = 510;
const int INF = 1000000000;
struct station {
	double price, dist;
}st[maxn];

bool cmp(station s1,station s2) {
	return s1.dist < s2.dist;
}

int main() {
	int n;
	double c, d, davg;
	scanf("%lf %lf %lf %d", &c, &d, &davg, &n);
	for (int i = 0; i < n; i++) {
		scanf("%lf %lf", &st[i].price, &st[i].dist);
	}
	st[n].price = 0;
	st[n].dist = d;
	sort(st, st + n, cmp);
	if (st[0].dist != 0) {
		printf("The maximun travel distance = 0.00\n");
	}
	else {
		int now = 0;
		double ans = 0, nowTank = 0, max = c * davg;
		while (now < n) {
			int k = -1;
			double pricemin = INF;
			for (int i = now + 1; i <= n && st[i].dist - st[now].dist <= max; i++) {
				if (st[i].price < pricemin) {
					pricemin = st[i].price;
					k = i;
					if (pricemin < st[now].price) {
						break;
					}
				}
			}
			if (k == -1)
				break;
			double need = (st[k].dist - st[now].dist) / davg;
			if (pricemin < st[now].price) {
				if (nowTank < need) {
					ans += (need - nowTank) * st[now].price;
					nowTank = 0;
				}
				else {
					nowTank -= need;
				}
			}
			else {
				ans += (c - nowTank) * st[now].price;
				nowTank = c - need;
			}
			now = k;
		}
		if (now == n) {
			printf("%.2f\n", ans);
		}
		else {
			printf("The maximun travel distance = %.2f\n", st[now].dist + max);
		}
	}

	return 0;
}

? *4.4 PAT A1037

The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus product for free. However, if you apply a coupon with a positive N to this bonus product, you will have to pay the shop N times the value of the bonus product… but hey, magically, they have some coupons with negative N’s!

For example, given a set of coupons { 1 2 4 −1 }, and a set of product values { 7 6 −2 −3 } (in Mars dollars M$) where a negative value corresponds to a bonus product. You can apply coupon 3 (with N being 4) to product 1 (with value M$7) to get M$28 back; coupon 2 to product 2 to get M$12 back; and coupon 4 to product 4 to get M$3 back. On the other hand, if you apply coupon 3 to product 4, you will have to pay M$12 to the shop.

Each coupon and each product may be selected at most once. Your task is to get as much money back as possible.

Input Specification:

Each input file contains one test case. For each case, the first line contains the number of coupons N**C, followed by a line with N**C coupon integers. Then the next line contains the number of products N**P, followed by a line with N**P product values. Here 1≤N**C,N**P≤105, and it is guaranteed that all the numbers will not exceed 230.

Output Specification:

For each test case, simply print in a line the maximum amount of money you can get back.

Sample Input:

4
1 2 4 -1
4
7 6 -2 -3

Sample Output:

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

const int maxn = 100010;
int coupon[maxn], product[maxn];

int main() {
	int n, m;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &coupon[i]);
	}
	scanf("%d", &m);
	for (int i = 0; i < m; i++) {
		scanf("%d", &product[i]);
	}
	sort(coupon, coupon + n);
	sort(product, product + m);

	int i = 0, j, ans = 0;
	while (i < n && i < m && coupon[i] < 0 && product[i] < 0) {
		ans += coupon[i] * product[i];
		i++;
	}
	i = n - 1;
	j = m - 1;
	while (i >= 0 && j >= 0 && coupon[i] > 0 && product[j] > 0) {
		ans += coupon[i] * product[j];
		i--;
		j--;
	}
	printf("%d", ans);
	return 0;
}

? *4.4 PAT A1067

Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way:

Swap(0, 1) => {4, 1, 2, 0, 3}
Swap(0, 3) => {4, 1, 2, 3, 0}
Swap(0, 4) => {0, 1, 2, 3, 4}

Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers.

Input Specification:

Each input file contains one test case, which gives a positive N (≤105) followed by a permutation sequence of {0, 1, …, N−1}. All the numbers in a line are separated by a space.

Output Specification:

For each case, simply print in a line the minimum number of swaps need to sort the given permutation.

Sample Input:

10
3 5 7 2 6 4 9 0 8 1

Sample Output:

9
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 100010;
int pos[maxn];

int main() {
	int n, ans = 0;
	scanf("%d", &n);
	int left = n - 1, num;
	for (int i = 0; i < n; i++) {
		scanf("%d", &num);
		pos[num] = i;
		//10
		//3 5 7 2 6 4 9 0 8 1
		//7 9 3 0 5 1 4 2 8 6
		if (num == i && num != 0) {
			left--;
		}
	}
	for (int i = 0; i < n; i++) {
		printf("%d ", pos[i]);
	}
	printf("\n==============\n");
	int k = 1;
	while (left > 0) {
		if (pos[0] == 0) {
			while (k < n) {
				if (pos[k] != k) {
					swap(pos[0], pos[k]);
					ans++;
					break;
				}
				k++;
			}
		}
		while (pos[0] != 0) {
			swap(pos[0], pos[pos[0]]);
			for (int i = 0; i < n; i++) {
				printf("%d ", pos[i]);
			}
			printf("\n~~~\n");
			ans++;
			left--;
		}
	}
	printf("%d", ans);
	return 0;
}
----------------------------------------;
10
3 5 7 2 6 4 9 0 8 1
7 9 3 0 5 1 4 2 8 6
==============
2 9 3 0 5 1 4 7 8 6
~~~
3 9 2 0 5 1 4 7 8 6
~~~
0 9 2 3 5 1 4 7 8 6
~~~
6 0 2 3 5 1 4 7 8 9
~~~
4 0 2 3 5 1 6 7 8 9
~~~
5 0 2 3 4 1 6 7 8 9
~~~
1 0 2 3 4 5 6 7 8 9
~~~
0 1 2 3 4 5 6 7 8 9
~~~
9

? *4.4 PAT A1038

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤104) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

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

string str[10010];
bool cmp(string a, string b) {
	return a + b < b + a;
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str[i];
	}
	sort(str, str + n, cmp);
	for (int i = 0; i < n; i++) {
		cout << str[i] << endl;
	}
	string ans;
	for (int i = 0; i < n; i++) {
		ans += str[i];
	}
	while (ans.size() != 0 && ans[0] == '0') {
		ans.erase(ans.begin());
	}
	if (ans.size() == 0) {
		cout << 0;
	}	
	else {
		cout << ans;
	}
	return 0;
}
------------------------------------------;
5 32 321 3214 0229 87
0229
321
3214
32
87
22932132143287
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值