uva 1444

Description

 

You are in a library equipped with bookracks that move on rails. There are many parallel rails, i.e., the bookracks are organized in several rows, see figure:

The boockracks in the library. There is no passage to the librarian at the moment.

To borrow a book, you have to find the librarian, who seems to hide on the opposite side of the bookracks. Your task then is to move the racks along the rails so that a passage forms. Each rack has a certain integer width, and can be safely positioned at any integer point along the rail. (A rack does not block in a non-integer position and could accidentally move in either direction). The racks in a single row need not be contiguous -- there can be arbitrary (though integer) space between two successive bookracks. A passage is formed at position k if there is no bookrack in the interval (kk + 1) in any row (somehow you don't like the idea of trying to find a more sophisticated passage in this maze.)

The passages formed in the library: at position 8 (the left figure) and at position 9 (the right figure). Both attained at cost 3 by moving the bookracks marked with arrows.

Moving a rack requires a certain amount of efflort on your part: moving it in either direction costs 1. This cost does not depend on the distance of the shift, which can be explained by a well known fact that static friction is considerably higher than kinetic friction. Still, you are here to borrow a book, not to work out, so you would like to form a passage (at any position) with as little efflort as possible.

Input

The input contains several test cases. The first line of the input contains a positive integer Z15, denoting the number of test cases. ThenZ test cases follow, each conforming to the format described below.

Two space separated integers R and L(1R, 1L106) are given in the first line of an input instance. They denote the number of rows and the width of each and every row, respectively. Then R lines with rows descriptions follow. Each such line starts with an integer ni, followed by ni integers ai, 1ai, 2,...ai, ni, all separated by single spaces. Number ai, j denotes either the width of a bookrack when ai, j > 0or a unit of empty space when ai, j = 0. Note that for any row i,ai, j equals L minus the number of ai, j that are equal to zero. You may assume that n1 + n2 + ... nR2*107. Moreover, there will be at least one 0 in the description of each row, which means that creating a passage is always possible.

Output

For each test case, your program has to write an output conforming to the format described below.

In the first line, your program should output the minimum cost of making a passage through the bookracks. In the second line, it should print out the increasing sequence of all the positions at which a minimum cost passage can be formed.

Sample Input

1
4 10
8 1 2 1 0 1 2 0 1
7 2 2 2 1 0 1 0
6 1 3 2 0 2 1
7 2 1 2 0 2 1 0

Sample Output

3
8 9 题意:从上面到下面需要通过很多行,每一行都有书架或者空格,只有空格才能通过,问你最少需要移动多少书架才能达到终点
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e6+;
int g[maxn],can[maxn],zero[maxn],pos[maxn],a[maxn],cost[maxn];
int N,H;
void Init()
{
int n;
cin>>n;
for(int i=;i<n;i++) cin>>a[i];
fill(cost,cost+maxn,-);
int ze=,now=;
for(int i=;i<n;i++)
{
if(a[i]==)
{
zero[ze++]=i;
can[now++]++;
}
else
{
now+=a[i];
int k=min(ze,a[i]);//空格长度与待移动书架长度,最多也只能移动空格位那么长
for(int j=;j<=k;j++)
{
cost[now-j]=i-zero[ze-j]-(j-);//只移j个,计算给(now-j)腾出空位需付代价
g[now-j]+=cost[now-j];//cost只是这一行的代价,总计用g(i)
can[now-j]++;//那么(now-j) 这个位置也可以通行了
}
}
}
reverse(a,a+n); //开始从右往左移
now--;ze=; //ze赋值为零!!!!!代表现在还没有看到可以直接通过的位置
for(int i=;i<n;i++)
{
if(a[i]==)
{
zero[ze++]=i;
now--;
}
else
{
now-=a[i];
int c=min(a[i],ze);
for(int j=;j<=c;j++)
{
if(cost[now+j]==-)
{
cost[now+j]=i-zero[ze-j]-(j-);
g[now+j]+=cost[now+j];
can[now+j]++;
}
else
{
int cc=i-zero[ze-j]-(j-);
g[now+j]+=min(,cc-zero[now+j]);
}
}
}
}
} void Print() //只要满足最小移动数的列举,其余就算可以通过也不要
{
int ans=1e9;
int cnt=;
for(int i=;i<H;i++)
{
if(can[i]==N)
{
if(ans>g[i])
{
ans=g[i];
cnt=;
pos[cnt++]=i;
}
else if(ans==g[i])
pos[cnt++]=i;
}
}
cout<<ans<<endl;
for(int i=;i<cnt;i++) cout<<pos[i]<<" ";
puts("");
} int main()
{
int T;
cin>>T;
while(T--)
{
memset(can,,sizeof(can)); //一定要清零
memset(cost,,sizeof(cost));
memset(pos,,sizeof(pos));
memset(g,,sizeof(g)); //一定要清零
cin>>N>>H;
for(int i=;i<N;i++) Init();
Print();
}
return ;
}

uva 1444 Knowledge for the masses的更多相关文章

  1. UVA 10002 Center of Masses

    题目链接:http://acm.uva.es/local/online_judge/search_uva.html Problem:Find out the center of masses of a ...

  2. UVA 1456 六 Cellular Network

    Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit S ...

  3. UVA 322 ships (POJ 1138)

    题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

随机推荐

  1. ZOJ 3955 Saddle Point 校赛 一道计数题

    ZOJ3955 题意是这样的 给定一个n*m的整数矩阵 n和m均小于1000 对这个矩阵删去任意行和列后剩余一个矩阵为M{x1,x2,,,,xm;y1,y2,,,,,yn}表示删除任意的M行N列 对于 ...

  2. 转贴:获取元素CSS值之getComputedStyle方法熟悉

    获取元素CSS值之getComputedStyle方法熟悉 一.碎碎念~前言 我们都用过jQuery的CSS()方法,其底层运作就应用了getComputedStyle以及getPropertyVal ...

  3. Objective-C 对象的类型与动态结合

    创建: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C] 完成: 2018/01/24 更新: 2018/01/24 加红加粗属性方法的声明 [不直接获取内部 ...

  4. bzoj 3743: [Coci2015]Kamp【树形dp】

    两遍dfs一遍向下,一边向上,分别记录子树内人数a,当前点到所有点的距离b,最大值c和次大值d,最大值子树p 然后答案是2b-c #include<iostream> #include&l ...

  5. RabbitMq安装成功后执行命令报错(Error: unable to connect to node 'rabbit@DESKTOP-LPKSION': nodedown)

    我们直接来看解决方案吧.首先打开服务,找到RabbitMq服务. 双击打开后选择登陆选项卡: 点选此账户,输入你计算机的登录名称.点击浏览: 在这里输入你的用户名,点检索: 这里的密码输入你电脑开机登 ...

  6. 【js】为什么要使用react+redux

    前端的浪潮一叠叠袭来,带走了jQuery,带走了backbone,带来了react,带来了redux,但是面对层出不穷的前端技术,我们应该何去何从呢?近一年来笔者的也发生了同样的变化,技术栈从.net ...

  7. 数据传递-------ajaxJson------spring3mvc中使用ajax传json中文乱码解决

    参考来源:http://blog.csdn.net/dangerous_fire/article/details/25904225 第一种解决方法,适用所有情况 因为在controller中返回jso ...

  8. poj1778 All Discs Considered

    思路: 拓扑排序.贪心. 实现: #include <bits/stdc++.h> using namespace std; vector<]; int n1, n2; inline ...

  9. Python学习日记之快捷键

    Alt+Enter 自动添加包Ctrl+t SVN更新Ctrl+k SVN提交Ctrl + / 注释(取消注释)选择的行Ctrl+Shift+F 高级查找Ctrl+Enter 补全Shift + En ...

  10. Chromium浏览器编译成功庆祝

     1.什么是Chromium     Chromium 是Google公司的开源项目     Google浏览器  最新版360浏览器 都是在Chromium的基础上重新编译的. 2.什么是双核浏览器 ...