zoj4062 Plants vs. Zombies 二分+模擬(貪心的思維)

題目傳送門

題目大意:有n個植物排成一排,標號為1-n,每株植物有自己的生長速度ai,每對植物澆一次水,該株植物就長高ai,現在機器人從第0個格子出發,每次走一步,不能停留,每一步澆一次水,總共可以走m步,問最矮的植物最高是多少。

思路:

  一般此類最小值最大問題都是二分,此題顯然也是可以二分植物的高度的。

  確定某一個高度后,也確定了每個植物需要澆幾次水,而對于一株植物來說,應當盡可能的在這株植物和后面那個格子來回走,是這株植物迅速超過最低高度(這樣的走法是最優的,因為可以想象,如果往后走很多步再走回來,中間浪費的可能性比較大),于是就是對n個植物模擬澆水,考慮一些細節就可以了(二分跳出條件,long long等等)

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
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*10+ch-'0';ch=getchar();}return x*f;}
const int maxn=100010;
int n;
ll a[maxn],c[maxn],m;
inline bool judge(ll high){CLR(c,0);ll temp=m;if(m==0)return false;c[1]=a[1],m--;int i=1;for(;i<=n;i++){if(m<=0)break;if(c[i]>=high){if(m>0){c[i+1]=a[i+1];m--;continue;}else{break;}}ll tmp=(ll)ceil((high-c[i])*1.0/a[i]);if(m>2*tmp){m-=2*tmp+1;c[i]+=a[i]*tmp;c[i+1]+=a[i+1]*(tmp+1);continue;}else if(m==2*tmp){c[i]+=a[i]*tmp;c[i+1]+=a[i+1]*tmp;break;}else{break;}}m=temp;for(i=1;i<=n;i++){if(c[i]<high)return false;}return true;
}
int main(){int t;cin>>t;while(t--){cin>>n>>m;ll l=0,r=0,mid,ans;for(int i=1;i<=n;i++){a[i]=read();r=max(r,a[i]*m);}if(m==0){printf("0\n");continue;}while(l<=r){mid=(l+r)>>1;//    printf("mid  %d\n",mid);if(judge(mid)){ans=mid;l=mid+1;}else{r=mid-1;}}printf("%lld\n",ans);}
} 
View Code
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 pixiv. ID: 21790160; Artist: socha)

There are??plants in DreamGrid's garden arranged in a line. From west to east, the plants are numbered from 1 to??and the?-th plant lies??meters to the east of DreamGrid's house. The?-th plant has a defense value of??and a growth speed of?. Initially,??for all?.

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?-th plant is at the robot's position, the robot will water the plant and??will be added to?. Because the water in the robot is limited, at most??steps can be done.

The defense value of the garden is defined as?. DreamGrid needs your help to maximize the garden's defense value and win the game.

Please note that:

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

Input

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

The first line contains two integers??and??(,?), indicating the number of plants and the maximum number of steps the robot can take.

The second line contains??integers??(), where??indicates the growth speed of the?-th plant.

It's guaranteed that the sum of??in all test cases will not exceed?.

Output

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

Sample Input

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

Sample Output

6
4

Hint

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}, so that we have??after the watering.

For the second test case, a candidate direction sequence is {E, E, E, E, W, E, W, E, W}, so that we have??after the watering.

轉載于:https://www.cnblogs.com/mountaink/p/9921988.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/396130.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/396130.shtml
英文地址,請注明出處:http://en.pswp.cn/news/396130.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MyBatis注解模式批量insert方法

2019獨角獸企業重金招聘Python工程師標準>>> 方法一:script標簽方式 Insert("<script>insert into xxx (channelId,siteId) " "values " "<foreach collection\"list\" item\"item\" index\"index\&quo…

尚硅谷學費有住宿么_我在12個小時的住宿期間了解到的硅谷知識

尚硅谷學費有住宿么by Sahil Khoja由Sahil Khoja 我在12個小時的住宿期間了解到的硅谷知識 (What I learned about Silicon Valley during my 12 hour stay) #1 Unless you’re a designer or a developer, the billboards are pure gibberish.&#xff03;1除非您是設計師或開…

以下屬于linux文件系統認為的文件是,信息安全技術題庫:在Linux系統中,圖形文件、數據文件、文檔文件等都屬于()。...

相關題目與解析Linux中圖像文件屬于()。A、文本文件B、連接文件C、特殊文件D、二進制文件主要用于Linux系統中進程間相互傳遞數據。A&#xff0e;FIFO文件B&#xff0e;設備文件C&#xff0e;鏈接文件D&#xff0e;目錄文件關于Linux文件組織方式的說法中&#xff0c;(32)是錯誤…

關于eclipse中文注釋亂碼的問題

今天打開了一個以前的android項目&#xff0c;發現中文注釋都成亂碼啦&#xff01;&#xff01;&#xff01; 后來在網上找了一會解決方法&#xff0c;知道了中文的編碼大體是兩種&#xff1a;GBK(漢字內碼擴展規范)和UTF-8(8-bit Unicode Transformation Format)。 因此問題的…

園林系統優秀黨員推薦材料_園林綠化公司黨員先進個人事跡材料

第1頁共5頁三一文庫(www.31doc.com)〔園林綠化公司黨員先進個人事跡材料〕我于年月踏出校門來到建設公司。初到公司&#xff0c;我被分配到分公司卉豐園林綠化公司工作。我努力學習公司各項規章制度和相關業務知識&#xff0c;多了解樹木、綠化的有關情況。在此期間&#xff0c…

python入門(5)使用文件編輯器編寫代碼并保存執行

python入門&#xff08;5&#xff09;使用文件編輯器編寫代碼并保存執行 兩款文本編輯器&#xff1a; 一個是Sublime Text&#xff0c;免費使用&#xff0c;但是不付費會彈出提示框&#xff1a; 一個是Notepad&#xff0c;免費使用&#xff0c;有中文界面&#xff1a; 請注意&…

js 獲取時間戳的方法

(new Date()).valueOf()1541569364658(new Date()).getTime()1541569372623Number(new Date())1541569386622 // 2019年1月23日補充 *除以1000得到的是Unix時間戳 // Math.floor(new Date().getTime() / 1000), // 當天// (new Date(new Date().setHours(0, 0, 0, 0)) / 1000) …

agpl限制了開源_不要限制您的開源項目的潛力

agpl限制了開源by Julien Danjou通過朱利安丹喬(Julien Danjou) 不要限制您的開源項目的潛力 (Don’t limit your open source project’s potential) During the OpenStack summit a few weeks ago, I had the chance to talk to some people about my experience on running…

linux 批量同步,多主機目錄到備份服務器批量同步腳本

為了方便同步多個主機的目錄到備份服務器&#xff0c;寫了如下腳本&#xff1a;#!/usr/bin/perluse strict;use File::Spec;use File::Basename;use File::Path;#設定存儲路徑my $storedir"/backup/";while(){chomp;my ($host,$s_path)split /\t/;my $project_namefi…

交流電的有效值rms值_交流電路的功率三角因數原來是這樣理解的

點擊“電工電氣學習”關注即可免費訂閱&#xff01;電工學習網&#xff1a;www.diangon.com關注電工學習網官方微信公眾號“電工電氣學習”&#xff0c;收獲更多經驗知識。交流電路中消耗的電能可以用直角三角形的三個邊來表示&#xff0c;通常稱為功率三角形我們在關于交流電路…

CSS3酷炫樣式集合

1、30種炫酷CSS鼠標滑過按鈕特效 2、CSS 變量實現炫酷鼠標懸浮效果 3、基于CSS3和jQuery實現跟隨鼠標方位的Hover特效 4、css3金屬質感登錄表單 4、CSS3動態下拉菜單 5、CSS3鼠標懸浮特效 轉載于:https://www.cnblogs.com/mankii/p/9922981.html

微信小程序工具篇

“工欲善其事必先利其器”&#xff0c;在開始新內容的學習之前&#xff0c;往往會對用哪個IDE開發而苦惱。因為自身硬件條件的限制&#xff08;公司給配的商務筆記本&#xff0c;真心的是中看不中用。也就是便攜這么個有點了&#xff09;。所以在選擇IDE方面&#xff0c;個人比…

NOIP2008 普及組T4 立體圖 解題報告-S.B.S.(施工未完成)

題目描述 小淵是個聰明的孩子&#xff0c;他經常會給周圍的小朋友們將寫自己認為有趣的內容。最近&#xff0c;他準備給小朋友們講解立體圖&#xff0c;請你幫他畫出立體圖。 小淵有一塊面積為m*n的矩形區域&#xff0c;上面有m*n個邊長為1的格子&#xff0c;每個格子上堆了一些…

及時溝通的重要性_溝通與代碼同樣重要

及時溝通的重要性by Andrea Goulet通過安德烈古萊特(Andrea Goulet) 溝通與代碼同樣重要 (Communication Is Just As Important As Code) This past weekend, I had the pleasure of being the closing keynote at Ruby Nation. I expanded on one of the core values at Corg…

linux telnet smtp,如何使用Telnet測試IMAP與SMTP

1 前言筆者有時候調試郵件服務器需要使用Telnet直接去操縱IMAP與SMTP的服務&#xff0c;所以整理此文。2 最佳實踐2.1 IMAP服務2.1.1 使用Telnet鏈接IMAP服務telnet imap.cmdschool.org 143信息顯示如下&#xff0c;Trying 113.96.209.109...Connected to imap.cmdschool.org.E…

圓柱體積怎么算立方公式_圓柱體積公式怎么算

圓柱的體積計算公式同仁實驗學校各年級組備課教師教案教案設計 課題 教學內容年級 六年級 科目 圓柱體積的計算公式數學教案類型新授P25 頁例 5 及補充例題&#xff0c;完成“做一做”及練習五第 1~3 題。授課人1、通過用切割拼合的方法借助長方體的體積公式推導出圓柱的體積公…

Python學習筆記7:函數對象及函數對象作參數

一、lambda函數比如&#xff1a;fun1 lambda x,y: x y print fun1(3,4)輸出&#xff1a;7lambda生成一個函數對象。該函數參數為x,y&#xff0c;返回值為xy。函數對象賦給func。func的調用與正常函數無異。上面的代碼等價于&#xff1a;def fun2(x, y):return x y二、函數作…

github 建立_建立在線社區:GitHub教師

github 建立by Gitter通過吉特 建立在線社區&#xff1a;GitHub教師 (Building Online Communities: GitHub Teacher) We talked to the GitHub Training team about the free GitHub courses they offer to both developers and non-developers, as well as about the commun…

廣數25i系統倒刀回刀m代碼_廣州數控系統GSK25i參數.pdf

GSK25i 銑床加工中心數控系統 使用手冊(第 3 分冊: 參數篇)在本使用手冊中&#xff0c;我們將盡力敘述各種與該系統操作相關的事項。限于篇幅限制及產品具體使用等原因&#xff0c;不可能對系統中所有不必做和/或不能做的操作進行詳細的敘述。因此&#xff0c;本使用手冊中沒有…

linux離線安裝rjava,無法在ubuntu系統上安裝rJava

我已經看過一些與此相關的帖子…但是所有建議的解決方案看起來似乎不工作….我在EC2實例中運行R&#xff0c;并運行以下命令來嘗試安裝rJava但無效…任何幫助將不勝感激。> install.packages("rJava")Installing package(s) into ‘/home/ubuntu/R/library’(as ‘…