B-knowledge for the masses

Source: Internet
Author: User

UVA 1444

Description

You is in a library equipped with Bookracks this move on rails. There is many parallel rails, i.e., the bookracks is 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 has to find the librarian, who seems to hide on the opposite side of the bookracks. Your task then was to move the racks along, the rails so, a passage forms. Each rack have a certain integer width, and can be safely positioned on any integer point along the rail. (A Rack does not block with A Non-integer position and could accidentally move in either direction). The racks in a single row need not being contiguous--there can be arbitrary (though integer) space between double successive b Ookracks. A Passage is formed at position K If there are no bookrack in the interval (k, K + 1) in Any row (somehow you don't like the idea of trying to find a further 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 is explained by a well known fact that static friction I s considerably higher than kinetic friction. Still, you is here-borrow a book, the not-to-work-out, so-would like-to-form a passage (at any position) with as Littl e Efflort as possible.

InputThe input contains several test cases. The first line of the input contains a positive integer Z, denoting the number of test cases. Then Z -Test cases follow, each conforming to the format described below.


Both space separated integersRandL(1R, 1L106) is given in the first line of an input instance. They denote the number of rows and the width of each and every row, respectively. ThenRLines with rows descriptions follow. Each such line starts with an integer nI, followed by ni integers ai, 1, ai, 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 is equal to zero. Assume that n1 + n2 + ... + nr2*107. Moreover, there'll be on least one 0 in the description of each row, which means that creating a passage are always Possi ble.

Outputfor each test case, the your program have to write a 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 B E formed.

Sample Input

1 4 10 8 1 2 1 0 1 2 0 17 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

Test instructions: From the top to the following through a lot of lines, each line has a bookshelf or a space, only the space to pass, ask you at least how many bookshelves need to move to reach the end

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1e6+Ten;intG[MAXN],CAN[MAXN],ZERO[MAXN],POS[MAXN],A[MAXN],COST[MAXN];intn,h;voidInit () {intN; CIN>>N;  for(intI=0; i<n;i++) cin>>A[i]; Fill (cost,cost+maxn,-1); intZe=0, now=0;  for(intI=0; i<n;i++)   {       if(a[i]==0) {Zero[ze++]=i; Can[now++]++; }       Else{ Now+=A[i]; intK=min (Ze,a[i]);//The length of the space and the length of the bookshelf to be moved, at most can only move space so long            for(intj=1; j<=k;j++) {Cost[now-j]=i-zero[ze-j]-(J-1);//move only J, calculate to (now-j) free space to pay a priceG[NOW-J]+=COST[NOW-J];//cost is only the price of this line, total with G (i)can[now-j]++;//then (NOW-J) This position is passable.}}} reverse (A,a+N);//start moving from right to leftNow--;ze=0;//The ze is assigned a value of zero!!!!! The rep has not yet seen a place to pass directly.    for(intI=0; i<n;i++)   {       if(a[i]==0) {Zero[ze++]=i; now--; }       Else{ Now-=A[i]; intC=min (A[i],ze);  for(intj=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               {                   intcc=i-zero[ze-j]-(J-1); G[now+j]+=min (0, cc-zero[now+J]); }           }       }   }}voidPrint ()//as long as the minimum number of moves are enumerated, the rest can be passed and not{    intans=1e9; intCnt=0;  for(intI=0; 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(intI=0; i<cnt;i++) cout<<pos[i]<<" "; Puts ("");}intMain () {intT; CIN>>T;  while(t--) {memset (CAN,0,sizeof(can));//be sure to clear 0 .memset (Cost,0,sizeof(cost)); memset (POS,0,sizeof(POS)); Memset (g,0,sizeof(g));//be sure to clear 0 .Cin>>n>>i;  for(intI=0; i<n;i++) Init ();    Print (); }    return 0;}
View Code

B-knowledge for the masses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.