牛客多校第八场K 贪心+int128

Kabaleo Lite
题意:给你n种菜的利润和n种菜的数量,且招待客人的话一定要从第一盘开始顺序选择,不能跳过任意一盘菜,问你最多能招待多少个客人,然后在招待相同的客人的情况下利润如何才能最高
思路:首先题目求的是招待最多的客人,而且还是只能从第一盘开始招待,所以显而易见的是最多能招待的客人为第一盘菜的数量,然后在招待相同客人的情况下,进行贪心,从前往后,首先维护利润的前缀和,菜的数量前面的一定大于等于后面的,所以可以从大到小进行贪心,具体实现可看代码,sort一边,然后定义最小的位置,即这个位置之后的菜都不能用了,需要注意的是__int128。

代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define rep(i, a, n) for(int i = a; i <= n; i++)
#define per(i, a, n) for(int i = n; i >= a; i--)
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define fopen freopen("file.in","r",stdin);freopen("file.out","w",stdout);
#define fclose fclose(stdin);fclose(stdout);
#define PI 3.14159265358979323846
const int inf = 1e9;
const ll onf = 1e18;
const int maxn = 1e5+10;
inline void print(__int128 x){
    if(x<0)
    {
        x=-x;
        putchar('-');
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    return x*f;
}
struct node
{
    ll id, num;
    bool operator < (const node &x)const {
        if(x.num==num) return x.id < id;
        return x.num<num;
    }
};
node a[maxn];
ll b[maxn];
inline void cf(){
    ll t = read();
    ll cas = 1;
    while(t--){
        ll n = read();
        a[0].num = 0;
        rep(i,1,n) a[i].num=read(),a[i].num+=a[i-1].num, a[i].id=i;
        int minn = inf;
        __int128 res = 0;
        rep(i,1,n) {
            b[i]=read();
            if(b[i]<minn){
                minn = b[i];
            }else b[i]=minn;
        }
        ll f = 0; minn = inf;
        sort(a+1, a+n+1);
        rep(i,1,n){
           if(a[i].id<minn && b[a[i].id]>f){
                res += a[i].num*(b[a[i].id]-f);
                f = b[a[i].id];
                //printf("%lld %lld\n", a[i].id, f);
           }
        }
        printf("Case #%lld: %lld ", cas++, b[1]),print(res);
        printf("\n");
    }
    return ;
}
signed main(){
    cf();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值