WHU 1552 Seats 枚举
题意:
有一个年级中7个班的n个学生。
一天,他们毫无顺序的站成一排。请计算最小的交换次数,使得 相同班的同学都站在一起。 (只有站在一起的人才能交换)
思路:
如果知道班级的最终排列就能在很短的时间里,计算出所需的交换次数。那么,我们只需要枚举排列即可。
如果,a班的一个人要走到前面,那么,他必须和他前面所有其他班的人交换。
所以,先预处理出一个数组 f[i][j] 表示 每个 i 班的前面的 j 班同学的总和。然后就可以在m(班级数) * m 的时间里求出排列需要交换的次数。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; typedef long long ll; const int MAXN = (int) 1e5+;
const ll INF = (ll)<<; ll f[][], sum[]; //f[i][j] : i 前面有多少个 j
int a[MAXN];
int n;
ll ans;
bool vis[];
int od[]; void cal() {
ll res = ;
for (int i = ; i < ; i++) {
for (int j = ; od[j]!=i; j++) {
res += f[i][od[j]];
}
}
ans = min(ans, res);
} void dfs(int cnt) {
if (==cnt) {
cal();
return ;
}
for (int i = ; i < ; i++) if (!vis[i]) {
vis[i] = true;
od[cnt] = i;
dfs(cnt+);
vis[i] = false;
}
} void print() {
for (int i = ; i < ; i++) {
for (int j = ; j < ; j++)
printf("%d ", f[i][j]);
puts("");
}
} int main() {
#ifdef Phantom01
freopen("WHU1552.in", "r", stdin);
#endif // Phantom01 while (scanf("%d", &n)!=EOF) {
memset(vis, false, sizeof(vis));
memset(f, , sizeof(f));
memset(sum, , sizeof(sum));
for (int i = ; i < n; i++) {
scanf("%d", &a[i]);
sum[a[i]]++;
for (int j = ; j < ; j++) {
f[a[i]][j] += sum[j];
}
}
// print();
ans = INF;
dfs();
printf("%lld\n", ans);
} return ;
}
p.s.:当时比赛的时候有想到过枚举排列,但是不知道该怎么很快的计算交换次数,所以就没写。 收集信息和组合信息的能力还不够啊……
WHU 1552 Seats 枚举的更多相关文章
- HDU 1937 F - Finding Seats 枚举
F - Finding Seats Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 华中邀请赛现场赛F题 Seats
题目链接:http://acm.whu.edu.cn/land/problem/detail?problem_id=1552 解题报告:题目意思应该很清楚,就是有n个人,分别属于7个班级,然后他们坐成 ...
- hdu1937 Finding Seats
hdu1937 Finding Seats 题意是 求最小的矩形覆盖面积内包含 k 个 空位置 枚举上下边界然后 双端队列 求 最小面积 #include <iostream> #incl ...
- UVA11846-Finding Seats Again(DFS)
Problem UVA11846-Finding Seats Again Accept: 69 Submit: 433Time Limit: 10000 mSec Problem Descrip ...
- [BZOJ 1552] 排序机械臂
Splay大法是坠吼滴! 1552: [Cerc2007]robotic sort Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 436 Solved: ...
- whu Problem 1537 - A - Stones I 贪心
题目链接: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 Stones I Time Limit: 1000MSMemory Li ...
- E. Lost in WHU。矩阵快速幂!
E. Lost in WHU 比赛的时候一直不知道样例怎么来的,然后和队友推了一下,然后还是没什么思路,样例手推很困难,然后我随口枚举了几个算法dp.广搜.快速幂.比赛结束问了谷队长结果真的是用快速幂 ...
- WHU 1537 Stones I
题目见: http://acm.whu.edu.cn/land/problem/detail?problem_id=1537 这个题相当无语,学长给的解法是:枚举取的个数k,然后对每个k贪心,取其中的 ...
- whu 1581 Union of cubes
题目链接: http://acm.whu.edu.cn/land/problem/detail?problem_id=1581 ------------------------------------ ...
随机推荐
- dev c++与VC assist的杂记
最近要处理一些数据,于是把旧本拿出来用用.但是发现旧本运行速度很慢. 分析之后发现是瑞星的老版本程序里面加了游戏的云存储节点的注册表键.果断把该键删了之后,CPU使用率从66%以上降到24%左右. 然 ...
- avalon 双工绑定以及一个按钮多个事件
<html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> ...
- eval-Evaluation
eval is a function which evaluates a string as though it were an expression and returns a result; in ...
- 线程状态与tcb、线程的生命周期
struct tcb { u32_t status; struct reg_context thread_context; void *stack; struct thread_info thread ...
- 解析浏览器和nodejs环境下console.log()的区别
写在前面的 在开发调试过程中,我们经常需要调用console.log 方法来打印出当前变量的值,然而,console.log在浏览器环境下 有时会出现一些异常的现象 开撸代码 在浏览器和nodejs环 ...
- POJ-3436 ACM Computer Factory 最大流 为何拆点
题目链接:https://cn.vjudge.net/problem/POJ-3436 题意 懒得翻,找了个题意. 流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规 ...
- 【mysql】新增、修改、删除、查询 语法讲义
一.DML - 数据操作语言 INSERT - 实现数据表数据的新增 UPDATE - 实现数据表数据的修改 DELETE - 实现数据表数据的删除 二.INSERT 语法: insert into ...
- R语言学习(一)前言
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/49768161 R是一个有着统计分析功能 ...
- HDU 2782 The Worm Turns (DFS)
Winston the Worm just woke up in a fresh rectangular patch of earth. The rectangular patch is divide ...
- leetCode解题报告5道题(七)
题目一:Interleaving String Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2 ...