You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
63 lines
1.5 KiB
1 year ago
|
#include <bits/stdc++.h>
|
||
|
using namespace std;
|
||
|
/*
|
||
|
14
|
||
|
3 2 2 3 2 2 3 2 2 2 3 3 3 3
|
||
|
|
||
|
*/
|
||
|
const int N = 110;
|
||
|
int n;
|
||
|
int a[N];
|
||
|
int res; // 执行次数
|
||
|
int st[10]; // 哪些数字使用过了
|
||
|
int b[10];
|
||
|
// 结构体
|
||
|
struct Node {
|
||
|
int c;
|
||
|
int count;
|
||
|
int st, ed;
|
||
|
bool const operator<(const &Node t) {
|
||
|
if (count != t.count) return count > t.count;
|
||
|
if (st < t.st && ed > t.ed) return false;
|
||
|
return true;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// 找出未处理的数字中符合条件的那个
|
||
|
Node find() {
|
||
|
memset(b, 0, sizeof b);
|
||
|
vector<Node> q;
|
||
|
for (int i = 1; i <= n; i++)
|
||
|
if (!st[a[i]]) b[a[i]]++; // 个数
|
||
|
|
||
|
for (int i = 1; i <= 9; i++) {
|
||
|
int start, end;
|
||
|
for (int j = 1; j <= n; j++)
|
||
|
if (a[j] == i) {
|
||
|
start = j;
|
||
|
break;
|
||
|
}
|
||
|
for (int j = n; j >= 1; j--)
|
||
|
if (a[j] == i) {
|
||
|
end = j;
|
||
|
break;
|
||
|
}
|
||
|
q.push_back({i, b[i], start, end});
|
||
|
}
|
||
|
sort(q.begin(), q.end());
|
||
|
|
||
|
|
||
|
return q[0];
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
cin >> n;
|
||
|
for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组
|
||
|
for (res = 0;; res++) {
|
||
|
// 找出没有记录过的数字,并且,它的数量最多,如果存在数量一样多的多个数字,就比较谁的区间大,大的优先,如果无法比较谁的区间大,就数小的优先
|
||
|
Node x = find();
|
||
|
if (x.count == 0) break;
|
||
|
}
|
||
|
cout << res << endl;
|
||
|
return 0;
|
||
|
}
|