Basic algorithm training: still pat level B (25 points have been completed! AK!)

Question 1: 1070 knots (25 points)

Insert picture description here

My AC code: This question does not resolve the small top heap

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

priority_queue<double, vector<double>, greater<double> > q;
int main() {
    
    
	int n;
	cin >> n;
	while (n--) {
    
    
		double a;
		cin >> a;
		q.push(a);
	}
	while (1 < (int)q.size()) {
    
    
		double f = q.top(); q.pop();
		double s = q.top(); q.pop();
		double Next = f / 2.0 + s / 2.0;
		q.push(Next);
	}
	cout << floor(q.top());
	return 0;
}

Question 2: 1065 Single Dogs (25 points)

Insert picture description here

My AC code: Note that if there is no order for guests, *(ans.begin()-1) will be illegally accessed! That’s because of the "segment fault" at the second test point

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 10005;
map<int, int> mp;
int n, m, a[maxn], b[maxn];
vector<int> ans;
int main() {
    
    
	scanf("%d", &n);
	while (n--) {
    
    
		int a, b;
		scanf("%d %d", &a, &b);
		mp[a] = b;
		mp[b] = a;
	}
	scanf("%d", &m);
	fill(a, a + m, -1);
	fill(b, b + m, -1);
	for (int i = 0; i < m; ++i) {
    
    
		scanf("%d", &a[i]);
		b[i] = a[i];
	}
	sort(b, b + m);
	for (int i = 0; i < m; ++i) {
    
    
		if (mp.find(a[i]) == mp.end())
			ans.push_back(a[i]);
		else if (!binary_search(b, b + m, mp[a[i]]))
			ans.push_back(a[i]);
	}
	printf("%d\n", (int)ans.size());
	if ((int)ans.size() > 0) {
    
    
		sort(ans.begin(), ans.end());
		for (vector<int>::iterator it = ans.begin(); ans.end() - 1 != it; ++it)
			printf("%05d ", *it);
		printf("%05d", ans[(int)ans.size() - 1]);
	}
	return 0;
}

The third question: 1050 spiral matrix (25 points)

Insert picture description here

My AC code: I guess you must remember that I used to sign in for the C++ final exam. At that time, I was ashamed, even ashamed, and even hated myself and failed to sign in.

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

bool cmp(int a, int b) {
    
     return a > b; }
int main() {
    
    
	int n, a, r, c;
	vector<int> Vec;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i) {
    
    
		scanf("%d", &a);
		Vec.push_back(a);
	}
	for (int i = (int)sqrt(n); i >= 1; --i) 
		if (!(n % i)) 
		{
    
    
			c = i, r = n / i;
			break;
		}
	sort(Vec.begin(), Vec.end(), cmp);
	int** ans = new int* [r];
	for (int i = 0; i < r; ++i)
		ans[i] = new int[c];
	int i = 0, j = 0, Row1 = 0, Row2 = r - 1, Col1 = 0, Col2 = c - 1;
	while (i < n) {
    
    
		if (!(j & 1)) {
    
    
			if (0 == j % 2 && 0 == j % 4) {
    
    
				for (int p = Col1; p <= Col2; ++p, ++i)
					ans[Row1][p] = Vec[i];
				j++;
				Row1++;
			}
			else {
    
    
				for (int p = Col2; p >= Col1; --p, ++i)
					ans[Row2][p] = Vec[i];
				j++;
				Row2--;
			}
		}
		else {
    
    
			if (1 == j % 2 && 1 == j % 4) {
    
    
				for (int p = Row1; p <= Row2; ++p, ++i)
					ans[p][Col2] = Vec[i];
				Col2--;
				j++;
			}
			else {
    
    
				for (int p = Row2; p >= Row1; --p, ++i)
					ans[p][Col1] = Vec[i];
				Col1++;
				j++;
			}
		}
	}
	for (int i = 0; i < r; ++i) {
    
    
		for (int j = 0; j < c; ++j) {
    
    
			if (j < c - 1)
				printf("%d ", ans[i][j]);
			else
				printf("%d", ans[i][j]);
		}
		printf("\n");
	}
	return 0;
}

Fourth question: 1045 quick sort (25 points)

Insert picture description here

My AC code: I will talk about the idea for this question:

At first, I thought it was too simple, use another array b[] to copy the original array a[], and then sort the array b[], if the order of an element in the ordered state is the same as the order in the original array , Indicating that the element of the original array is already in place. Later I constructed a test case:
5
5 4 3 2 1

To be reasonable, it should output 0, but the result is:
1
3 The
reason is: the
array after sorting: 1 2 3 4 5 The
original state array: 5 4 3 2 1 As
you can see, this 3 seems to be in place, but it is not. It should be in place, this reverse orderly state will cause our program to go wrong! How to solve this problem? At first, it was very brainstorming. Later, after reading Liu Ru's blog, I realized that I had missed one point! In the case of order, the i-th element must be the largest element among the first i elements! If it is in the reverse order, it is the smallest element, which distinguishes the order from the reverse order! Note that this bug is not only caused when all elements are completely reversed! As long as a continuous interval is completely reversed, this kind of bug will occur!

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 1e5 + 5;
int n, a[maxn], b[maxn], preMax[maxn];
vector<int> ans;
int main() {
    
    
	cin >> n;
	for (int i = 1; i <= n; ++i) {
    
    
		cin >> a[i];
		b[i] = a[i];
	}
	preMax[1] = a[1];
	for (int i = 2; i <= n; ++i) {
    
    
		if (a[i] > preMax[i - 1])
			preMax[i] = a[i];
		else
			preMax[i] = preMax[i - 1];
	}
	sort(b + 1, b + n + 1);
	for (int i = 1; i <= n; ++i)
		if (b[i] == a[i] && preMax[i] == a[i])
			ans.push_back(a[i]);
	cout << ans.size() << endl;
	if ((int)ans.size() > 0) {
    
    
		for (int i = 0; i < (int)ans.size() - 1; ++i)
			cout << ans[i] << ' ';
		cout << ans[(int)ans.size() - 1];
	}
	cout << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/105244109