ABC 363

目录

        C. Avoid K Palindrome 2 

        E. Sinking Land


 

 

 

C. Avoid K Palindrome 2 

 

        字符串截取:substr(i,len)

        字符串反转:reverse(s.begin(), s.end())

        全排列生成:next_permutation(next_permutation(&s[1], &s[n + 1]))

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, INF = 1e18;

int T, n, k, cnt, ans;
string s;

signed main()
{
	cin >> n >> k >> s;
	s = ' ' + s;
	sort(&s[1], &s[n + 1]);
	do
	{
		int p = 0;
		for (int i = 1; i <= n - k + 1; i ++)
		{
			string x = s.substr(i, k);
			string y = x;
			reverse(y.begin(), y.end());
			if (x == y)
			{
				p = 1;
				break;
			}
		}
		if (p == 0)
			ans ++;
	}while (next_permutation(&s[1], &s[n + 1]));
	cout << ans;
	return 0;
}

 

 

 

 

 

E. Sinking Land

        首先要想清楚优先队列里面存的是什么。应该存与海相邻的格子,然后通过在队列里的格子去扩展淹没。

        比较难想的是对于一个会被淹没的格子,它被淹没的时间取决于来淹没它的格子有多高。T 只增不减,对于当前被淹没的格子是贡献到 T 里面去的。

        这里必须要像 bfs 一样一层一层去扩展淹没,如果对一个队首的格子去 dfs 那会导致 T 偏大,所以每次只能扩一层。

        因为只算了每一个时间 T 的值,所以最后做一个前缀和

        优先队列默认是大根堆,用的是小于号,所以重载小于号。因为要从小到大,所以外面是小于号,里面是大于号,大的反而小,排在后面。

 

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, M = 1e3 + 5, INF = 1e18;

struct node
{
	int x, y, h;
	bool operator < (const node & other) const
	{
		return h > other.h;
	}
};

int T, n, m, Y, cnt, ans[N], a[M][M], vis[M][M];
priority_queue<node> pq;

signed main()
{
	cin >> n >> m >> Y;
	int T = 0;
	int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
	for (int i = 1; i <= n; i ++)
		for (int j = 1; j <= m; j ++)
		{
			cin >> a[i][j];
			if ((i == 1 || j == 1 || i == n || j == m) && vis[i][j] == 0)
				pq.push({i, j, a[i][j]}), vis[i][j] = 1;
		}
	while (!pq.empty())
	{
		node t = pq.top();	pq.pop();
		T = max(T, t.h);
		ans[T] ++;
		for (int i = 0; i < 4; i ++)
		{
			int tx = t.x + dx[i], ty = t.y + dy[i];
			if (tx < 1 || tx > n || ty < 1 || ty > m || vis[tx][ty] == 1)
				continue;
			vis[tx][ty] = 1;
			pq.push({tx, ty, a[tx][ty]});
		}
	}
	int tot = n * m;
	for (int i = 1; i <= Y; i ++)
		ans[i] += ans[i - 1];
	for (int i = 1; i <= Y; i ++)
		cout << tot - ans[i] << '\n';
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值