[Title] thinking CodeForce 1007A Reorder the Array

Copyright: Johnson https://blog.csdn.net/m0_38055352/article/details/91356718

To indulge this time to brush the question for some time, let CSDN accompany me with it!

First, the title effect

Entitled you mean a sequence length n is an integer of asking you rearrangement, rearrangement is required as much as possible the number of each position is large, such as the original location is a subscript 1, reordering the subscript 1 is position 3, then this is the bigger, the final requirements of the rearranged larger maximum number of subscripts.

Second, the subject of thinking and AC codes

For this question, or the solution can easily think of violence, and that is arranged directly through all the circumstances, and then taking a larger arrangement of each index value and the maximum value is obtained, of course, this is obviously not desirable complexity is too high.

So we have to consider how given him a strategy, this strategy can be found in accordance with the maximum number of larger index. We can put the array into the second largest number of large positions, then the second largest number into third position, so you can ensure minimal loss, it can guarantee the subscript larger number as much as possible, but it should be noted that there may be more than the maximum number, the second largest number may have more, it requires careful handling.

AC codes are given below:

#include <iostream>
#include <algorithm>
#define MAXN 100010
using namespace std;

typedef pair<int, int> pp;

int n;
pp a[MAXN];

bool cmp(pp a, pp b) {
	return a.first > b.first;
}

int main()
{
	cin >> n;

	for (int i = 0; i < n; i++) {
		a[i].second = i;
		cin >> a[i].first;
	}

	sort(a, a + n, cmp);
	bool* b = new bool[n];
	for (int i = 0; i < n; i++) {
		b[i] = false;
	}

	int cnt = 0;
	for (int i = 0; i < n; i++) {
		int j = i + 1;
		while (b[j] || (j < n && a[j].first == a[i].first)) {
			j++;
		}
		if (j >= n) break;
		b[j] = true;
		cnt++;
	}

	cout << cnt << endl;

    return 0;
}

If you have questions, please correct me! ! !

Guess you like

Origin blog.csdn.net/m0_38055352/article/details/91356718