2019ICPC 南京网赛 F. Greedy Sequence 难想的暴力


2019ICPC 南京网赛 F. Greedy Sequence


标签

  • 难想的暴力

前言

  • 我的csdn和博客园是同步的,欢迎来访danzh-博客园~
  • 这题读题好费劲…

简明题意

  • 读题太费劲了。
  • 给定序列a[],现在需要你构造n个序列: s 1 s n s_1-s_n
  • 每个序列 s i [ 1 ] = i s_i[1]=i ,然后对于每个s序列,相邻的两个数在a[]序列中的下标差不超过k。现在每个序列需要字典序最大,然后问你所有数列的数字数量累加起来是多少。

思路

  • 字典序最大是突破口。有了这个限制,我们可以直接从序列的第一个元素开始考虑。第一个元素是i,那么直接遍历i-1到1,一旦满足下标差<=k,就应该添加到s中。
  • 我代码中的排序应该是不用写的。

注意事项


总结


AC代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;

struct Node
{
	int val, pos;
	bool operator < (const Node& a)
	{
		return val < a.val;
	}
};

int n, k, ans[maxn];
Node a[maxn];

void solve()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d", &n, &k);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i].val), a[i].pos = i;
		sort(a + 1, a + 1 + n);

		int cnt = 0;
		for (int i = 1; i <= n; i++)
		{
			ans[i] = 1;
			for (int j = i - 1; j >= 1; j--)
				if (abs(a[j].pos - a[i].pos) <= k)
				{
					ans[i] = ans[j] + 1;
					break;
				}
			printf("%d", ans[i]);
			if (i != n)
				printf(" ");
		}
		printf("\n");
	}
}

int main()
{
	//freopen("Testin.txt", "r", stdin);
	solve();
	return 0;
}
发布了109 篇原创文章 · 获赞 33 · 访问量 3518

猜你喜欢

转载自blog.csdn.net/weixin_42431507/article/details/100521492