G. Shuffling Songs
G. Shuffling Songs
Vladislav has a playlist consisting of songs, numbered from to . Song has genre and writer . He wants to make a playlist in such a way that every pair of adjacent songs either have the same writer or are from the same genre (or both). He calls such a playlist exciting. Both and are strings of length no more than .
It might not always be possible to make an exciting playlist using all the songs, so the shuffling process occurs in two steps. First, some amount (possibly zero) of the songs are removed, and then the remaining songs in the playlist are rearranged to make it exciting.
Since Vladislav doesn't like when songs get removed from his playlist, he wants the making playlist to perform as few removals as possible. Help him find the minimum number of removals that need to be performed in order to be able to rearrange the rest of the songs to make the playlist exciting.
Input
The first line of the input contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains a single integer () — the number of songs in the original playlist.
Then lines follow, the -th of which contains two strings of lowercase letters and () — the genre and the writer of the -th song. Where and are lengths of the strings.
The sum of over all test cases does not exceed .
The sum of over all test cases does not exceed .
Output
For each test case, output a single integer — the minimum number of removals necessary so that the resulting playlist can be made exciting.
Example
input
4
1
pop taylorswift
4
electronic themotans
electronic carlasdreams
pop themotans
pop irinarimes
7
rap eminem
rap drdre
rap kanyewest
pop taylorswift
indierock arcticmonkeys
indierock arcticmonkeys
punkrock theoffspring
4
a b
c d
e f
g h
output
0
0
4
3
Note
In the first test case, the playlist is already exciting.
In the second test case, if you have the songs in the order , it is exciting, so you don't need to remove any songs.
In the third test case, you can remove songs . Then the playlist with songs in the order is exciting.
解题思路
因为前段时间有事好久没有写博客了,也没有怎么写题。挑个上场的 Div. 4 最后一题水水()
数据范围的提示 "The sum of over all test cases does not exceed ." 就直接把做法贴脸上了,挺乐的()
先定义符号 和 分别表示第 首音乐的作者和流派。 表示第 首音乐能否与第 首音乐相邻,直接暴力预处理即可。定义状态 表示由二进制集合 中的音乐(即所有是 位)构成的所有合法序列中(且最后一首音乐为 ),代价的最小值。其中代价指集合中要删除的元素数量。当然还有一个前提是 的二进制下的第 位要为 。
考虑状态转移,如果第 首音乐不接在任何一首音乐的后面,那么代价就是 ,其中 表示 在二进制下为 的位数。如果要接在第 首音乐后面,有两个条件要满足: 的第 位是 且 。代价就是 。因此状态转移方程就是
AC代码如下,时间复杂度为 :
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 16, M = 1 << 16, INF = 0x3f3f3f3f;
string a[N], b[N];
int g[N][N];
int f[M][N];
void solve() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] == a[j] || b[i] == b[j]) g[i][j] = g[j][i] = 1;
else g[i][j] = g[j][i] = 0;
}
}
for (int i = 1; i < 1 << n; i++) {
for (int j = 0; j < n; j++) {
if (i >> j & 1) {
f[i][j] = __builtin_popcount(i) - 1;
for (int k = 0; k < n; k++) {
if (i >> k & 1 && g[j][k]) f[i][j] = min(f[i][j], f[i ^ 1 << j][k]);
}
}
else {
f[i][j] = INF;
}
}
}
int ret = n;
for (int i = 0; i < n; i++) {
ret = min(ret, f[(1 << n) - 1][i]);
}
cout << ret << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
参考资料
Codeforces Round 937 (Div. 4) Editorial:https://codeforces.com/blog/entry/127664
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18109645
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-04-01 机器人移动
2022-04-01 序列重排
2021-04-01 在堆区申请二维数组的方法