[LNOI2022] 吃

洛谷题面

贪心很显然是错的,但是可以拿到 \(75\) 真的离谱。。。

题目大意

\(n\) 个食物,每个食物有 \(a_i,b_i\) 两个参数。你的初始质量为 \(1\),可以以 任意顺序 吃这些食物,吃掉食物 \(i\) 之后,你的质量可以乘上 \(a_i\),或者加上 \(b_i\),最大化质量。

\(1\le a_i,b_i\le 10^6\)

题目分析

\(a_i=1\) 时,\(ans\times 1\) 必定小于 \(ans\) 加一个大于等于 \(1\) 的数,所以此时必选加。

\(a_i\neq 1\) 时,此时即 \(a_i\ge 2\),可以证明最多不会做超过 \(1\) 次加法操作。


证明:

假设加了两次,贡献为 \(b_i+b_j\)。而乘的贡献为 \(a_i\times a_j\),所以 \(b_i+b_j\le a_i\times a_j\)。加大于两次的情况是类似的。


所以就只有两种情况:加一次或零次即不加。

所以枚举在哪里加就好了。

注意过程中不要取模,最大是 \(10^{17}\) 级别的,long long 是够的。

代码

// Problem: P8365 [LNOI2022] 吃
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P8365
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// Date:2022-06-04 09:45
// 
// Powered by CP Editor (https://cpeditor.org)

#include <iostream>
#include <cstdio>
#include <climits>//need "INT_MAX","INT_MIN"
#include <cstring>//need "memset"
#include <numeric>
#include <algorithm>
#include <cmath>
#define int long long
#define enter putchar(10)
#define debug(c,que) std::cerr << #c << " = " << c << que
#define cek(c) puts(c)
#define blow(arr,st,ed,w) for(register int i = (st);i <= (ed); ++ i) std::cout << arr[i] << w;
#define speed_up() std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0)
#define mst(a,k) memset(a,k,sizeof(a))
#define stop return(0)
const int mod = 1e9 + 7;
inline int MOD(int x) {
	if(x < 0) x += mod;
	return x % mod;
}
namespace Newstd {
	inline int read() {
		int ret = 0,f = 0;char ch = getchar();
		while (!isdigit(ch)) {
			if(ch == '-') f = 1;
			ch = getchar();
		}
		while (isdigit(ch)) {
			ret = (ret << 3) + (ret << 1) + ch - 48;
			ch = getchar();
		}
		return f ? -ret : ret;
	}
	inline double double_read() {
		long long ret = 0,w = 1,aft = 0,dot = 0,num = 0;
		char ch = getchar();
		while (!isdigit(ch)) {
			if (ch == '-') w = -1;
			ch = getchar();
		}
		while (isdigit(ch) || ch == '.') {
			if (ch == '.') {
				dot = 1;
			} else if (dot == 0) {
				ret = (ret << 3) + (ret << 1) + ch - 48;
			} else {
				aft = (aft << 3) + (aft << 1) + ch - '0';
				num ++;
			}
			ch = getchar();
		}
		return (pow(0.1,num) * aft + ret) * w;
	}
	inline void write(int x) {
		if(x < 0) {
			putchar('-');
			x = -x;
		}
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
	}
}
using namespace Newstd;

const int N = 5e5 + 5;
struct Node {
	int mul,add;
	bool used;
} node[N];
int n;
#undef int
int main(void) {
	#define int long long
	n = read();
	for (register int i = 1;i <= n; ++ i) node[i].mul = read();
	for (register int i = 1;i <= n; ++ i) node[i].add = read();
	int ans = 1;
	for (register int i = 1;i <= n; ++ i) {
		if (node[i].mul == 1) {
			ans = ans + node[i].add;
			node[i].used = true;
		}
	}
	long double Max = ans;
	int pos = -1;
	for (register int i = 1;i <= n; ++ i) {
		if (!node[i].used) {
			long double p = (long double)(ans + node[i].add) / (long double)node[i].mul;
			if (p > Max) {
				Max = p;
				pos = i;
			}
		}
	}
	int res = ans;
	if (pos == -1) {
		for (register int i = 1;i <= n; ++ i) {
			if (!node[i].used) {
				res = res * node[i].mul % mod;
			}
		}
	} else {
		res = res + node[pos].add;
		for (register int i = 1;i <= n; ++ i) {
			if (!node[i].used && i != pos) {
				res = res * node[i].mul % mod;
			}
		}
	}
	printf("%lld\n",res);
	
	return 0;
}
posted @ 2022-06-04 11:47  Coros_Trusds  阅读(50)  评论(0编辑  收藏  举报