目录
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;
}