B - Knowledge for the masses

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+10;
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=0;i<n;i++) cin>>a[i];
   fill(cost,cost+maxn,-1);
   int ze=0,now=0;
   for(int i=0;i<n;i++)
   {
       if(a[i]==0)
       {
           zero[ze++]=i;
           can[now++]++;
       }
       else
       {
           now+=a[i];
           int k=min(ze,a[i]);//空格长度与待移动书架长度,最多也只能移动空格位那么长
           for(int j=1;j<=k;j++)
           {
               cost[now-j]=i-zero[ze-j]-(j-1);//只移j个,计算给(now-j)腾出空位需付代价
               g[now-j]+=cost[now-j];//cost只是这一行的代价,总计用g(i)
               can[now-j]++;//那么(now-j) 这个位置也可以通行了
           }
       }
   }
   reverse(a,a+n);   //开始从右往左移
   now--;ze=0;        //ze赋值为零!!!!!代表现在还没有看到可以直接通过的位置
   for(int i=0;i<n;i++)
   {
       if(a[i]==0)
       {
           zero[ze++]=i;
           now--;
       }
       else
       {
           now-=a[i];
           int c=min(a[i],ze);
           for(int j=1;j<=c;j++)
           {
               if(cost[now+j]==-1)
               {
                   cost[now+j]=i-zero[ze-j]-(j-1);
                   g[now+j]+=cost[now+j];
                   can[now+j]++;
               }
               else
               {
                   int cc=i-zero[ze-j]-(j-1);
                   g[now+j]+=min(0,cc-zero[now+j]);
               }
           }
       }
   }
}

void Print()       //只要满足最小移动数的列举,其余就算可以通过也不要
{
    int ans=1e9;
    int cnt=0;
    for(int i=0;i<H;i++)
    {
        if(can[i]==N)
        {
            if(ans>g[i])
            {
                ans=g[i];
                cnt=0;
                pos[cnt++]=i;
            }
            else if(ans==g[i])
            pos[cnt++]=i;
        }
    }
    cout<<ans<<endl;
    for(int i=0;i<cnt;i++) cout<<pos[i]<<" ";
    puts("");
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
     memset(can,0,sizeof(can));   //一定要清零
     memset(cost,0,sizeof(cost));
     memset(pos,0,sizeof(pos));
     memset(g,0,sizeof(g));       //一定要清零
     cin>>N>>H;
     for(int i=0;i<N;i++) Init();
     Print();
    }
    return 0;
}

时间: 2024-08-24 21:46:53

B - Knowledge for the masses的相关文章

UVA 1444 - Knowledge for the masses

UVA 1444 - Knowledge for the masses 题目链接 题意:给定R排书架,现在要求最小代价移动书架打开一条通道,每次移动书架的代价为1(距离不限),问最小代价和最小代价的位置 思路:对于每一行,从左往右再从右往左各推一遍,每次把各个位置代价的最小值算出来,计算的过程要记录每个位置对应前面空位个数和空位位置,这样每个书架要移动的代价就能快速算出,最后处理往后,在每个位置遍历找出最小值位置即可 代码: #include <cstdio> #include <cst

uva 1444 - Knowledge for the masses(高效)

题目链接:uva 1444 - Knowledge for the masses 题目大意:给出R和L,R表示有R行,L表示一行的最大长度. 对于每一行,给出n,然后是n个数,arr[i]为0表示空格,长度为1,否则表示书架,长度为arr[i].现在人要从上边走到下边,问说最少移动几个书架,并且输出可以通过的路径坐标. 解题思路:c[i]表示第i个坐标有多少行可以通过,当c[i]==R时,表示人可以从该位置穿过整个书架.g[i]表示从i这个位置穿过的代价.然后对于每一行处理,pos[i]记录第i

UVALive - 4629 Knowledge for the masses 高效

题目大意:有一个人要找管理员,但前进的路上有很多的障碍 移动一个障碍到相应行的空位置需要花费1点体力,距离不限 问花费的最少体力值是多少,能前进的路是哪几条 解题思路:参考了学长的代码 求出每条前进道路的需要花费的最小体力值,再进行比较就可以 难点刚好就是这个了 vis[i]代表将该行的i列位置扫清需要消耗的体力值,flag[i]表示可以将第i列的几个位置扫清,如果i == R,就表示该列可以前行了,zero[i]表示该行第i个0所在的位置,cost[i]表示清每行第i列障碍的体力和 #incl

The influence of informal governance mechanisms on knowledge integration

Title:The influence of informal governance mechanisms on knowledge integration within cross-functional project teams: A social capital perspective Journal:KNOWLEDGE MANAGEMENT RESEARCH & PRACTICE ABSTRACT :This paper aims to explore the influence of

(paper reading)Entity Linking with a Knowledge Base: Issues, Techniques, and Solutions

给定一个包含一系列实体E的知识库,以及提到了M个已确定实体的文本集合,实体链接的目的是将文本中提到的每个实体m∈M链接到知识库中对应的实体e∈E上.如果文本中提到的实体在知识库中没有对应,则被称为unlinkable mentions,对这样的一类实体,一个实体链接系统会给它加上一个特殊的标签NIL. 一个典型的实体链接系统应该包含三个模块: Candidate entity generation 对M当中的每一个m,实体链接系统需要在知识库中找出候选的实体集合Em,主要的实现方法有: dict

Knowledge Generation Model for Visual Analytics 第二部分

2.2探索循环(Exploration Loop) 探索循环描述分析师同一个可视化分析系统进行一系列互动行为(Action),如数据准备.建立模型.操控可视化结果等,观察和探索由此产生的反馈,并获得发现(Finding).分析师的行为应当遵循分析的目标而展开. 2.2.1行为(Action) Actions可能考虑一方面用户不同的目标和任务,另一方面交互的可视化.根据最近交互分类. 准备工作行为,处理数据采集和数据选择 建模行为,创造模型的行为,跟KDD过程以及配置相关 模型的应用称作 mode

Knowledge Generation Model for Visual Analytics

1 摘要 可视分析使得人们能够分析大量的信息,为了支持复杂的决策和数据探索.人类作为一个中心的角色在知识产生的过程,从片段的证明到可视数据分析.虽然前者的研究提供了框架来产生这些过程,他们的范围通常聚焦很窄,所以他们不包含不同等级的不同视角.本文提供一个知识产生的可视分析模型,将这些分离的框架结合到一起,但是,仍然保留以前先进的模型(例如.KDD过程)来描述整个可视分析流程的个体片段.为了测试这个模型的有效性,用一个现实世界的可视分析系统来比较这个模型,证明当开发和评估这个系统,这个支持产生流程

前端技能汇总 Frontend Knowledge Structure

Frontend Knowledge Structure 项目起源 还记得@jayli 的这幅前端知识结构图么. 图片的形式具有诸多的不便.缺失源图的我们,无法为此图贡献些什么,随着时间的迁移,或许有些技术点会发生改变,所以有了这个GitHub项目.我们可以通过协作的方式来共同维护这个项目.Git的历史记录也可以见证前端行业的一些变迁. 尽管会变成文字的方式来维护这些内容,但是我承诺写一个小工具帮大家生成更好玩的图形(基于DataV项目). 前端开发知识结构 前端工程师 浏览器 IE6/7/8/

business knowledge

Finance knowledge Trading---At the core of our business model is Trading, which involves the buying and selling of financial tools to generate profit. Trading takes place in our Global Markets division, which spans collateralised financing, commoditi