欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > ABC 363

ABC 363

2025/3/31 14:15:02 来源:https://blog.csdn.net/2301_81608637/article/details/146568544  浏览:    关键词: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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com