ZOJ 4062 Plants vs. Zombies(二分答案)

题目链接:Plants vs. Zombies

题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度。人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次。求m步走完后最低高度的植物最大高度为多少。

题解:明显二分答案的题目。check时从左往右遍历,贪心思路:把该位置满足同时给后面减少浇水次数,策略是该位置和后一个位置左右横跳,注意最后一个位置的时候,如果满足就不需要再跳过去。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 const int N=1e5+10;
 8 typedef long long ll;
 9 ll a[N],d[N],n,m;
10
11 bool check(ll k){
12     for(ll i=1;i<=n;i++) d[i]=0;
13     ll cnt=m;
14     for(ll i=1;i<=n;i++){
15         if(cnt==0){
16             if(i==n&&d[i]>=k) return true;
17             return false;
18         }
19         cnt--;
20         d[i]+=a[i];
21         if(d[i]<k){
22             ll tmp=k-d[i];
23             ll c=tmp/a[i];
24             if(tmp%a[i]!=0) c++;
25             if(cnt>=2*c){
26                 cnt-=2*c;
27                 d[i]+=(a[i]*c);
28                 d[i+1]+=(a[i+1])*c;
29             }
30             else return false;
31         }
32     }
33     return true;
34 }
35
36 int main(){
37     int t;
38     scanf("%d",&t);
39
40     while(t--){
41         ll ans=0;
42         scanf("%lld%lld",&n,&m);
43         for(ll i=1;i<=n;i++){
44             scanf("%lld",&a[i]);
45         }
46         ll l=0,r=1e17+100;
47         while(l<=r){
48             ll mid=(l+r)/2;
49             if(check(mid)) ans=max(ans,mid),l=mid+1;
50             else r=mid-1;
51         }
52         printf("%lld\n",ans);
53     }
54
55     return 0;
56 }

Code

原文地址:https://www.cnblogs.com/ehanla/p/9974960.html

时间: 2024-10-10 09:41:05

ZOJ 4062 Plants vs. Zombies(二分答案)的相关文章

2018 青岛ICPC区域赛E ZOJ 4062 Plants vs. Zombie(二分答案)

Plants vs. Zombies Time Limit: 2 Seconds      Memory Limit: 65536 KB BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies. Plants vs. Zombies(?) (Image from pi

ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ 个植物,编号为 $1 \sim n$,第 $i$ 个植物的位置在坐标 $i$,成长值为 $a_i$,初始防御值为 $d_i$. 现在有一辆小车从坐标 $0$ 出发,每次浇水操作必须是先走 $1$ 单位长度,然后再进行浇水,植物被浇一次水,防御值 $d_i+=a_i$. 现在知道,小车最多进行 $m

zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的植物最高是多少. 思路: 一般此类最小值最大问题都是二分,此题显然也是可以二分植物的高度的. 确定某一个高度后,也确定了每个植物需要浇几次水,而对于一株植物来说,应当尽可能的在这株植物和后面那个格子来回走,是这株植物迅速超过最低高度(这样的走法是最优的,因为可以想象,如果往后走很多步再

Plants vs. Zombies(二分好题+思维)

Plants vs. Zombies http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5819 BaoBao and DreamGrid are playing the game Plants vs. Zombies. In the game, DreamGrid grows plants to defend his garden against BaoBao's zombies. Plants vs. Zombies(?)(

【ZOJ4062】Plants vs. Zombies(二分)

题意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai, 现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走m步,问最矮的植物最高是多少. n<=1e6,sigma n<=1e7,0<=m<=1e12,1<=a[i]<=1e5 思路:7Y--你不死谁死 显然最小值最大可以二分,二分答案,最优的策略一定是从左到右依次左右横跳,直到当前格子不小于二分的值 一个致命的细节:最后一个如果在倒数第二个左

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

HDU3081Marriage Match II(二分答案+并查集+最大流SAP)经典

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2507    Accepted Submission(s): 856 Problem Description Presumably, you all have known the question of stable marriage match. A

Codeforce 371C Hamburgers (二分答案)

题目链接 Hamburgers 二分答案,贪心判断即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define REP(i,n) for(int i(0); i < (n); ++i) 6 #define LL long long 7 8 char str[1010]; 9 LL len; 10 LL b, c, s, nb, nc, ns, pb, pc, ps; 11 LL money; 12 13 bool