codeforces 294B Shaass and Bookshelf

本文探讨了如何通过贪心算法和背包问题解决最小垂直书堆厚度问题,详细阐述了解题思路并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Shaass and Bookshe
time limit per test    2 seconds
memory limit per test 256 megabytes
input   standard input
output  standard output

Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshelf's dimensions to be as small as possible. The thickness of thei-th book isti and its pages' width is equal towi. The thickness of each book is either1 or2. All books have the same page heights.

Shaass puts the books on the bookshelf in the following way. First he selects some of the books and put them vertically. Then he puts the rest of the books horizontally above the vertical books. The sum of the widths of the horizontal books must be no more than the total thickness of the vertical books. A sample arrangement of the books is depicted in the figure.

Help Shaass to find the minimum total thickness of the vertical books that we can achieve.

Input

The first line of the input contains an integer n,(1 ≤ n ≤ 100). Each of the nextn lines contains two integersti andwi denoting the thickness and width of thei-th book correspondingly, (1 ≤ ti ≤ 2, 1 ≤ wi ≤ 100).

Output

On the only line of the output print the minimum total thickness of the vertical books that we can achieve.

Sample test(s)
Input
5
1 12
1 3
2 15
2 5
2 1
Output
5
Input
3
1 10
2 1
2 4
Output
3
  昨天晚上处理好软件工程的作业后,都很晚了 ,就开始做这题,做到这题的时候,实在是太困了,本想着用贪心,可是一时想不出策略,就睡了。 今天早上起来想了想,这题好像是用背包能搞,n=100, 并且t都是2 的时候,最大值也就是200, 完全可以看成是一个容量为v=200的背包,然后从1开始枚举,背包的容量是1-200,背包的大小就是所求t和,只要找到结果肯定是最小的。 背包里面: 正好能将其装满,将w看成价值,此价值最大, 只要剩下的w和<=枚举的背包大小就可以了
#include <stdio.h>
#include <string.h>
#include <math.h>
struct num
{
    int v,w;
}a[1000];
int V[1000],s,n,sum_val;
int INF=0x7ffffff;
int main()
{
    int ZeroOnePack(int v);
    int i,j,m,t,k;
    while(scanf("%d",&n)!=EOF)
    {
        s=0;
        sum_val=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d %d",&a[i].v,&a[i].w);
            s+=a[i].v;
            sum_val+=a[i].w;
        }
        for(i=1;i<=s;i++)
        {
            k=ZeroOnePack(i);
            if(k==1)
            {
                break;
            }
        }
        printf("%d\n",i);
    }
    return 0;
}
int ZeroOnePack(int v)
{
    int i,j;
    V[0]=0;
    for(i=1;i<=v;i++)
    {
        V[i]=-INF;
    }
    for(i=1;i<=n;i++)
    {
        for(j=v; j>=a[i].v; j--)
        {
            if(V[j]<(V[j-a[i].v]+a[i].w))
            {
                V[j]=V[j-a[i].v]+a[i].w;
            }
        }
    }
    if(v>=(sum_val-V[v]))
    {
        return 1;
    }
    return 0;
}

### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值