列表

详情


NC24881. [USACO 2009 Ope G]Tower of Hay

描述

Cows so hate the dark. In order to change a lightbulb at the top of the barn, Bessie must build a tower out of bales of hay to climb so that she can reach it. The N (1 <= N <= 100,000) bales of hay (conveniently numbered 1..N) enter the barn sequentially on a conveyor belt. Bale i has integer width w_i (1 <= w_i <= 10,000); all bales have depth and height of one unit.
Bessie must use all N hay bales to build the tower and must lay them down in the order they arrive. She can lay as many bales as she wishes (tightly packed in a line, of course) to create the foundation of the tower. She can then lay some of the next bales on top of the previous level to create the next level (no level can be wider than the level beneath it). She continues this process until all the bales are used. She must stack the bales in the order they arrive, from the bottom to the top of the tower. To be clear: she must not try to sneak a bale back on the foundation after a bale is placed on the second layer.
Bessie's goal is to create the tallest tower possible -- and she already knows how wide each every bale of hay will be as it comes into the barn on the belt. How tall a tower can she construct?

输入描述

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer: w_i

输出描述

* Line 1: A single integer, the height of the tallest possible tower that can be built

示例1

输入:

3
1
2
3

输出:

2

说明:

Use the first bales with width 1 and 2 for the bottom row (total
width: 3), then the next bale (width 3) for the top row.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 53ms, 内存消耗: 1636K, 提交时间: 2019-08-13 08:38:07

#include<iostream>
#include<cstdio>
#include<algorithm>
#define FOR(i,a,b) for(register int i(a);i<=b;++i)
#define ROF(i,a,b) for(register int i(a);i>=b;--i)
using namespace std;
const int N(1000010);
int n;
int f[N],g[N],sum[N];
int main()
{
    cin>>n;ROF(i,n,1)cin>>sum[i];
    FOR(i,1,n)sum[i]+=sum[i-1];
    FOR(i,1,n)
    {
        ROF(j,i-1,0)
        if(sum[i]-sum[j]>=g[j])
        {
            f[i]=f[j]+1;
            g[i]=sum[i]-sum[j];
            break;
        }
    }
    cout<<f[n]<<'\n';
    return 0;
}

Pascal(fpc 3.0.2) 解法, 执行用时: 49ms, 内存消耗: 1896K, 提交时间: 2019-08-12 14:30:41

var
n,i,j:longint;
a,f,g:array[0..1000010]of longint; 
begin
 read(n);
 for i:=n downto 1 do read(a[i]);
 for i:=1 to n do a[i]:=a[i]+a[i-1];
 for i:=1 to n do
  begin
   for j:=i-1 downto 0 do
    if a[i]-a[j]>=g[j] then begin
                             f[i]:=f[j]+1;
                             g[i]:=a[i]-a[j];
                             break;
                            end;
  end;
 writeln(f[n]);
end.

C++ 解法, 执行用时: 35ms, 内存消耗: 2296K, 提交时间: 2022-04-30 21:16:39

#include<bits/stdc++.h>
#define w while
#define f(l,r) for(int i=l;i<=r;i++)
#define r(r,l) for(int i=r;i>=l;i--)
using namespace std;
int n,a[100001],s[100001],f[100001],g[100001],q[100001],l,r;
int main() {
	cin>>n;
	r(n,1)cin>>a[i];
	f(1,n)s[i]=s[i-1]+a[i];
	f(1,n){w(l<r&&s[q[l+1]]+g[q[l+1]]<=s[i])l++;f[i]=f[q[l]]+1;g[i]=s[i]-s[q[l]];w(l<r&&s[q[r]]+g[q[r]]>=s[i]+g[i])r--;q[++r]=i;}
	cout<<f[n];
	return 0;
}

上一题