动态规划(2)

合集下载

2024咨询工程师:城市轨道交通线网规划答案

2024咨询工程师:城市轨道交通线网规划答案

一、判断题(每题2 分,共38 题,总分76 分)1、到2030年,健全国土空间规划法规政策和技术标准体系;全面实施国土空间检测预警和绩效考核机制;形成以国土空间规划为基础,以统一用途管制为手段的国土空间开发保护制度。

()A、对B、错正确2、每座主变电所建设控制区宜为3000~4000平方米,长度宜为60~70米,宽度宜为50~60米。

A、对B、错正确3、公元前一世纪,古罗马建筑师维特鲁威的《按照艺术原则进行城市设计》是西方古代保留至今唯一最完整的古典建筑典籍,第一卷和第五卷提出了不少关于城市规划、建筑工程等方面的论述。

()A、对B、错正确4、地铁A、B、C三种车型,主要差异在车辆尺寸、定员和轴重三个方面。

A、对B、错正确5、合格的线网规划人员必须具备运营的基本知识。

()A、对B、错正确6、单轨系统分为跨坐式单轨系统和悬挂式单轨系统。

A、对B、错正确7、车站站台可以分为岛站台和侧站台两种,从客流的适用功能来看,侧站台更优于岛站台。

()A、对B、错正确8、四阶段法是目前国内城市交通需求预测中最普遍采用的预测方法。

A、对B、错正确9、城市规划是为了实现一定时期内城市的经济和社会发展目标,确定城市性质、规模和发展方向,合理利用城市土地,协调城市空间布局和各项建设所作的综合部署和具体安排。

()A、对B、错正确10、资源共享必须以满足轨道交通本身的功能为前提,不能过分强调资源共享而忽视功能本身。

A、对B、错正确11、中心城区轨道交通线网层次的重点是速度的级配组合,市域轨道交通网络层次的重点是运量和速度的级配组合。

A、对B、错正确12、城市交通方式结构是指城市各种交通出行方式的客运量占城市总客运量的比例。

A、对B、错正确13、出行分布一般采用重力模型进行预测。

()A、对B、错正确14、环线的作用之一是联络轨道交通平行线或放射线,通过客流转换,提高轨道交通网络可达性,一般网络中有了环线换乘系数会降低。

A、对B、错正确15、换乘站客流特征具有高集中性、多方向性、短时冲击性等。

《信息学奥赛一本通》:第9章 第2节 动态规划背包问题(C++版)

《信息学奥赛一本通》:第9章  第2节 动态规划背包问题(C++版)
f[n][m]即为最优解。
【参考程序】
#include<cstdio> using namespace std;
const int maxm = 201, maxn = 31;
int m, n;
int w[maxn], c[maxn];
int f[maxn][maxm];
int main()
{
scanf("%d%d",&m, &n);
for (int i=1; i <= n; i++)
//设f(v)表示重量不超过v公斤的最大价值
for (int v = m; v >= w[i]; v--)
if (f[v-w[i]]+c[i]>f[v])
f[v] = f[v-w[i]]+c[i];
printf("%d",f[m]);
// f(m)为最优解
【例9-12】、完全背包问题 【问题描述】
设有n种物品,每种物品有一个重量及一个价值。但每种物品的数量是无限 的,同时有一个背包,最大载重量为M,今从n种物品中选取若干件(同一种物品 可以多次选取),使其重量的和小于等于M,而价值的和为最大。
【输入格式】
第一行:两个整数,M(背包容量,M<=200)和N(物品数量,N<=30); 第2..N+1行:每行二个整数Wi,Ci,表示每个物品的重量和价值。 【输出格式】
第九章 动态规划
第二节 背包问题
第二节 背包问题
一、01背包问题 问题:
有N件物品和一个容量为V的背包。第i件物品的费用(即体积,下同)是w[i], 价值是c[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量, 且价值总和最大。 基本思路:

动态规划最优子结构和状态转移方程

动态规划最优子结构和状态转移方程

动态规划最优子结构和状态转移方程动态规划(Dynamic Programming)是一种通过将问题分解为子问题的方式来求解复杂问题的方法。

其核心思想是将问题划分为相互重叠的子问题,并对子问题的解进行存储,避免重复计算,从而提高效率。

在动态规划中,最优子结构和状态转移方程是两个重要的概念。

最优子结构指的是问题的最优解可以通过子问题的最优解来构造。

也就是说,一个问题的最优解包含了其子问题的最优解,并且这些子问题之间相互独立。

通过找到最优子结构,我们可以将原问题分解为子问题,并利用子问题的最优解来构造整体的最优解。

状态转移方程(State Transition Equation)描述了问题求解的递推关系。

在动态规划中,我们需要定义状态,状态转移方程描述了从一个状态到另一个状态的转移方式。

通过不断迭代和更新状态,最终获得问题的最优解。

为了更好地理解最优子结构和状态转移方程,在接下来的内容中,我将通过一个具体的例子来详细说明。

假设有一个背包问题,背包的容量为C,有n个物品,每个物品有重量wi和价值vi。

我们的目标是在背包容量有限的情况下,选择一些物品放入背包,使得背包中物品的总价值最大化。

首先,我们定义一个二维数组dp,其中dp[i][j]表示将前i个物品放入容量为j的背包中所能获得的最大价值。

那么我们可以推导出状态转移方程如下:当j<wi时:dp[i][j] = dp[i-1][j]当j>=wi时:dp[i][j] = max(dp[i-1][j], dp[i-1][j-wi] + vi)上述状态转移方程表示的意思是,当待放入物品i的重量wi大于背包的容量j时,我们无法将该物品放入背包,此时的最大价值与dp[i-1][j]相等,即与前i-1个物品得到的最大价值相同。

而当待放入物品i的重量wi小于等于背包的容量j时,我们可以选择放入该物品或者不放入,取两者之间的最大价值作为dp[i][j]。

通过不断迭代计算状态转移方程,最终可以得到问题的最优解dp[n][C],即将n个物品放入容量为C的背包中所能获得的最大价值。

动态规划方法求解线性规划问题

动态规划方法求解线性规划问题

动态规划方法求解线性规划问题引言概述:动态规划是一种常用的优化方法,可以用于求解各种复杂的问题。

在线性规划问题中,动态规划方法也可以发挥重要作用。

本文将介绍动态规划方法在求解线性规划问题中的应用,并分为四个部份进行详细阐述。

一、线性规划问题的定义和特点1.1 线性规划问题的定义线性规划是一种数学建模方法,用于求解一类特殊的优化问题。

它的目标函数和约束条件都是线性的。

1.2 线性规划问题的特点线性规划问题具有可行解的存在性、有界性和最优性。

同时,线性规划问题的解空间是一个凸多面体。

二、动态规划方法的基本思想2.1 动态规划的基本原理动态规划是一种将问题分解为子问题并保存子问题解的方法。

通过递归地求解子问题,最终得到原问题的解。

2.2 动态规划方法的三个基本步骤动态规划方法包括问题的划分、状态的定义和状态转移方程的建立。

通过这三个步骤,可以得到问题的最优解。

2.3 动态规划方法的优点动态规划方法具有时间和空间复杂度低的优点,可以有效地求解大规模的优化问题。

三、动态规划方法在线性规划问题中的应用3.1 线性规划问题的动态规划模型将线性规划问题转化为动态规划模型,可以通过动态规划方法求解。

其中,状态的定义和状态转移方程的建立是关键。

3.2 动态规划方法求解线性规划问题的步骤通过将线性规划问题转化为动态规划模型,可以按照动态规划方法的三个基本步骤求解线性规划问题。

3.3 动态规划方法求解线性规划问题的实例通过一个具体的实例,详细介绍动态规划方法在求解线性规划问题中的具体应用步骤和求解过程。

四、动态规划方法在线性规划问题中的局限性和改进方法4.1 动态规划方法的局限性动态规划方法在求解线性规划问题时,可能会面临状态空间过大、计算复杂度高等问题。

4.2 动态规划方法的改进方法为了解决动态规划方法的局限性,可以采用剪枝策略、状态压缩等方法来提高求解效率。

结论:动态规划方法在求解线性规划问题中具有重要的应用价值。

动态规划 cvxpy

动态规划 cvxpy

动态规划 cvxpy
动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题。

但是经分解得到的子问题往往不是互相独立的。

不同子问题的数目常常只有多项式量级。

在用分治法求解时,有些子问题被重复计算了许多次。

如果能够保存已解决的子问题的答案,而在需要时再找出已求得的答案,就可以避免大量重复计算,从而得到多项式时间算法。

用一个表来记录所有已经解决的子问题的答案。

不管该子问题以后是否被用到,只要它被计算过,就将其结果填入表中。

这就是动态规划的基本思想。

动态规划(dynamicprogramming)是运筹学的一个分支,是求解决策过程(decisionprocess)最优化的数学方法。

20世纪50年代初美国数学家R。

E。

Bellman等人在研究多阶段决策过程(multistepdecisionprocess)的优化问题时,提出了著名的最优化原理(principleofoptimality),把多阶段过程转化为一系列单阶段问题,利用各阶段之间的关系,逐个求解,创立了解决这类过程优化问题的新方法——动态规划。

1957年出版了他的名著DynamicProgramming,这是该领域的第一本著作。

动态规划问世以来,在经济管理、生产调度、工程技术和最优控制等方面得到了广泛的应用。

例如最短路线、库存管理、资源分配、设备更新、排序、装载等问题,用动态规划方法比用其它方法求解更为方便。

动态规划-动态规划-美国数学家贝尔曼-动态规划领域

动态规划-动态规划-美国数学家贝尔曼-动态规划领域

物品
1 2 … j …n
重量(公斤/件) a1 a2 … aj … an
每件使用价值 c1 c2 … cj … cn
类似问题:工厂里的下料问题、运输中的 货物装载问题、人造卫星内的物品装载问题等。
生产决策问题:企业在生产过程中,由于需求 是随时间变化的,因此企业为了获得全年的最佳 生产效益,就要在整个生产过程中逐月或逐季度 地根据库存和需求决定生产计划。
描述状态的变量称为状态变量,它可用一个数、 一组数或一向量(多维情形)来描述,第k阶段 的状态变量常用sk表示,通常一个阶段有若干个 状态。
第k阶段的状态就是该阶段所有始点的集合, 用Sk表示。在第1阶段状态变量s1是确定的,称初 始状态。如引例中:
S1 A,S2 B1, B2, B3,S3 C1,C2,C3,S4 D1, D2
min
4
9
12
决策点为B3
AB3
f2
B3
3 9*
f1(A)=12说明从A到E的最短距离为12,最短路 线的确定可按计算顺序反推而得。即
A→B3→C2→D2→E 上述最短路线问题的计算过程,也可借助于图
形直观的表示出来:
12 2 A4
3
11
B1
7 4
6
93
B2 2
4
96
B3
2 5
6
C1 3
多阶段决策过程特点:
(1)根据过程的特性可以将过程按空 间、时间等标志分为若干个互相联系又互相 区别的阶段。
(2)在每一个阶段都需要做出决策,从 而使整个过程达到最好的效果。
(3)在处理各阶段决策的选取上,不仅只 依赖于当前面临的状态,而且还要注意对以后 的发展。即是从全局考虑解决局部(阶段)的 问题。

DP入门(2)——DAG上的动态规划

DP入门(2)——DAG上的动态规划

DP⼊门(2)——DAG上的动态规划有向⽆环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础。

很多问题都可以转化为DAG上的最长路、最短路或路径计数问题。

⼀、DAG模型【嵌套矩形问题】问题:有n个矩形,每个矩形可以⽤两个整数a、b描述,表⽰它的长和宽。

矩形X(a , b)可以嵌套在矩形Y(c , d)中当且仅当a<c,b<d,或者b<c,a<d(相当于把矩形X旋转90°)。

例如(1,5)可以嵌套在(6, 2)内,但不能嵌套在(3, 4)内。

你的任务是选出尽可能多的矩形排成⼀⾏,使得除了最后⼀个之外,每个矩形都可以嵌套在下⼀个矩形内。

如果有多解,矩形编号的字典序应尽量⼩。

分析:矩形之间的“可嵌套”关系是⼀个典型的⼆元关系(我的理解是两个矩形之间存在关系),⼆元关系可以⽤图来建模。

如果矩形X可以嵌套在矩形Y⾥,就从X到Y连⼀条有向边。

这个有向图必然是⽆环的,因为⼀个矩形⽆法直接或间接地嵌套在⾃⼰内部。

换句话说,它是⼀个DAG。

这样,所要求的便是DAG上的最长路径。

【硬币问题】问题:有n种硬币,⾯值分别为V1, V2, ..., V n,每种都有⽆限多。

给定⾮负整数S,可以选⽤多少个硬币,使得⾯值之和恰好为S?输出硬币数⽬的最⼩值和最⼤值。

1 <= n <= 100, 0 <= S <= 10000, 1 <= V i <= S。

分析:此问题尽管看上去和嵌套矩形问题很不⼀样,但本题的本质也是DAG上的路径问题。

将每种⾯值看作⼀个点,表⽰“还需要凑⾜的⾯值”,则初始状态为S,⽬标状态为0。

若当前在状态 i,每使⽤⼀个硬币 j,状态便转移到i - V j。

补充:这个模型和上⼀题类似,但也有⼀些明显地不同之处:上题并没有确定路径的起点和终点(可以把任意矩形放在第⼀个和最后⼀个),⽽本题的起点必须为S,终点必须为0。

动态规划(生产和存储问题)

动态规划(生产和存储问题)

动态规划(生产和存储问题)一、动态规划法的发展及其研究内容动态规划是运筹学的一个分支,是求解决策过程最优化的数学方法。

20世纪50年代初美国数学家R.E.BELLMAN等人在研究多阶段决策过程的优化问题时,提出了著名的最优化原理,把多阶段问题转化为一系列的单阶段问题,逐个求解创立了解决这类过程优化问题的新方法——动态规划。

1957年出版的他的名著《Dynamic Proggramming》,这是该领域的第一本著作。

动态规划问世以来,在经济管理·生产调度·工程技术和最优控制等方面得到了广泛的应用。

例如最短路线·库存管理·资源分配·设备更新·组合·排序·装载等问题,采用动态规划法求解比用其他方法更为简便。

二、动态规划法基本概念一个多阶段决策过程最优化问题的动态规划模型通常包括以下几个要素:1.阶段阶段(stage)是对整个过程的自然划分。

通常根据时间顺序或是空间特征来划分阶段,对于与时间,空间无关的“静态”优化问题,可以根据其自然特征,人为的赋予“时段”概念,将静态问题动态化,以便按阶段的顺序解优化问题。

阶段变量一般用k=1.2….n.表示。

1.状态状态(state)是我们所研究的问题(也叫系统)在过个阶段的初始状态或客观条件。

它应能描述过程的特征并且具有无后效性,即当某阶段的状态给定时,这个阶段以后的过程的演变与该阶段以前各阶段的状态无关。

通常还要求状态是可以直接或者是间接可以观测的。

描述状态的变量称为状态变量(State Virable)用s 表示,状态变量的取值集合称为状态集合,用S表示。

变量允许取值的范围称为允许状态集合(set of admissble states).用x(k)表示第k阶段的状态变量,它可以是一个数或者是一个向量。

用X(k)表示第k阶段的允许状态集合。

n 个阶段的决策过程有n+1个状态变量,x(n+1)是x(n)的演变的结果。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input. Calculate the fence placement that maximizes the average, given the constraint.Input* Line 1: Two space-separated integers, N and F.* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.Output* Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields. Sample Input10 664210385941Sample Output6500Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of Aizu, had decided to grant him a rectangular estate within a large field in the Aizu Basin. Although the size (width and height) of the estate was strictly specified by the lord, he was allowed to choose any location for the estate in the field. Inside the field which had also a rectangular shape, many Japanese persimmon trees, whose fruit was one of the famous products of the Aizu region known as 'Mishirazu Persimmon', were planted. Since persimmon was Hayashi's favorite fruit, he wanted to have as many persimmon trees as possible in the estate given by the lord.For example, in Figure 1, the entire field is a rectangular grid whose width and height are 10 and 8 respectively. Each asterisk (*) represents a place of a persimmon tree. If the specified width and height of the estate are 4 and 3 respectively, the area surrounded by the solid line contains the most persimmon trees. Similarly, if the estate's width is 6 and its height is 4, the area surrounded by the dashed line has the most, and if the estate's width and height are 3 and 4 respectively, the area surrounded by the dotted line contains the most persimmon trees. Note that the width and height cannot be swapped; the sizes 4 by 3 and 3 by 4 are different, as shown in Figure 1.Figure 1: Examples of Rectangular EstatesYour task is to find the estate of a given size (width and height) that contains the largest number of persimmon trees.InputThe input consists of multiple data sets. Each data set is given in the following format.NW Hx1 y1x2 y2...xN yNS TN is the number of persimmon trees, which is a positive integer less than 500. W and H are the width and the height of the entire field respectively. You can assume that both W and H are positive integers whose values are less than 100. For each i (1 <= i <= N), xi and yi are coordinates of the i-th persimmon tree in the grid. Note that the origin of each coordinate is 1. You can assume that 1 <= xi <= W and 1 <= yi <= H, and no two trees have the same positions. But you should not assume that the persimmon trees are sorted in some order according to their positions. Lastly, S and T are positive integers of the width and height respectively of the estate given by the lord. You can also assume that 1 <= S <= W and 1 <= T <= H.The end of the input is indicated by a line that solely contains a zero. OutputFor each data set, you are requested to print one line containing the maximum possible number of persimmon trees that can be included in an estate of the given size.Sample Input1610 82 22 52 73 33 84 24 54 86 46 77 57 88 18 49 610 34 386 41 22 12 43 44 25 36 16 23 2Sample Output 432033Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages:Alice: "Let's just use a very simple code: We'll assign 'A' the code word 1, 'B' will be 2, and so on down to 'Z' being assigned 26."Bob: "That's a stupid code, Alice. Suppose I send you the word 'BEAN' encoded as 25114. You could decode that in many different ways!”Alice: "Sure you could, but what words would you get? Other than 'BEAN', you'd get 'BEAAD', 'YAAD', 'YAN', 'YKD' and 'BEKD'. I think you would be able to figure out the correct decoding. And why would you send me the word ‘BEAN’ anyway?”Bob: "OK, maybe that's a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense." Alice: "How many different decodings?"Bob: "Jillions!"For some reason, Alice is still unconvinced by Bob's argument, so she requires a program that will determine how many decodings there can be for a given string using her code.InputInput will consist of multiple input sets. Each set will consist of a single line of digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of '0' will terminate the input and should not be processedOutputFor each input set, output the number of possible decodings for the input string. All answers will be within the range of a long variable.Sample Input2511411111111113333333333Sample Output 68912063John never knew he had a grand-uncle, until he received the notary's letter. He learned that his late grand-uncle had gathered a lot of money, somewhere in South-America, and that John was the only inheritor.John did not need that much money for the moment. But he realized that it would be a good idea to store this capital in a safe place, and have it grow until he decided to retire. The bank convinced him that a certain kind of bond was interesting for him.This kind of bond has a fixed value, and gives a fixed amount of yearly interest, payed to the owner at the end of each year. The bond has no fixed term. Bonds are available in different sizes. The larger ones usually give a better interest. Soon John realized that the optimal set of bonds to buy was not trivial to figure out. Moreover, after a few years his capital would have grown, and the schedule had to be re-evaluated.With a capital of e10 000 one could buy two bonds of $4 000, giving a yearly interest of $800. Buying two bonds of $3 000, and one of $4 000 is a better idea, as it gives a yearly interest of $900. After two years the capital has grown to $11 800, and it makes sense to sell a $3 000 one and buy a $4 000 one, so the annual interest grows to $1 050. This is where this story grows unlikely: the bank does not charge for buying and selling bonds. Next year the total sum is $12 850, which allows for three times $4 000, giving a yearly interest of $1 200.Here is your problem: given an amount to begin with, a number of years, and a set of bonds with their values and interests, find out how big the amount may grow in the given period, using the best schedule for buying and selling bonds.InputThe first line contains a single positive integer N which is the number of test cases. The test cases follow.The first line of a test case contains two positive integers: the amount to start with (at most $1 000 000), and the number of years the capital may grow (at most 40).The following line contains a single number: the number d (1 <= d <= 10) ofavailable bonds.The next d lines each contain the description of a bond. The description of a bond consists of two positive integers: the value of the bond, and the yearly interest for that bond. The value of a bond is always a multiple of $1 000. The interest of a bond is never more than 10% of its value.OutputFor each test case, output – on a separate line – the capital at the end of the period, after an optimal schedule of buying and selling.Sample Input110000 424000 4003000 250Sample Output14050The Recaman's sequence is defined by a0 = 0 ; for m > 0, a m = a m−1− m if the rsulting a m is positive and not already in the sequence, otherwise a m = a m−1 + m.The first few numbers in the Recaman's Sequence is 0, 1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9 ...Given k, your task is to calculate a k.InputThe input consists of several test cases. Each line of the input contains an integer k where 0 <= k <= 500000.The last line contains an integer −1, which shou ld not be processed. OutputFor each k given in the input, print one line containing a k to the output. Sample Input710000-1Sample Output2018658Farmer John's cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump.The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.Each potion has a 'strength' (1 <= strength <= 500) that enhances the cows' jumping ability. Taking a potion during an odd time step increases the cows' jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows' jumping ability is, of course, 0.No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.Determine which potions to take to get the highest jump.Input* Line 1: A single integer, P* Lines 2..P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.Output* Line 1: A single integer that is the maximum possible jump.Sample Input872184356Sample Output 17Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0. Define set B = {< x, y > | x, y ∈R and there exists an index i > 0 such that 0 <= y <= hi ,∑0<=j<=i-1wj <= x <= ∑0<=j<=i wj}Again, define set S = {A| A = WH for some W , H ∈R+ and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}.Your mission now. What is Max(S)?Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy.But for this one, believe me, it's difficult.InputThe input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 andw1h1+w2h2+...+w n h n < 109.OutputSimply output Max(S) in a single line for each case.Sample Input31 23 41 233 41 23 4-1Sample Output12 14"Fat and docile, big and dumb, they look so stupid, they aren't much fun..."- Cows with Guns by Dana LyonsThe cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows arewell-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.Input* Line 1: A single integer N, the number of cows* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.Output* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0.Sample Input5-5 78 -66 -32 1-8 -5Sample Output8HintOUTPUT DETAILS:Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF = 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value of TS+TF to 10, but the new value of TF would be negative, so it is not allowed.2231Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise.FJ's N cows (1 <= N <= 10,000) all graze at various locations on a longone-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.Input* Line 1: N* Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000). OutputThere are five cows at locations 1, 5, 3, 2, and 4.Sample Input515324Sample Output40HintINPUT DETAILS:There are five cows at locations 1, 5, 3, 2, and 4.OUTPUT DETAILS:Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.2279Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5, 3, 3 and 1 students.X X X X XX X XX X XXIn addition, Mr. Young wants the students in each row arranged so that heights decrease from left to right. Also, student heights should decrease from the back to the front. Thinking about it, Mr. Young sees that for the12-student example, there are at least two ways to arrange the students (with 1 as the tallest etc.):1 2 3 4 5 1 5 8 11 126 7 8 2 6 99 10 11 3 7 1012 4Mr. Young wonders how many different arrangements of the students there might be for a given arrangement of rows. He tries counting by hand starting with rows of 3, 2 and 1 and counts 16 arrangements:123 123 124 124 125 125 126 126 134 134 135 135 136 136 145 14645 46 35 36 34 36 34 35 25 26 24 26 24 25 26 256 5 6 5 6 4 5 4 6 5 6 4 5 4 3 3Mr. Young sees that counting by hand is not going to be very effective for any reasonable number of students so he asks you to help out by writing a computer program to determine the number of different arrangements of students for a given set of rows.InputThe input for each problem instance will consist of two lines. The first line gives the number of rows, k, as a decimal integer. The second line contains the lengths of the rows from back to front (n1, n2,..., nk) as decimal integers separated by a single space. The problem set ends with a line with a row count of 0. There will never be more than 5 rows and the total number of students, N, (sum of the row lengths) will be at most 30.OutputThe output for each problem instance shall be the number of arrangements of the N students into the given rows so that the heights decrease along each row from left to right and along each column from back to front as a decimal integer. (Assume all heights are distinct.) The result of each problem instance should be on a separate line. The input data will be chosen so that the result will always fit in an unsigned 32 bit integer.Sample Input13051 1 1 1 133 2 145 3 3 156 5 4 3 2215 15Sample Output111641581418926089694845Input is the matrix A of N by N non-negative integers.A distance between two elements A ij and A pq is defined as |i − p| + |j − q|. Your program must replace each zero element in the matrix with the nearest non-zero one. If there are two or more nearest non-zeroes, the zero must be left in place.Constraints1 ≤ N ≤ 200, 0 ≤ Ai ≤ 1000000InputInput contains the number N followed by N2 integers, representing the matrix row-by-row.OutputOutput must contain N2 integers, representing the modified matrixrow-by-row.Sample Input30 0 01 0 20 3 0Sample Output1 0 21 0 20 3 0Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry. There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?InputThe first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.OutputFor each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.Sample Input22 10 101020304050607080902 10 3103040Sample Output 100 550 2The public transport administration of Ekaterinburg is anxious about the fact that passengers don't like to pay for passage doing their best to avoid the fee. All the measures that had been taken (hard currency premiums for all of the chiefs, increase in conductors' salaries, reduction of number of buses) were in vain. An advisor especially invited from the Ural State University says that personally he doesn't buy tickets because he rarely comes across the lucky ones (a ticket is lucky if the sum of the first three digits in its number equals to the sum of the last three ones). So, the way out is found — of course, tickets must be numbered in sequence, but the number of digits on a ticket may be changed. Say, if there were only two digits, there would have been ten lucky tickets (with numbers 00, 11, ..., 99). Maybe under the circumstances the ratio of the lucky tickets to the common ones is greater? And what if we take four digits? A huge work has brought the long-awaited result: in this case there will be 670 lucky tickets. But what to do if there are six or more digits?So you are to save public transport of our city. Write a program that determines a number of lucky tickets for the given number of digits. By the way, there can't be more than 10 digits on one ticket.InputInput contains a positive even integer N not greater than 10. It's an amount of digits in a ticket number.OutputOutput should contain a number of tickets such that the sum of the first N/2 digits is equal to the sum of the second half of digits.Sample Input4Sample Output670Mr. F. wants to get a document be signed by a minister. A minister signs a document only if it is approved by his ministry. The ministry is an M-floor building with floors numbered from 1 to M, 1<=M<=100. Each floor has N rooms (1<=N<=500) also numbered from 1 to N. In each room there is one (and only one) official.A document is approved by the ministry only if it is signed by at least one official from the M-th floor. An official signs a document only if at least one of the following conditions is satisfied:a. the official works on the 1st floor;b. the document is signed by the official working in the room with the same number but situated one floor below;c. the document is signed by an official working in a neighbouring room (rooms are neighbouring if they are situated on the same floor and their numbers differ by one).Each official collects a fee for signing a document. The fee is a positive integer not exceeding 10^9.You should find the cheapest way to approve the document.InputThe first line of an input file contains two integers, separated by space. The first integer M represents the number of floors in the building, and the second integer N represents the number of rooms per floor. Each of the next M lines contains N integers separated with spaces that describe fees (thek-th integer at l-th line is the fee required by the official working in the k-th room at the l-th floor).OutputYou should print the numbers of rooms (one per line) in the order they should be visited to approve the document in the cheapest way. If there are more than one way leading to the cheapest cost you may print an any of them.Sample Input3 410 10 1 102 2 2 101 10 10 10Sample Output33211HintYou can assume that for each official there always exists a way to get the approval of a document (from the 1st floor to this official inclusively) paying no more than 10^9.This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).InputThe first line of the input contains the single number N. Each of next N lines contains one number from the given set.OutputIn case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them. Sample Input512341Sample Output223It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for them to fall. However, she must catch them in the air since the apples bruise when they hit the ground (and no one wants to eat bruised apples). Bessie is a quick eater, so an apple she does catch is eaten in just a few seconds.Each minute, one of the two apple trees drops an apple. Bessie, having much practice, can catch an apple if she is standing under a tree from which one falls. While Bessie can walk between the two trees quickly (in much less than a minute), she can stand under only one tree at any time. Moreover, cows do not get a lot of exercise, so she is not willing to walk back and forth between the trees endlessly (and thus misses some apples).Apples fall (one each minute) for T (1 <= T <= 1,000) minutes. Bessie is willing to walk back and forth at most W (1 <= W <= 30) times. Given which tree will drop an apple each minute, determine the maximum number of apples which Bessie can catch. Bessie starts at tree 1.Input* Line 1: Two space separated integers: T and W* Lines 2..T+1: 1 or 2: the tree that will drop an apple each minute. Output* Line 1: The maximum number of apples Bessie can catch without walking more than W times.Sample Input7 22112211Sample Output6HintINPUT DETAILS:Seven apples fall - one from tree 2, then two in a row from tree 1, then two in a row from tree 2, then two in a row from tree 1. Bessie is willing to walk from one tree to the other twice.OUTPUT DETAILS:Bessie can catch six apples by staying under tree 1 until the first two have dropped, then moving to tree 2 for the next two, then returning back to tree 1 for the final two.2392The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000).Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.Input* Line 1: A single integer, K* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.Output* Line 1: A single integer H, the maximum height of a tower that can be builtSample Input37 40 35 23 82 52 6Sample Output48HintOUTPUT DETAILS:From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.2424Sick and tired of pushing paper in the dreary bleary-eyed world of finance, Flo ditched her desk job and built her own restaurant.In the small restaurant, there are several two-seat tables, four-seat tables and six-seat tables. A single diner or a group of two diners should be arranged to a two-seat table, a group of three or four diners should be arranged to afour-seat table, and a group of five or six diners should be arranged to asix-seat table.Flo's restaurant serves delicious food, and many people like to eat here. Every day when lunch time comes, the restaurant is usually full of diners. If there is no suitable table for a new coming group of diners, they have to wait until some suitable table is free and there isn't an earlier arrival group waiting for the same kind of tables. Kind Flo will tell them how long they will get their seat, and if it's longer than half an hour, they will leave for another restaurant.Now given the list of coming diners in a day, please calculate how many diners take their food in Flo's restaurant. You may assume it takes half an hour for every diner from taking a seat to leaving the restaurant.InputThere are several test cases. The first line of each case contains there positive integers separated by blanks, A, B and C (A, B, C >0, A + B + C <= 100), which are the number of two-seat tables, the number of four-seat tables and the number of six-seat tables respectively. From the second line, there is a list of coming groups of diners, each line of which contains two integers, T and N (0 < N <= 6), representing the arrival time and the number of diners of each group. The arrival time T is denoted by HH:MM, and fixed between 08:00 and 22:00 (the restaurant closes at 23:00). The list is sorted by the arrival time of each group in an ascending order, and you may assume that no groups arrive at the same time. Each test case is ended by a line of "#".A test case with A =B =C = 0 ends the input, and should not be processed. Output。

相关文档
最新文档