2020牛客暑期多校训练营(第八场)—— K

Kabaleo Lite

链接:https://ac.nowcoder.com/acm/contest/5673/K
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

Tired of boring WFH (work from home), Apollo decided to open a fast food restaurant, called \textbf{Kabaleo Lite}Kabaleo Lite.
The restaurant serves n kinds of food, numbered from 1 to n. The profit for the i-th kind of food is a_ia
i

. Profit may be negative because it uses expensive ingredients. On the first day, Apollo prepared b_ib
i

dishes of the i-th kind of food.
The peculiarity of Apollo’s restaurant is the procedure of ordering food. For each visitor Apollo himself chooses a set of dishes that this visitor will receive. When doing so, Apollo is guided by the following rules:
every visitor should receive at least one dish.
each visitor should receive continuous kinds of food started from the first food. And the visitor will receive exactly 1 dish for each kind of food. For example, a visitor may receive 1 dish of the 1st kind of food, 1 dish of the 2nd kind of food, 1 dish of the 3rd kind of food.
What’s the maximum number of visitors Apollo can feed? And he wants to know the maximum possible profits he can earn to have the maximum of visitors.

输入描述:

The first line of the input gives the number of test case, \mathbf{T}T (1 \leq \mathbf{T} \leq 101≤T≤10). \mathbf{T}T test cases follow.
Each test case begins with a line containing one integers n (1 \le n \le 10^51≤n≤10
5
), representing the number of different kinds of food.
The second line contains n space-separated numbers a_ia
i

(-10^9 \le a_i \le 10^9−10
9
≤a
i

≤10
9
), where a_ia
i

denotes the profit of one dish of the i-th kind.
The third line contains n space-separated numbers b_ib
i

(1 \le b_i \le 10^51≤b
i

≤10
5
), where b_ib
i

denotes the number of the i-th kind of dishes.

输出描述:

For each test case, output one line containing ``Case\ #x:\ y\ z’’‘‘Case #x: y z
′′
, where x is the test case number (starting from 1), y is the maximum number of visitors, and z is the maximum possible profits.

示例1

输入
复制
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.
挺简单一道题,没算清楚答案的数据范围,爆了longlong
写这篇博客主要想记录一下 __int128 这个数据类型,第一次见。

#include
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+500;
const ll inf=2e15;
ll a[N],b[N];
struct node {
	ll sum;
	int i;
} zz[N];
bool cmp(node a,node b) {
	if(a.sum==b.sum)return a.i<b.i;
	return a.sum>b.sum;
}
struct tree {
	int l,r;
	ll data,tag;
} t[N*4];
void pushup(int p) {
	t[p].data=min(t[p*2].data,t[p*2+1].data);
	return;
}
void pushdown(int p) {
	if(t[p].tag) {
		t[p*2].data-=1ll*t[p].tag;
		t[p*2].tag+=1ll*t[p].tag;
		t[p*2+1].data-=1ll*t[p].tag;
		t[p*2+1].tag+=1ll*t[p].tag;
		t[p].tag=0;
	}
	return;
}
void build(int p,int l,int r) {
	t[p].l=l,t[p].r=r,t[p].tag=0;
	if(l==r) {
		t[p].data=1ll*b[l];
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	pushup(p);
}
void change(int p,int l,int r,ll val) {
	if(l<=t[p].l&&t[p].r<=r) {
		t[p].data-=1ll*val;
		t[p].tag+=1ll*val;
		return;
	}
	pushdown(p);
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid)change(p*2,l,r,val);
	if(r>mid)change(p*2+1,l,r,val);
	pushup(p);
}
ll ask(int p,int l,int r) {
	if(l<=t[p].l&&t[p].r<=r) {
		return t[p].data;
	}
	ll ans=inf;
	pushdown(p);
	int mid=(t[p].l+t[p].r)/2;
	if(l<=mid)ans=min(ans,ask(p*2,l,r));
	if(r>mid)ans=min(ans,ask(p*2+1,l,r));
	return ans;
}
void print(__int128 x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) {
		print(x / 10);
	}
	putchar(x % 10 + '0');
}
int main() {
	int T;
	scanf("%d",&T);
	for(int cas=1; cas<=T; ++cas) {
		int n;
		scanf("%d",&n);
		zz[0].sum=0;
		for(int i=1; i<=n; ++i) {
			scanf("%lld",&a[i]);
			zz[i].sum=zz[i-1].sum+1ll*a[i];
			zz[i].i=i;
		}
		sort(zz+1,zz+1+n,cmp);
		for(int i=1; i<=n; ++i) {
			scanf("%lld",&b[i]);
		}
		__int128 ans=0;
		build(1,1,n);
		ll cut=0;
		for(int i=1; i<=n; ++i) {
			if(zz[i].sum>=a[1]) {
				cut=ask(1,1,zz[i].i);
				ans+=(__int128)zz[i].sum*cut;
				change(1,1,zz[i].i,cut);
			}
		}
		printf("Case #%d: %lld ",cas,b[1]);
		print(ans);
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(数据结构,大精度运算)