P9889 [ICPC2018 Qingdao R] Plants vs. Zombies 题解 二分+贪心

news2024/10/26 5:21:08

[ICPC2018 Qingdao R] Plants vs. Zombies

传送门

题面翻译

给定 n n n 个植物和 m m m 的步数限制,每个植物在位置 1 … n 1\dots n 1n 上。你初始时在位置 0 0 0,每次可以移动到相邻的位置上。

每次设你走完一步后到达的位置是 i i i,则会使得这个位置的植物的高度增加 a i a_i ai。设 d i d_i di 为走完 m m m 步后位置 i i i 的植物高度,求出一个最优的走法使得 min ⁡ 1 ≤ i ≤ n d i \min\limits_{1 \le i \le n} d_i 1inmindi 最大。

2 ≤ n ≤ 1 0 5 2\leq n\leq 10 ^ 5 2n105 0 ≤ m ≤ 1 0 12 0\leq m\leq 10 ^ {12} 0m1012 1 ≤ a i ≤ 1 0 5 1\leq a_i\leq 10 ^ 5 1ai105 ∑ n ≤ 1 0 6 \sum n\leq 10 ^ 6 n106

题目描述

BaoBao and DreamGrid are playing the game Plants   vs.   Zombies \textit{Plants vs. Zombies} Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao’s zombies.

Plants   vs.   Zombies(?) (Image   from   pixiv.   ID:   21790160;   Artist:   socha) \textit{Plants vs. Zombies(?)} \\ \textit{(Image from pixiv. ID: 21790160; Artist: socha)} Plants vs. Zombies(?)(Image from pixiv. ID: 21790160; Artist: socha)

There are n n n plants in DreamGrid’s garden arranged in a line. From west to east, the plants are numbered from 1 to n n n and the i i i-th plant lies i i i meters to the east of DreamGrid’s house. The i i i-th plant has a defense value of d i d_i di and a growth speed of a i a_i ai. Initially, d i = 0 d_i = 0 di=0 for all 1 ≤ i ≤ n 1 \le i \le n 1in.

DreamGrid uses a robot to water the plants. The robot is in his house initially. In one step of watering, DreamGrid will choose a direction (east or west) and the robot moves exactly 1 meter along the direction. After moving, if the i i i-th plant is at the robot’s position, the robot will water the plant and a i a_i ai will be added to d i d_i di. Because the water in the robot is limited, at most m m m steps can be done.

The defense value of the garden is defined as min ⁡ { d i ∣ 1 ≤ i ≤ n } \min\{d_i | 1 \le i \le n\} min{di∣1in}. DreamGrid needs your help to maximize the garden’s defense value and win the game.

  • Each time the robot MUST move before watering a plant;
  • It’s OK for the robot to move more than n n n meters to the east away from the house, or move back into the house, or even move to the west of the house.

输入格式

There are multiple test cases. The first line of the input contains an integer T T T, indicating the number of test cases. For each test case:

The first line contains two integers n n n and m m m ( 2 ≤ n ≤ 1 0 5 2 \le n \le 10^5 2n105, 0 ≤ m ≤ 1 0 12 0 \le m \le 10^{12} 0m1012), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains n n n integers a 1 , a 2 , . . . , a n a_1, a_2, ... , a_n a1,a2,...,an ( 1 ≤ a i ≤ 1 0 5 1 \le a_i \le 10^5 1ai105), where a i a_i ai indicates the growth speed of the i i i-th plant.

It’s guaranteed that the sum of n n n in all test cases will not exceed 1 0 6 10^6 106.

输出格式

For each test case output one line containing one integer, indicating the maximum defense value of the garden DreamGrid can get.

样例 #1

样例输入 #1

2
4 8
3 2 6 6
3 9
10 10 1

样例输出 #1

6
4

提示

In the explanation below, E indicates that the robot moves exactly 1 meter to the east from his current position, and W indicates that the robot moves exactly 1 meter to the west from his current position.

For the first test case, a candidate direction sequence is { E , E , W , E , E , W , E , E } \{E, E, W, E, E, W, E, E\} {E,E,W,E,E,W,E,E}, so that we have d = { 6 , 6 , 12 , 6 } d = \{6,6,12,6\} d={6,6,12,6} after the watering.

For the second test case, a candidate direction sequence is { E , E , E , E , W , E , W , E , W } \{E, E, E, E, W, E, W, E, W\} {E,E,E,E,W,E,W,E,W}, so that we have d = { 10 , 10 , 4 } d = \{10, 10, 4\} d={10,10,4} after the watering.

注明

以上来自洛谷。 以上来自洛谷。 以上来自洛谷。

解题思路

题目中让我们求最小值最大,并具有单调性,所以可以二分答案。对于二分答案,这里不细讲。

以下为验证函数的写法:

  • 先让一个数组 T o n g Tong Tong 满足 T o n g i × d i ≥ m i d d l e Tong_i ×d_i \ge middle Tongi×dimiddle
  • 对于 T o n g i ≥ 1 Tong_i \ge 1 Tongi1 都需要在 T o n g i Tong_i Tongi T o n g i + 1 Tong_{i+1} Tongi+1 之间走 2 × T o n g i − 1 2\times Tong_i −1 2×Tongi1 次才能满足条件,而 T o n g i + 1 Tong_{i+1} Tongi+1 项也要去掉 T o n g i − 1 Tong_i −1 Tongi1 次;
  • 若走的总次数 C o u n t Count Count 满足 C o u n t ≥ m Count \ge m Countm了,就返回假,否则为真。

附上贪心证明(Ta人博客,尽量点赞)。

AC Code

#include<bits/stdc++.h>
using namespace std;
char buf[1048576], *p1, *p2;
template<typename T>inline void Super_Quick_Read(T &x) {
	bool f = 1;
	x = 0;
	char ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = !f;
		ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	}
	while (ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + (ch ^ 48), ch = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++);
	x = (f ? x : -x);
	return;
}
template<typename T>inline void Quick_Write(T x) {
	if (x < 0) putchar('-'), x = -x;
	if (x > 9) Quick_Write(x / 10);
	putchar(x % 10 + '0');
	return;
}
int n;
long long m, a[100001];
long long Tong[100001];
long long Answer;
inline bool Check(long long x) {
  long long _count = 0;
  for (register int i = 1; i <= n; ++i) Tong[i] = ceil(1.0 * x / a[i]);
  for (register int i = 1; i <= n; ++i) {
    if (Tong[i] <= 0) {
      ++_count;
      continue;
    }
    _count += Tong[i] * 2 - 1, Tong[i + 1] -= Tong[i] - 1;
    if (_count > m) return 0;
  }
  return 1;
}
signed main() {
  int T;
  Super_Quick_Read(T);
  while (T--) {
    Super_Quick_Read(n), Super_Quick_Read(m);
    for (register int i = 1; i <= n; ++i) Super_Quick_Read(a[i]);
    long long left = 0, right = LONG_LONG_MAX, middle;
    while (left <= right) {
      middle = (left + right) >> 1;
      if (Check(middle)) left = middle + 1, Answer = middle;
      else right = middle - 1;
    }
    Quick_Write(Answer), puts("");
  }
  return 0;
}

知道复制代码会 CE,为什么不自己写呢?

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1497305.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LeetCode每日一题之 快乐数

目录 题目介绍&#xff1a; 算法原理&#xff1a; 鸽巢原理&#xff1a; 如何找到环里元素&#xff1a; 代码实现&#xff1a; 题目介绍&#xff1a; 题目链接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 算法原理&#xff1a; 我先简单举两个例子&#xff…

最新的前端开发技术(2024年)

关于作者&#xff1a; 还是大剑师兰特&#xff1a;曾是美国某知名大学计算机专业研究生&#xff0c;现为航空航海领域高级前端工程师&#xff1b;CSDN知名博主&#xff0c;GIS领域优质创作者&#xff0c;深耕openlayers、leaflet、mapbox、cesium&#xff0c;canvas&#xff0…

如何将虚拟机设置成固定IP

问题描述&#xff1a; 在VMware虚拟机上部署的项目ip地址和数据库ip地址发生变动&#xff0c;导致mysql,nginx,redis等无法访问&#xff0c;要改配置又特别麻烦&#xff0c;而且下次可能还会变动。 解决方法&#xff1a; 将虚拟机ip地址配置成固定ip 关闭虚拟机&#xff0c;找…

ai数字人虚拟直播:AI大模型带给你不一样的体验

AI数字人虚拟直播&#xff0c;这一新兴的科技形式&#xff0c;正逐渐融入人们的生活之中。通过AI大模型的技术支持&#xff0c;数字人可以实现高度仿真的互动体验&#xff0c;让观众感受到前所未有的沉浸式乐趣。 数字人虚拟直播的魅力在于其超越了传统直播形式的局限性&#…

R语言lavaan结构方程模型在复杂网络分析中的科研技术新趋势

此外&#xff0c;我们还将深入探讨R语言的基础知识、结构方程模型的基本原理、lavaan程序包的使用方法等内容。无论是潜变量分析、复合变量分析&#xff0c;还是非线性/非正态/缺失数据处理、分类变量分析、分组数据处理等复杂问题&#xff0c;我们都将一一为您解析。 希望通过…

谷粒商城实战(002 oss 文件储存系统)

Java项目《谷粒商城》架构师级Java项目实战&#xff0c;对标阿里P6-P7&#xff0c;全网最强 总时长 104:45:00 共408P 此文章包含第61p-第p101的内容 简介 P61 服务器 对象存储服务 OSS 也可以用minio 三种上传方式 推荐第三种 1.过服务器 安全 但是占用性能 2.不过服…

Kubernetes-3

Kubernetes学习第3天 Kubernetes-31、查看实时的cpu和内存消耗1.1、kubectl top node 2、卷的使用2.1、什么是卷&#xff1f;1. 解决数据持久性问题2. Kubernetes 中的卷抽象概念3. 共享数据示例4. Kubernetes 中的卷使用5. 不同类型的卷6. 灵活、可靠的数据管理 2.2、联想到do…

前端每日一练 :相邻元素、嵌套元素Margin 塌陷、合并问题如何额解决?

相邻元素外边距塌陷合并 表现示例 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</t…

旧物回收小程序开发:环保与科技的创新结合

随着科技的飞速发展&#xff0c;我们的日常生活越来越离不开手机应用程序。而在环保日益成为社会焦点的今天&#xff0c;如何将科技与环保相结合&#xff0c;成为了一个值得深思的问题。今天&#xff0c;我们将探讨旧物回收小程序的开发&#xff0c;它如何助力环保&#xff0c;…

nginx代理访问Kuboard, 解决日志无法查看问题

错误方式 这种代理方式在点击追踪日志按钮, 会无法查看日志, 因为日志是通过weboscket传输 worker_processes 1; #设置 Nginx 启动的工作进程数为 1。events {worker_connections 1024; ##设置每个工作进程的最大并发连接数为 1024。 }http {include mime.types; #该…

社区店选址案例研究:成功与失败的经验教训

大家好&#xff0c;我是一名鲜奶吧5年的创业者&#xff0c;在社区店经营方面有着丰富的经验。 今天&#xff0c;我将分享一些关于社区店选址的成功与失败案例&#xff0c;希望能给想开实体店或创业的朋友们提供有价值的干货信息。 首先&#xff0c;让我们来看看成功的社区店选…

【Leetcode每日一题】 前缀和 - 除自身以外数组的乘积(难度⭐⭐)(27)

1. 题目解析 题目链接&#xff1a;238. 除自身以外数组的乘积 这个问题的理解其实相当简单&#xff0c;只需看一下示例&#xff0c;基本就能明白其含义了。 核心在于计算题目所给数组除本身外其他元素的积的数组返回即可。 2. 算法原理 为了计算每个位置i的最终结果ret[i]&…

鸿蒙Harmony应用开发—ArkTS声明式开发(基础组件:AlphabetIndexer)

可以与容器组件联动用于按逻辑结构快速定位容器显示区域的组件。 说明&#xff1a; 该组件从API Version 7开始支持。后续版本如有新增内容&#xff0c;则采用上角标单独标记该内容的起始版本。 子组件 无 接口 AlphabetIndexer(value: {arrayValue: Array<string>, s…

python_读取txt文件绘制多条曲线

读取txt文件&#xff0c;将匹配条件的多列绘制成曲线展示&#xff1a; import matplotlib.pyplot as plt import re from datetime import datetime from pylab import mplmpl.rcParams["font.sans-serif"] ["SimHei"] # 设置显示中文字体 mpl.rcParam…

uniapp+node.js前后端做帖子模块:发布帖子评论(社区管理平台的小程序)

目录 0前提1.一些准备1.1表帖子表 post帖子评论表 postComment 1.2总体思路 2.前端3.后端4.验证结果 &#x1f44d; 点赞&#xff0c;你的认可是我创作的动力&#xff01; ⭐️ 收藏&#xff0c;你的青睐是我努力的方向&#xff01; ✏️ 评论&#xff0c;你的意见是我进步的…

还为没有本科学历发愁?2024年成人学历提升最全攻略

如果你因为学历低而处处不顺的时候&#xff0c; 请记着&#xff0c; 我们还有提升学历的机会&#xff01; 成人想提升学历有三大方式&#xff1a;成人高考&#xff0c;自学考试&#xff0c;开放大学。 通过这几种途径获得的文凭都是国家承认&#xff0c;学信网可查的&#x…

刷题日记:面试经典 150 题 DAY5

刷题日记&#xff1a;面试经典 150 题 DAY4 125. 验证回文串28. 找出字符串中第一个匹配项的下标151. 反转字符串中的单词6. Z 字形变换68. 文本左右对齐 125. 验证回文串 原题链接 125. 验证回文串 双指针&#xff0c;一前一后&#xff0c;遇到非数字字母跳过即可 class So…

【机器学习】【决策树】分类树|回归树学习笔记总结

决策树算法概述 基本概念 决策树&#xff1a;从根节点开始一步步走到叶子节点&#xff0c;每一步都是决策过程 对于判断的先后顺序把控特别严格 一旦将判断顺序进行变化则最终的结果将可能发生改变 往往将分类效果较佳的判断条件放在前面&#xff0c;即先初略分在进行细节分…

MySQL学习Day24—数据库的设计规范

一、数据库设计的重要性: 1.糟糕的数据库设计产生的问题: (1)数据冗余、信息重复、存储空间浪费 (2)数据更新、插入、删除的异常 (3)无法正确表示信息 (4)丢失有效信息 (5)程序性能差 2.良好的数据库设计有以下优点: (1)节省数据的存储空间 (2)能够保证数据的完整性 …

Jmeter将接口查询结果列表按顺序赋值给各线程

Jmeter做性能测试会遇到这么一个场景&#xff1a;后面的请求需要根据前面的查询列表结果通过正则表达式提取器取值后赋值&#xff0c;而后面用户的赋须是唯一的&#xff0c;此值必时该如何做&#xff1f; 只需要把前面的结果保存到一个数组变量中&#xff0c;后面的用户&#…