2020牛客暑期多校训练营(第八场) Kabaleo Lite

原题
题目描述
厌倦了无聊的 W F H WFH ,阿波罗决定开设一家名为 K a b a l e o L i t e Kabaleo Lite 的快餐店。
该餐厅提供n种食物,编号从 1 1 n n 。第 i i 种食物的利润是 a i ai 。利润可能为负,因为它使用了昂贵的原料。第一天,阿波罗用第 i i 种食材做了 b i bi 道菜。
阿波罗餐厅的独特之处在于订购食物的过程。阿波罗亲自为每个访客选择了一组该访客将获得的菜肴。这样做时,阿波罗遵循以下规则:
● 每个访客应至少获得一道菜。
● 每个访客都得到从第 1 1 种开始的连续种类编号的菜,而且每道菜只能有一碟。
阿波罗最多可容纳多少访客?而且他想知道最大的访问者可以赚取的最大利润。
输入描述:
输入的第一行给出测试用例的数量 T ( 1 T 10 ) T(1≤T≤10) 。 随后是 T T 测试用例。
每个测试用例均以包含一个整数 n ( 1 n 1 0 5 ) n(1≤n≤10^5) 的一行开头,代表不同种类的食物的数量。
第二行包含n个以空格分隔的数字 a i ( 10 ai(-10 9 a i 10 ≤ai≤10 9 ) ) ,其中ai表示第i类一道菜的收益。
第三行包含 n n 个以空格分隔的数字 b i ( 1 b i 10 bi(1≤bi≤10 5 ) ) ,其中 b i bi 表示第 i i 种菜肴的数量。
输出描述:
对于每个测试用例,以形式输出一行,其中 x x 是测试用例编号(从 1 1 开始), y y 是最大的访问者数量, z z 是最大的可能利润。
样例
输入

2
3
2 -1 3
3 2 1
4
3 -2 3 -1
4 2 1 2

输出

Case #1: 3 8
Case #2: 4 13

说明

For test case 1, the maximum number of visitors is 3, one of a possible solution is:
The first visitor received food 1, the profit is 2.
The second visitor received food 1, the profit is 2.
The third visitor received food 1 + food 2 + food 3, the profit is 2+(-1)+3.

思路
因为每个客户都要从第一道菜开始吃,所有 b [ 1 ] b[1] 就是最大顾客数量。
如果第 i i 盘菜被吃了,那么前 i 1 i-1 盘菜也被吃了,所以可以提前求一下前缀和,然后比较一下与菜本身的利润,因为菜的利润可能是负数。
然后把最大值累加到 a n s ans 即可。
代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+5,maxx=10;
const ll mod=1e4;
int t,n,cnt=1;
ll sum1,sum2,num;
struct node{ll p,q;}a[maxn];
bool cmp(node x,node y){return x.p>y.p;}
struct GJD
{
    ll aa[maxx];
    GJD(){}
    GJD(ll x)
    {
        aa[0]=x;
        for(int i=0;i<maxx-1;i++)aa[i+1]=aa[i]/mod,aa[i]%=mod;
    }
};
GJD J(GJD x,GJD y)
{
    GJD ret;
    for(int i=0;i<maxx;i++)ret.aa[i]=x.aa[i]+y.aa[i];
    for(int i=0;i<maxx-1;i++)ret.aa[i+1]+=ret.aa[i]/mod,ret.aa[i]%=mod;
    return ret;
}
GJD C(GJD x,GJD y)
{
    GJD ret;memset(ret.aa,0,sizeof(ret.aa));
    for(int i=0;i<maxx;i++)for(int j=0;i+j<maxx;j++)ret.aa[i+j]+=x.aa[i]*y.aa[j];
    for(int i=0;i<maxx-1;i++)ret.aa[i+1]+=ret.aa[i]/mod,ret.aa[i]%=mod;
    return ret;
}
void slove(GJD x)
{
    int zgw=0;
    for(int i=maxx-1;i>=0;i--)if(x.aa[i]){zgw=i;break;}
    if(zgw^0)
    {
        if(x.aa[zgw]>0)for(int i=zgw;i^0;i--)x.aa[i]--,x.aa[i-1]+=mod;
        else for(int i=zgw;i^0;i--)x.aa[i]++,x.aa[i-1]-=mod;
        for(int i=0;i<maxx-1;i++)x.aa[i+1]+=x.aa[i]/mod,x.aa[i]%=mod;
        if(!x.aa[zgw])zgw--;
        printf("%lld",x.aa[zgw]);
        for(int i=zgw-1;i>=0;i--)printf("%04lld",abs(x.aa[i]));
    }
    else printf("%lld",x.aa[0]);
}
int main()
{
    for(scanf("%d",&t);t--;num=0)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){scanf("%lld",&a[i].p);if(i^1)a[i].p+=a[i-1].p;}
        for(int i=1;i<=n;i++){scanf("%lld",&a[i].q);if(i^1)a[i].q=min(a[i].q,a[i-1].q);}
        sort(a+1,a+n+1,cmp);GJD ans=GJD(0);
        for(int i=1;i<=n;i++)if(a[i].q>num)ans=J(ans,C(GJD(a[i].p),GJD(a[i].q-num))),num=a[i].q;
        printf("Case #%d: %lld ",cnt++,num);
        slove(ans);
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/bbbll123/article/details/107887521