2017 JUST Programming Contest 2.0

题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2

J. Husam and the Broken Present 2
time limit per test:1.0 s
memory limit per test:256 MB
input:standard input
output:standard output

After you helped Husam and rebuilt his beautiful array a he became very happy. To avoid losing his array again, Husam made n copies from it, and distributed it to n of his friends. After that Husam became sure that he can rebuild the array a again if he lost it, so he destroyed the table t.

Today, Husam was looking for his array a, but he was not able to find it. Husam visited all his n friends to take a copy from the array. Unfortunately, all his friends thought that the length of the array a was very long, so instead of keeping the array itself, each friend i take a subarray (li, ri) from the array and kept it in a safe place, and get rid of the rest of the array.

Now Husam has n subarrays from the array a, but he cannot remember the original array or even its length. Husam now needs your help again, he will give you the n subarrays, and your task is to build a new array a such that it contains all the given subarrays inside it as subarrays, and its length must be as minimal as possible. Can you?

Input

The first line contains an integer n (1 ≤ n ≤ 15), where n is the number of friends Husam has.

Then n lines follow, each line i begins with an integer mi (1 ≤ mi ≤ 100), where mi is the length of the subarray the ith friend has. Then mi integers follow, representing the ith subarray. All values x in the subarrays are in the range (1 ≤ x ≤ 109).

Output

Print the minimal length of the new array a, such that a contains all the given subarrays in the input inside it as subarrays.

Examples
Input
3
2 1 2
4 3 4 5 6
3 2 3 4
Output
6
Input
5
3 4 7 5
4 7 9 2 5
3 7 5 2
4 5 1 4 7
4 9 2 5 1
Output
9
Note

A subarray of the array a is a sequence al, al + 1, ..., ar for some integers (l, r) such that (1 ≤ l ≤ r ≤ n).

In the first test case the array a can be [1, 2, 3, 4, 5, 6]. Its length is as minimal as possible, and it contains all the the given subarrays in the input inside it as subarrays.

题意:构造一个序列包含所给的N个子序列(N≤15),求构造序列的最短长度。

题解:

考虑状态压缩,有2^N种状态,设dp[i][j]表示状态为i,以第j个子序列结尾的最小长度;

状态转移:从以第j个子序列结尾的状态转移到以第k个子序列的状态:

  dp[i|(1<<(k-1))][k]=min{dp[i][j]+a[k][0]-num[j][k]}   (其中,a[k][0]表示第k个子序列的长度,num[j][k]表示第j个子序列的后缀与第k个子序列的前缀重合部分的长度)

注意:先将被其他子序列包含的子序列删去......

【感觉自己现在一敲题就各种手残情况......】

代码:

 #include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int a[][];
bool vis[]; //vis[i]:第i个串是否被其他串包含
int num[][];
int dp[(<<)][];//dp[i][j]:状态为i,以第j个子序列结尾的最小长度
int main() {
int n, i, j, k, o, sum = , t, ans = 1e9;
scanf("%d", &n);
for(i = ; i <= n; ++i) {
scanf("%d", &a[i][]);
for(j = ; j <= a[i][]; ++j) scanf("%d", &a[i][j]);
}
for(i = ; i <= n; ++i) if(!vis[i]){//删去被其他子序列包含的子序列
for(j = ; j <= n; ++j) {
if(vis[j] || i == j || a[j][] > a[i][]) continue; for(k = ; k <= a[i][]-a[j][]+; ++k) {
for(o = ; o <= a[j][]; ++o)
if(a[i][k+o-] != a[j][o]) break;
if(o == a[j][]+) {vis[j] = true; break;}
}
}
}
for(i = ; i <= n; ++i) if(!vis[i]) {
for(sum++,j=; j<=a[i][]; ++j) a[sum][j] = a[i][j];
}
n = sum;
for(i = ; i <= n; ++i) {//计算num[i][j]
for(j = ; j <= n; ++j) {
if(i == j) continue;
for(k = ; k <= a[i][]; ++k) {//枚举重合长度
for(t=, o=a[i][]-k+; o <= a[i][]; ++o)
if(a[i][o] != a[j][t++]) break;
if(o == a[i][]+) num[i][j] = k;
}
}
}
sum = (<<n)-; //状态总数
memset(dp, inf, sizeof(dp));
for(i = ; i <= n; ++i) dp[<<(i-)][i] = a[i][];
for(i = ; i <= sum; ++i) {//计算dp[i][j]
for(j = ; j <= n; ++j) {
if(!((<<(j-))&i)) continue;
for(k = ; k <= n; ++k) {
if((<<(k-))&i) continue;
dp[i|(<<(k-))][k]=min(dp[i|(<<(k-))][k],dp[i][j]+a[k][]-num[j][k]);
}
}
}
for(i = ; i <= n; ++i) ans = min(ans, dp[sum][i]);
printf("%d\n", ans);
return ;
}

31ms

Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】的更多相关文章

  1. gym101343J. Husam and the Broken Present 2 (状压DP)

    题意:给定n个串 每个串长度不超过100 找到一个新串 使得这n个串都是它的字串 输出这个新串的最小长度 题解:n是15 n的阶乘的复杂度肯定不行 就想到了2的15次方的复杂度 想到了状压但是不知道怎 ...

  2. Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)

    http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...

  3. Codeforces Gym 191033 E. Explosion Exploit (记忆化搜索+状压)

    E. Explosion Exploit time limit per test 2.0 s memory limit per test 256 MB input standard input out ...

  4. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  5. Codeforces Gym 100015F Fighting for Triangles 状压DP

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  6. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  7. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  8. 状压dp Gym - 100676G

    http://codeforces.com/gym/100676 题目大意: 给你n个科目,m个关系,例如A->B,表示要学习B科目,一定要把A科目学习掉.同理,如果还有C->B,那么,B ...

  9. 状压dp Codeforces Beta Round #8 C

    http://codeforces.com/contest/8/problem/C 题目大意:给你一个坐标系,给你一个人的目前的坐标(该坐标也是垃圾桶的坐标),再给你n个垃圾的坐标,这个人要捡完所有的 ...

随机推荐

  1. css3 transition(转换)笔记

    之前transition也用过,大都是ctrl+c,然后ctrl+v,没有了解太详细,这次对transition的应用源自侧边抽屉展开收起的动画效果需要. W3C标准中对css3的transition ...

  2. mysql 优化海量数据插入和查询性能

    对于一些数据量较大的系统,数据库面临的问题除了查询效率低下,还有就是数据入库时间长.特别像报表系统,每天花费在数据导入上的时间可能会长达几个小时或十几个小时之久.因此,优化数据库插入性能是很有意义的. ...

  3. 区分IE8/IE7/IE6及其他浏览器-CSS “\9″

    区分IE8/IE7/IE6及其他浏览器-CSS “\9″ 原创文章,转载请注明来自张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] by zhangxinxu from h ...

  4. 阿里云服务器被他人通过SSH暴力破解后的安全加固

    背景说明:我登录阿里云服务器控制台时,收到几条安全警告信息. 从图中可以知道,对方的IP地址是47.97.68.118,通过SSH登录方式,登录时用我服务器里的admin用户,然后用穷举法暴力破解ad ...

  5. EOS下控制台以及图形界面打印sql语句

    EOS下控制台以及图形界面打印sql语句 场景需求:在eos中打印sql语句,包括数据实体,查询实体和命名sql的sql语句. 所需资源: P6spy:负责拦截sql,并打印. Sqlprofiler ...

  6. 使用MaxCompute访问TableStore(OTS) 简明手册

    摘要: 大数据计算服务 MaxCompute 能够提供强大的分析能力,而分布式 NoSQL 数据库表格存储在行级别上的实时更新和可覆盖性写入等特性,相对于 MaxCompute 内置表 append- ...

  7. 重装系统,打开VS进行程序调试运行的时候 Unable to find manifest signing certificate in the certificate store

    重装系统,打开VS进行程序调试运行的时候 Unable to find manifest signing certificate in the certificate store. 项目的属性-> ...

  8. Vue 框架-10-搭建脚手架 CLI

    Vue 框架-10-搭建脚手架 CLI + 批处理快捷启动 脚手架是通过 webpack 搭建的开发环境 使用 ES6 语法 打包和压缩 JS 为一个文件 项目文件在环境中,而不是浏览器 实现页面自动 ...

  9. linux 源码包之脚本安装包的安装

    脚本安装包 脚本安装包并不是独立的软件包类型,常见的实际是源码包.是人为地把安装过程写成了自动安装脚本,只要执行脚本,定义简单的参数,就可以完成安装.非常类似于windows软件的安装方式.在linu ...

  10. CCSUOJ评测系统

    队名: BUGG 团队信息与分工: 开发: 周斌 B20150304221 舒 溢 B20150304209 测试: 许嘉荣 B20150304213 唐 浩 B20150304316 Product ...