当前位置 : 主页 > 编程语言 > c语言 >

H - DA sort (greedy)

来源:互联网 收集:自由互联 发布时间:2023-09-07
H · DA-Sort You recently learned a new way to sort an array of numbers in your algorithms course. The algorithm sorts an array of numbers by repeatedly performing the Delete-and-Append operation. The Delete- and-Append operation consists

H · DA-Sort

You recently learned a new way to sort an array of numbers in your algorithms course. The algorithm sorts an array of numbers by repeatedly performing the Delete-and-Append operation. The Delete- and-Append operation consists of three steps:

 

1) Choose an element from the array.

2) Delete the chosen element from the array.

3) Append the chosen element to the end of the array.

 

Being a curious student, you wonder what is the minimum number of Delete-and-Append operations required to sort a given array.

 

Input

 

The first line of input contains a single decimal integer P, (1 £ P £ 100), which is the number of data sets that follow. Each data set should be processed identically and independently.

 

Each data set consists of two or more lines of input. The first line contains the data set number, K, followed by a single space, followed by an integer N, (1 £ N £ 1000), which is the length of the array to sort. The remaining lines in the dataset contains N positive integers that comprise the array to be sorted, 10 values per line, except for the last line which may have less than 10 values. All the array elements are no larger than 109. The same value may appear more than once in the array to be sorted.

 

Output

 

For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by an integer which is the minimum number of Delete-and- Append operations required to sort the array.

 

Sample Input

Sample Output

3

1 3

1 3 2

2 6

1 5 2 4 3 6

3 23

67890 56312 999999999 12345 23456

38927 45632 100345 98765 23456

87654 43278 23456 117654 321899

25432 54326 217435 26845 31782

33456 41234 56213

1 1

2 3

3 15

题意:给定一个数组,每次选出一个数,删除(delete)并放在数组末尾(append),问最少几次这种operation可以将数组排序好。 我们将有重复元素的情况一块考虑,其实就是从原数组头到尾遍历一遍,但我们要和已经排序好且无重复元素的数组一块进行。用 cnt 统计数量。简单说,就是看原来数组有多少是已经排好序的。

map 映射即可:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define maxn 200005
#define inf 0x3f3f3f3f
#define ii 0x3f
const int mod = 1e9 + 7;


ll read() {
	ll x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') {
			f = -1;

		}
		ch = getchar();
	}
	while (ch >= '0'&&ch <= '9') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

ll quickpow(ll a, ll b) {
	ll ans = 1;
	while (b > 0) {
		if (b % 2)ans = ans * a;
		b = b / 2;
		a = a * a;
	}
	return ans;
}
//char s[maxn];

int gcd(int a, int b) {
	return b == 0 ? a : gcd(b, a%b);
}

int a[maxn];
int b[maxn];
int c[maxn];
map<ll, ll>mp;
map<ll, ll>vis;
int main() {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int ct;
		cin >> ct;
		int n;
		cin >> n;
		memset(b, 0, sizeof(b));
		memset(a, 0, sizeof(a));
		memset(c, 0, sizeof(c));
		int i, j;
		mp.clear();
		vis.clear();
		j = 0;
		int cnt = 0;
		for (i = 0; i < n; i++) {
			cin >> a[i];
			
			//if (mp[a[i]])cnt++;
			if (!mp[a[i]]) {
				b[j] = a[i];
				j++;

			}
			mp[a[i]]++;
			
		}
	//	cout << cnt << endl;
		sort(b, b + j);
		int k = 0;
		int ctt = 0;
		for (i = 0; i < n; i++) {
			if (b[k] == a[i]) {
				mp[a[i]]--;
				if (mp[a[i]] <= 0)k++;
			}
			else cnt++;
		}
		//cout << ctt << endl;
		//cnt += (n - ctt);
		cout << ct << ' ' << cnt << endl;
	}
}

上一篇:UESTC Training for summer selection E
下一篇:没有了
网友评论