Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

 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个子序列的前缀重合部分的长度)

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

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

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf = 0x3f3f3f3f;
 4 int a[16][101];
 5 bool vis[16];       //vis[i]:第i个串是否被其他串包含
 6 int num[16][16];
 7 int dp[(1<<16)][16];//dp[i][j]:状态为i,以第j个子序列结尾的最小长度
 8 int main() {
 9     int n, i, j, k, o, sum = 0, t, ans = 1e9;
10     scanf("%d", &n);
11     for(i = 1; i <= n; ++i) {
12         scanf("%d", &a[i][0]);
13         for(j = 1; j <= a[i][0]; ++j) scanf("%d", &a[i][j]);
14     }
15     for(i = 1; i <= n; ++i) if(!vis[i]){//删去被其他子序列包含的子序列
16         for(j = 1; j <= n; ++j) {
17             if(vis[j] || i == j || a[j][0] > a[i][0]) continue;
18 
19             for(k = 1; k <= a[i][0]-a[j][0]+1; ++k) {
20                 for(o = 1; o <= a[j][0]; ++o)
21                     if(a[i][k+o-1] != a[j][o]) break;
22                 if(o == a[j][0]+1) {vis[j] = true; break;}
23             }
24         }
25     }
26     for(i = 1; i <= n; ++i) if(!vis[i]) {
27         for(sum++,j=0; j<=a[i][0]; ++j) a[sum][j] = a[i][j];
28     }
29     n = sum;
30     for(i = 1; i <= n; ++i) {//计算num[i][j]
31         for(j = 1; j <= n; ++j) {
32             if(i == j) continue;
33             for(k = 1; k <= a[i][0]; ++k) {//枚举重合长度
34                 for(t=1, o=a[i][0]-k+1; o <= a[i][0]; ++o)
35                     if(a[i][o] != a[j][t++]) break;
36                 if(o == a[i][0]+1) num[i][j] = k;
37             }
38         }
39     }
40     sum = (1<<n)-1; //状态总数
41     memset(dp, inf, sizeof(dp));
42     for(i = 1; i <= n; ++i) dp[1<<(i-1)][i] = a[i][0];
43     for(i = 1; i <= sum; ++i) {//计算dp[i][j]
44         for(j = 1; j <= n; ++j) {
45             if(!((1<<(j-1))&i)) continue;
46             for(k = 1; k <= n; ++k) {
47                 if((1<<(k-1))&i) continue;
48                 dp[i|(1<<(k-1))][k]=min(dp[i|(1<<(k-1))][k],dp[i][j]+a[k][0]-num[j][k]);
49             }
50         }
51     }
52     for(i = 1; i <= n; ++i) ans = min(ans, dp[sum][i]);
53     printf("%d\n", ans);
54     return 0;
55 }
31ms

 

 

 

posted @ 2018-05-05 23:01  GraceSkyer  阅读(599)  评论(0编辑  收藏  举报

~~~~~~ACM大牛语录,激励一下~~~~~~

为了世界的和平,为了女生的安全,我拼命做题,做题,做题!

用最短的时间,刷最多的题!

给我一滴泪,我就看到了你全部的海洋!

seize the hour, seize the day.

人生难免有无奈,幸福走远了,或是感叹幸福来迟了.其实我一直相信,无论手中的幸福是多么微不足道的感觉,我会把握住那每一分,每一秒,当幸福依旧像那百鸟般飞逝,终究无法掌握时,我会感谢它,曾经降临过!

A自己的题,让别人郁闷去吧

WA肠中过,AC心中留 TLE耳边过,AC特别牛

天然的悲苦和伤逝,过去有过,以后还会有

^*^一步一步往上爬^*^

AC就像练级,比赛就像PK. 练级不如PK好玩

其实,世上本没有ACM,AC的人多了,也便有了!

AC无止尽~ Seek you forever~

找呀找呀找水题,找到一个AC一个呀!

AC是检验程序的唯一标准。

真的猛士,敢于直面惨淡的人生,敢于正视淋漓的鲜血……