DP大乱炖 A

There are two kinds of tasks, namely A and B. There are N workers and the i-th worker would like to finish one task A in ai minutes, one task B in bi minutes. Now you have X task A and Y task B, you want to assign each worker some tasks and finish all the tasks as soon as possible. You should note that the workers are working simultaneously.

input:In the first line there is an integer T(T<=50), indicates the number of test cases. 
In each case, the first line contains three integers N(1<=N<=50), X,Y(1<=X,Y<=200). Then there are N lines, each line contain two integers ai, bi (1<=ai, bi <=1000). 

output:For each test case, output “Case d: “ at first line where d is the case number counted from one, then output the shortest time to finish all the tasks.

 Sample input:

3
2 2 2
1 10
10 1
2 2 2
1 1
10 10

3 3 3
2 7
5 5
7 2

 Sample output:

 

Case 1: 2
Case 2: 4
Case 3: 6
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3433
题目分析:
你要明白最多所需花费时间为所有人中花费时间最多的人的时间,对于这个时间,我们可以二分得到,然后用DP来验证。
详细DP验证过程,请看代码注释
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
using namespace std;
int t;
int n,m,f[55][510],a[51],b[51],x,y,l,r;
bool work(int p)
{
    memset(f,-1,sizeof(f));
    //每次验证之前需要把数组全部赋值为负 
    f[0][0]=0;
    for(int i=1;i<=n;i++)//枚举每一个工人 
    {
        for(int j=0;j<=x;j++)//在此处,我们做一个类似于背包问题的dp,把所需要做A时间看为背包代价 
        {
            f[i][j]=f[i-1][j];
            for(int k=0;k<=j;k++)//在做j个A事件时能做的最多的B事件 
            {
                if(p-k*a[i]<0) break;
                if(f[i-1][j-k]!=-1)
                f[i][j]=max(f[i][j],f[i-1][j-k]+(p-k*a[i])/b[i]);
            }
        }
    }
    if(f[n][x]>=y) return true;//如果在做完所有A事件的时候所能做最多数量的B事件。 
    else return false;
}
int main()
{
    scanf("%d",&t);
    for(int qw=1;qw<=t;qw++)
    {
        scanf("%d%d%d",&n,&x,&y);
        for(int i=1;i<=n;i++){scanf("%d%d",&a[i],&b[i]);}
        l=1,r=1000000;
        while(l+1<r)//二分枚举最短时间 
        {
            int mid=(l+r)/2;
            if(work(mid))r=mid;
            else l=mid;
        }
        cout<<"Case "<<qw<<": ";
        if(work(l))cout<<l<<endl;
        else cout<<r<<endl;
    }
    return 0;
} 
View Code

 

转载于:https://www.cnblogs.com/mybing/p/7358364.html

### Hive 中的数组操作及其用法 Hive 提供了一系列用于处理数组类型的内置函数,这些函数可以帮助用户轻松地执行各种数组操作。以下是几个常用的 Hive 数组函数以及它们的具体用法和示例。 #### 1. `size(array)` 该函数返回数组中的元素数量。 ```sql SELECT size(array(1, 2, 3)); -- 返回 3 [^1] ``` #### 2. `array_contains(array<T>, value)` 此函数用于判断指定值是否存在于给定的数组中。如果存在,则返回 true;否则返回 false。 ```sql SELECT array_contains(array('apple', 'banana'), 'banana'); -- 返回 true ``` #### 3. `sort_array(array<T>)` `sort_array` 函数可以按照升序或降序对数组进行排序,默认情况下按升序排列。 ```sql -- 升序排序 SELECT sort_array(array(5, 3, 8, 1)); -- 返回 [1,3,5,8] [^1] -- 降序排序 SELECT sort_array(array(5, 3, 8, 1), FALSE); -- 返回 [8,5,3,1] ``` #### 4. `concat_ws(separator, array<string>)` 通过指定分隔符将数组中的字符串连接成单个字符串。 ```sql SELECT concat_ws(',', array('red', 'green', 'blue')); -- 返回 red,green,blue ``` #### 5. `split(string, pattern)` 这个函数的作用是从字符串创建一个数组,基于所提供的正则表达式分割输入字符串。 ```sql SELECT split('one|two|three', '\\|'); -- 返回 ["one","two","three"] [^1] ``` 以上就是一些关于 Hive 数据库中数组类型的操作方法及其实例说明。 ### 示例代码综合展示 下面是一个综合使用的 SQL 查询例子: ```sql WITH example_data AS ( SELECT array('dog', 'cat', 'bird') as pets, array(90, 75, 85) as scores ) SELECT pets, scores, size(pets) as pet_count, array_contains(scores, 85) as has_score_85, sort_array(scores) as sorted_scores FROM example_data; ``` 上述查询会输出宠物列表、分数列表、宠物的数量、是否存在特定分数以及已排序的成绩表。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值