UVA 1444 - Knowledge for the masses

UVA 1444 - Knowledge for the masses

题目链接

题意:给定R排书架,现在要求最小代价移动书架打开一条通道,每次移动书架的代价为1(距离不限),问最小代价和最小代价的位置

思路:对于每一行,从左往右再从右往左各推一遍,每次把各个位置代价的最小值算出来,计算的过程要记录每个位置对应前面空位个数和空位位置,这样每个书架要移动的代价就能快速算出,最后处理往后,在每个位置遍历找出最小值位置即可

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1000005;

int t, R, L, n, blo[N], zero[N], value[N], can[N], g[N];

void build() {
    scanf("%d", &n);
    int zn = 0, now = 0;
    memset(value, -1, sizeof(value));
    for (int i = 0; i < n; i++) {
	scanf("%d", &blo[i]);
	if (blo[i] == 0) {
	    zero[zn++] = i;
	    can[now]++;
	    now++;
	}
	else {
	    now += blo[i];
	    int k = min(zn, blo[i]);
	    for (int j = 1; j <= k; j++) {
		value[now - j] = i - zero[zn - j] - j + 1;
		can[now - j]++;
		g[now - j] += value[now - j];
	    }
	}
    }
    reverse(blo, blo + n);
    zn = 0; now--;
    for (int i = 0; i < n; i++) {
	if (blo[i] == 0) {
	    zero[zn++] = i;
	    now--;
	}
	else {
	    now -= blo[i];
	    int k = min(zn, blo[i]);
	    for (int j = 1; j <= k; j++) {
		if (value[now + j] == -1) {
		    value[now + j] = i - zero[zn - j] - j + 1;
		    g[now + j] += value[now + j];
		    can[now + j]++;
		}
		else {
		    int tmp = i - zero[zn - j] - j + 1;
		    g[now + j] += min(0, tmp - value[now + j]);
		}
	    }
	}
    }
}

void solve() {
    int an = 0, Min = INF;
    for (int i = 0; i < L; i++) {
	if (can[i] != R) continue;
	if (Min > g[i]) {
	    Min = g[i];
	    an = 0;
	    value[an++] = i;
	}
	else if (Min == g[i]) {
	    value[an++] = i;
	}
    }
    printf("%d\n", Min);
    for (int i = 0; i < an; i++)
	printf("%d ", value[i]);
    printf("\n");
}

int main() {
    scanf("%d", &t);
    while (t--) {
	scanf("%d%d", &R, &L);
	memset(can, 0, sizeof(can));
	memset(g, 0, sizeof(g));
	for (int i = 0; i < R; i++)
	    build();
	solve();
    }
    return 0;
}

UVA 1444 - Knowledge for the masses,布布扣,bubuko.com

时间: 2024-10-21 04:37:18

UVA 1444 - Knowledge for the masses的相关文章

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

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

UVALive - 4629 Knowledge for the masses 高效

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

UVA 322 ships (POJ 1138)

题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=258 http://poj.org/problem?id=1138 题目描写叙述:  Ships  Probably everyone who ever attended school knows the game where two opposing players place

UVA 1456 六 Cellular Network

Cellular Network Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1456 A cellular network is a radio network made up of a number of cells each served by a base station located in the cell. The base sta

UVA 10158 War(并查集)

War A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country's espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including y

题解 $UVA$ 11825【$Hackers$&#39; $Crackdown$】

本题的数学模型是:把\(\mathcal{n}\)个集合\(\mathcal{P1,P2,...,Pn}\)分成尽量多组,使得每组中所以集合的并集等于全集.这里集合\(\mathcal{Pi}\)就是计算机\(\mathcal{i}\)及其相邻计算机的集合,每组对应于题目中的一项服务. 注意到\(\mathcal{n}\)很小,可以套用<算法竞赛>里面提到的二进制法表示这些集合,即在代码中, 每个集合\(\mathcal{Pi}\)实际上是一个非负整数.输入部分代码如下: for(int i=

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te