main
黄海 1 year ago
parent 334b5fd56f
commit ba3dfe77f6

@ -1,68 +1,35 @@
#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];
// 结构体
int st[10];
struct Node {
int c;
int count;
int st, ed;
const bool operator<(const Node &t) const {
if (count != t.count) return count > t.count;
if (st < t.st && ed > t.ed) return false;
return true;
}
};
vector<Node> q;
/*
5
1 2 3 3 2
*/
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组
// 找出未处理的数字中符合条件的那个
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++) { // 遍历所有数字1~9
if (b[i] > 0) { // 如果是真实存在的有效的数字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;
for (int i = 1; i <= n; i++) {
if (!st[a[i]]) {
st[a[i]] = 1;
for (int j = n; j; j--)
if (a[i] == a[j]) {
q.push_back({i, j});
break;
}
// 记录数字,个数,开始,结束
q.push_back({i, b[i], start, end});
}
}
// 按结构体自定义的顺序进行排序
sort(q.begin(), q.end());
// 我们只关心第一个,也就是数量最多的那个
if (q.size()) return q[0];
return {0, 0, 0, 0};
}
// 从左到右枚举,所以,不再需再进行按左端点排序,现在就是按左端点排序完的
for (int i = 0; i < q.size(); i++) cout << q[i].st << " " << q[i].ed << endl;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i]; // 密码数组
for (res = 0;; res++) {
// 找出没有记录过的数字,并且,它的数量最多,如果存在数量一样多的多个数字,就比较谁的区间大,大的优先,如果无法比较谁的区间大,就数小的优先
Node x = find();
// 将x这个数字标识为已处理
st[x.c] = 1;
if (x.count == 0) break;
}
cout << res << endl;
return 0;
}

@ -0,0 +1,6 @@
https://gesp.ccf.org.cn/
18686619970
mdcija780522
进入后台
Loading…
Cancel
Save