社区讨论

vector 与 multiset 相同操作时间复杂度

学术版参与者 2已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@mhjsvmqw
此快照首次捕获于
2025/11/04 07:57
4 个月前
此快照最后确认于
2025/11/04 07:57
4 个月前
查看原帖
相当于插入后排序。
CPP
vector<int> A;
int main () {
	scanf ("%d", &n);
	for (int i = 1; i <= n; ++i) scanf ("%d", &a[i]);
	for (int i = 1; i <= n; ++i) {
		A.insert (lower_bound (A.begin (), A.end (), a[i]), a[i]);
		int f = 0;
		for (auto j : A) {
			if (f > j) f--;
			else if (f < j) f++;
		}
		printf ("%d\n", f);
	}
	return 0;
}
某道题以上方法拿了更多的分数(开了 sub)
但是以下方法 T 了
CPP
multiset<int> s;
signed main() {
	ios::sync_with_stdio(false);
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	for (int i = 1; i <= n; i++) {
		s.insert(a[i]);
		int ans = 0;
		for (auto j : s) {
			if (ans < j) {
				ans++;
			} else if (ans > j) {
				ans--;
			}
		}
		cout << ans << "\n";
	}
	return 0;
}
当然,两份代码都是部分分,但是不知道为什么第二份不能拿更多的分数。

回复

5 条回复,欢迎继续交流。

正在加载回复...