CodeForces 1141F2 贪心 离散化

CodeForces 1141F2 贪心 离散化

题意

给定一个序列,要求我们找出最多数量的不相交区间,每个区间和都相等。

思路

一开始没有头绪,不过看到 \(n \le 1500\) 后想到可以把所有的区间和都保存下来。那么就可以遍历n2个区间和,用一个vector保存和相同的区间,(因为区间和可能很大,所以可以离散化)之后对于每个vector,都进行以右端点为主的排序,然后顺着取合法不相交区间,即可得到关于当前和的最多的不相交区间,最终得到答案。

  • 时间复杂度:小于 \(O(n^2logn)\)

AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
using namespace std;

int n;
struct ab
{
	int l;
	int r;
	bool operator < (const struct ab& c) const
	{
		return r < c.r;
	}
};
map<long long, int>mm;
long long su[1505];
long long cc[2500005], co = 0;
vector<struct ab> D[2500005];

int main()
{
	scanf("%d", &n);
	su[0] = 0;
	for (int i = 1; i <= n; ++i)
	{
		long long xx;
		scanf("%lld", &xx);
		su[i] = su[i - 1] + xx;
	}
	for (int i = 1; i <= n; ++i)
	{
		for (int j = i; j <= n; ++j)
		{
			if (!mm.count(su[j] - su[i - 1]))
			{
				mm[su[j] - su[i - 1]] = ++co;
				cc[co] = su[j] - su[i - 1];
			}
			D[mm[su[j] - su[i - 1]]].push_back((struct ab){i, j});
		}
	}
	int ansl = 0, ans = 0;
	for (int i = 1; i <= co; ++i)
	{
		int ind = mm[cc[i]];
		sort(D[ind].begin(), D[ind].end());
		int rr = 0, anss = 0;
		for (unsigned int j = 0; j < D[ind].size(); ++j)
		{
			if (D[ind][j].l > rr)
			{
				++anss;
				rr = D[ind][j].r;
			}
		}
		if (anss > ans)
		{
			ansl = i;
			ans = anss;
		}
	}
	printf("%d\n", ans);
	int rr = 0;
	for (unsigned int i = 0; i < D[ansl].size(); ++i)
	{
		if (D[ansl][i].l > rr)
		{
			rr = D[ansl][i].r;
			printf("%d %d\n", D[ansl][i].l, D[ansl][i].r);
		}
	}
	return 0;
}
posted @ 2020-12-08 19:45  _int_me  阅读(83)  评论(0编辑  收藏  举报