首页 > 其他分享 >D. Yet Another Monster Fight

D. Yet Another Monster Fight

时间:2024-06-22 16:12:17浏览次数:5  
标签:check int max 3e5 mid Fight Another Yet maxx

cf链接

洛谷链接

方法一

最大最小值问题我们很容易想到二分答案法。那么我们如何写出check函数呢?

对于答案x,若x-i+1<a[i],则选定怪物一定不在 i 位置左侧,即L=i;

若x-n+i<a[i],则选定怪物一定不在 i 位置右侧,R=min(R,i)。

遍历数组,如果L<=R则答案符合题意;否则不符合。

code

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=3e5+5;
int a[N],n;
int check(int x)
{
    int l=0,r=n;
    for(int i=1;i<=n;i++)
    {
        if(x-a[i]+1<i) l=i;
        if(x-a[i]<n-i) r=min(r,i);
    }
    if(l<=r) return 1;
    return 0;
}
int main()
{
    int maxx=-1;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i],maxx=max(maxx,a[i]);
    int l=maxx-1,r=2e9,mid;
    while(l+1<r)
    {
        mid=l+(r-l>>1);
        if(check(mid)) r=mid;
        else l=mid;
    }
    cout<<r;
    return 0;
}

 

方法二

洛谷讨论区题解。

code

 

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int a[N],b[N],c[N];
int MAX(int a,int b,int c){
    a=max(a,b);
    a=max(a,c);
    return a;
}
int main(){
    int n;
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>a[i];
        b[i]=max(b[i-1],a[i]+n-i);
    }
    for (int i=n;i>=1;i--){
        c[i]=max(c[i+1],a[i]+i-1);
    }
    int ans=2e9;
    for (int i=1;i<=n;i++){
        ans=min(ans,MAX(a[i],b[i-1],c[i+1]));
    }
    cout<<ans;
    return 0;
}

 

标签:check,int,max,3e5,mid,Fight,Another,Yet,maxx
From: https://www.cnblogs.com/purple123/p/18262427

相关文章

  • MySQL The instance is already part of another Replication Group
    MySQLInnoDBCluster(测试环境为MySQL8.0.35)将一个实例重新加入集群时,遇到了下面这个错误"Theinstance'dbu03:3306'isalreadypartofanotherReplicationGroup"MySQL  10.160.2.55:3306 ssl  JS > cluster.addInstance('[email protected]:3306')ERROR: Ru......
  • GD32学习中遇到 warning: #188-D: enumerated type mixed with another type 强迫症尽
    项目场景:今天往GD32的系统板里加入六个按键,在DEMO程序的基础上要做一些修改。在对时钟使能的时候,习惯的用STM32的方法。加|线隔开两个GPIO口,结果报出warning:#188-D:enumeratedtypemixedwithanothertype的警告。强迫症尽量不要有警告……rcu_periph_clock_enable(RC......
  • CF1234F Yet Another Substring Reverse
    CF1234FYetAnotherSubstringReverse状压dp+高维前缀和一个很显然的发现是最长子串长度不会超过字符集。那么如果没有这个操作,是很简单的,我们看看多了这个操作意味着什么。对于一个子串,考虑一次翻转对它的影响。在它内部的翻转肯定是没有意义的;我们一定有一个操作能将任意......
  • git 命令报错:Another git process seems to be running in this repository, e.g. an
    执行git命令时,报错:Anothergitprocessseemstoberunninginthisrepository,e.g.aneditoropenedby'gitcommit'.Pleasemakesureallprocessesareterminatedthentryagain.Ifitstillfails,agitprocessmayhavecrashedinthisrepository......
  • SystemC & TLM-2.0 - TLM-2.0 Interoperability in SyetemC
    TML-2.0Interoperabilityabouttellingtointeroperabilitylet'sabouttellingtointeroperabilitylet'sstartbydefiningsometermsuntilthemtoaninitiatorisacomponentthatinitiatesnewtransactionsatargetisacomponentbutactsast......
  • CF1228E Another Filling the Grid 题解
    tag:容斥原题+组合数设F[i]F[i]F[i]表示至少......
  • Another Filling the Grid
    AnotherFillingtheGrid题目信息题目描述Youhave$n\timesn$squaregridandaninteger$k$.Putanintegerineachcellwhilesatisfyingtheconditionsbelow.Allnumbersinthegridshouldbebetween$1$and$k$inclusive.Minimumnumberofth......
  • Engage with world in another way, Strench myself. dataism已经进入房间, 等待历史
    忘记历史,你就不会被历史所羁绊,你看到的每一天都是全新的。engagewithyourlife,而不是藏在生活的后面,liveinyourlife,notbehindoraboveyourlife,notpretenttolive,justliveinit.体现物体特性的其实是分子,而不是原子。虽然游离态的原子更自由,但是原子性质更单......
  • 【ubuntu】22.04安装Redis Insight及AnotherRedisDesktopManager
    一、RedisInsight1、官网下载https://redis.io/insight/#insight-form  2、安装sudodpkg-iRedisInsight-linux-amd64.deb 3、运行  二、AnotherRedisDesktopManager1、官网下载https://github.com/qishibo/AnotherRedisDesktopManager/releases/tag/v1.......
  • D - Another Sigma Problem
    D-AnotherSigmaProblemhttps://atcoder.jp/contests/abc353/tasks/abc353_d 思路前缀和+快速幂https://zhuanlan.zhihu.com/p/697255076 Codehttps://atcoder.jp/contests/abc353/submissions/53514365typedeflonglongll;llpow(llx,lln){if(n==......