【HT-059-Rainbow】核桃彩虹周赛
T3 门



赛时思路:
对于每个钻石,dfs寻找相邻的水方块
如果碰到草方块,直接标记为不可能(但由于dfs这一部分处理的不好)
如果长度>=4,再递归一遍,将水方块改成传送门
结果:RE
可能爆栈了,也可能字符串输入处理有误
警示:能用BFS别用DFS
正解:
从钻石的位置开始遍历水方块,如果相邻方块有泥土说明这个水坑不能转化为传送门。
然后再遍历一遍把能转化成传送门的水坑变成传送门。
注意不需要从同一个水坑里的其它钻石位置再次遍历。//优化复杂度
复杂度 O(nm+k)
其实和我思路差不多
正解代码:
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<string> a(n);
for (int i=0; i<n; i++) {
cin >> a[i];
}
vector<vector<int>> vis(n, vector<int>(m));
vector<int> xx = {1, -1, 0, 0};
vector<int> yy = {0, 0, 1, -1};
auto bfs1 = [&](int x0, int y0) -> int {
int tot = 0;
if (vis[x0][y0] || a[x0][y0]!='.') {
return 0;
}
queue<pair<int, int>> q;
q.emplace(x0, y0);
vis[x0][y0] = 1;
while (q.size()) {
auto [x, y] = q.front();
q.pop();
tot++;
for (int i=0; i<4; i++) {
int u = x+xx[i], v = y+yy[i];
if (a[u][v]=='#') {
tot = -1e9;
}
if (a[u][v]=='.' && !vis[u][v]) {
q.emplace(u, v);
vis[u][v] = 1;
}
}
}
return tot;
};
auto bfs2 = [&](int x0, int y0) -> void {
queue<pair<int, int>> q;
q.emplace(x0, y0);
a[x0][y0] = '@';
while (q.size()) {
auto [x, y] = q.front();
q.pop();
for (int i=0; i<4; i++) {
int u = x+xx[i], v = y+yy[i];
if (a[u][v]=='.') {
q.emplace(u, v);
a[u][v] = '@';
}
}
}
};
for (int i=0; i<k; i++) {
int x, y;
cin >> x >> y;
x--, y--;
if (bfs1(x, y)>=4) {
bfs2(x, y);
}
}
for (int i=0; i<n; i++) {
cout << a[i] << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
T4 集


赛时切!
思路:
先贪心考虑,要使值最小,需要将A从大到小排序,从大到小插入B
考虑ai的贡献

最后加起来就行
注意2^(n-2)可以提出来最后算
官方题解思路:

最后的式子其实是一样的
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<bits/stdc++.h>
#define int long long
using namespace std;
int ksm(int a,int b,int p){
if(b==0) return 1;
if(b==1) return a%p;
int c=ksm(a,b/2,p);
c=c*c%p;
if(b%2==1) c=c*a%p;
return c%p;
}
inline int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
const int MOD=998244353;
int a[100005];
signed main()
{
//freopen("filename.in", "r", stdin);
//freopen("filename.out", "w", stdout);
int T=read();
while(T--){
int n=read();
int ans=0;
for(int i=1;i<=n;i++) a[i]=read();
if(n==1){
cout<<0<<'\n';
continue;
}
sort(a+1,a+n+1);
int cnt=0;
for(int i=n;i>=1;i--){
cnt++;
ans=(ans+(cnt-1)*a[i]%MOD)%MOD;
}
ans=ans*ksm(2,n-2,MOD)%MOD;
cout<<ans<<'\n';
}
return 0;
}

浙公网安备 33010602011771号