icpc:problems:usaco23open_field_day_s
目录
problems | |
---|---|
名称 | Field Day S |
题目编号 | USACO23OPEN_S2 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | BFS, 状态压缩 |
难易程度 | 容易 |
Field Day S
想法
位数的范围为18,可以状态压缩来存储所有的情况。题目想要求每个序列在集合中的最大差异值,可以转换为求在相反集合中的最小差异值。
使用广搜来求每个情况到各个相反情况的最小差异值。最后结果为 位数 - 相反情况的最小差异值。
代码实现
#include<cstdio> #include<cstring> #include<queue> const int N = 1e5+10; const int ALL = (1<<19)-1; int dis[ALL], line[N]; bool vis[ALL]; int main() { int c, n; char temp[20]; memset(dis, 0x7f, sizeof(dis)); std::queue<int>qu; scanf("%d %d\n", &c, &n); for (int i = 1; i <= n; ++i) { scanf("%s", temp+1); int front=0, back=0; for (int t = 1; t <= c; ++t) { front <<= 1; back <<= 1; if(temp[t] == 'G') front += 1; else back += 1; } line[i] = front; qu.push(back); dis[back] = 0; vis[back] = true; } while(!qu.empty()) { int now = qu.front(); qu.pop(); vis[now] = false; int sit = 1; for (int i = 1; i <= c; ++i, sit<<=1) { int t = now ^ sit; if(dis[t] > dis[now] + 1) { dis[t] = dis[now] + 1; if(vis[t])continue; vis[t] = true; qu.push(t); } } } for (int i = 1; i <= n; ++i) printf("%d\n", c - dis[line[i]]); return 0; }
/app/www/public/data/pages/icpc/problems/usaco23open_field_day_s.txt · 最后更改: 2023/05/24 09:04 由 温婕莺