1 2 3 4

牛客多校2020 Greater and Greater

https://ac.nowcoder.com/acm/contest/5667/G

题解其实有个前置知识,先整出一个O(n*m)的写法,很简单。但是需要优化。

利用bitset数1的个数计算答案,排好顺序。具体看神仙的代码吧,真的很强!我是看代码学会的

 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<bitset>
using namespace std;
const int maxn = 2e5 + 11;
vector<pair<int, int> >A, B;
bitset<maxn>ans, G;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n, m;

	cin >> n >> m;;
	int x;
	for (int i = 1; i <= n; i++) {
		cin >> x;
		A.push_back({ x,i });
	}
	for (int i = 1; i <= m; i++) {
		cin >> x;
		B.push_back({ x,i });
	}

	sort(A.begin(), A.end());
	sort(B.begin(), B.end());
	ans.set();
	for (int i = m - 1, j = n - 1; i >= 0; i--) {
		while (j >= 0 && A[j].first >= B[i].first) {
			G.set(A[j--].second);
		}
		ans &= G >> B[i].second;
	}
	cout << ans.count() << endl;

	return 0;
}

  

 

posted @ 2020-07-14 23:47  Lesning  阅读(224)  评论(0编辑  收藏  举报