首页 > 其他分享 >[atAGC062D]Walk Around Neighborhood

[atAGC062D]Walk Around Neighborhood

时间:2023-06-30 09:12:33浏览次数:42  
标签:begin le Neighborhood Around sum Walk ge end cases

记\(D=\max_{1\le i\le n}d_{i}\),则无解当且仅当\(2D>\sum_{i=1}^{n}d_{i}\)

结论:\(\forall (x,y),\exists (X,Y),\begin{cases}|X|+|Y|=R\\|x-X|+|y-Y|=d\end{cases}\) 当且仅当\(|r-R|\le d\le r+R\)(其中\(r=|x|+|y|\))

必要性:根据\(|a|-|b|\le |a-b|\le |a|+|b|\),显然成立

充分性:根据对称性,不妨假设\(x,y\ge 0\)

  • 若\(d=r+R\),则取\((X,Y)=(-R,0)\)即可
  • 若\(d=|r-R|\),则取\((X,Y)=\begin{cases}(x+(R-r),y)&r\le R\\(x-(r-R),y)&r>R\and r-R\le x\\(0,y-((r-R)-x))&r>R\and r-R>x\end{cases}\) 即可

由于\(\{(X,Y)\mid |X|+|Y|=R\}\)构成正方形,进而\(|x-X|+|y-Y|\)的取值构成区间,即得证

根据结论,问题即转化为以下形式:

选择排列\(d_{i}\)和\(r_{i}\),要求\(\begin{cases}r_{0}=r_{n}=0\\\forall i\in [1,n],|r_{i-1}-r_{i}|\le d_{i}\le r_{i-1}+r_{i}\end{cases}\),并最小化\(\max_{1\le i\le n}r_{i}\)

二分枚举答案\(s\),显然有解的必要条件为\(s\ge \frac{D}{2}\)

当确定\(d_{i}\)后,每个\(r_{i}\)的取值构成区间,转移为\([x,y]\rightarrow [\min_{x\le i\le y}|i-d|,\min(y+d,s)]\)

  • 对于\(d\le s\),必然可以转移,且\(y=s\)时转移后为\([\max(x-s,0),y]\)
  • 对于\(d>s\),转移时需有\(y\ge d-s\),且转移后为\([d-y,s]\)

取\(a,b\)分别为最小/最大的\(i\)满足\(d_{i}>s\),则应有\(\begin{cases}\sum_{i=1}^{a-1}d_{i}\ge d_{a}-s\\\sum_{i=b+1}^{n}d_{i}\ge d_{b}-s\end{cases}\),且其余\(d_{i}\)均存在合适的位置

可以贪心取\(d_{a},d_{b}\)为\(d_{i}>s\)的最、次小值(若唯一则均为\(D\)),即转化为对\(d_{i}\le s\)的背包

具体的,记\(S=\sum_{d_{i}\le s}d_{i}\),即判断是否存在子集和\(\in [d_{a}-s,S-(d_{b}-s)]\)

当区间长度\(\ge s\)时必然有解,进而\(S-(d_{b}-s)-(d_{a}-s)+1<s\),放缩得\(S\le 2D\)

根据经典结论,不同的\(d_{i}\)个数为\(O(\sqrt{D})\),并可以利用加入过程将二分改为枚举,时间复杂度为\(O(D\sqrt{D})\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;
int n,s,ans,a[N],cnt[N],g[N<<1],f[N<<1];
ll sum;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		cnt[a[i]]++;
	}
	sort(a+1,a+n+1);
	for(int i=1;i<n;i++)sum+=a[i];
	if (sum<a[n]){
		puts("-1");
		return 0;
	}
	s=sum=0,ans=a[n],f[0]=1;
	for(int i=1;i<a[n];i++){
		if (cnt[i]){
			s+=cnt[i];
			if (sum<=(a[n]<<1)){
				for(int j=sum;j;j--)f[j]-=f[j-1];
			}
			sum+=(ll)i*cnt[i];
			if (sum<=(a[n]<<1)){
				int s=i*(cnt[i]+1);
				for(int j=0;j<=sum;j++)g[j]=f[j]+(j<i ? 0 : g[j-i]);
				for(int j=0;j<=sum;j++)f[j]=(g[j]>(j<s ? 0 : g[j-s]));
				for(int j=1;j<=sum;j++)f[j]+=f[j-1];
			}
		}
		if (i>=(a[n]>>1)){
			if (sum>(a[n]<<1)){ans=i;break;}
			int x=a[s+1]-i,y=sum-(a[min(s+2,n)]-i);
			if ((x<=y)&&(f[y]>f[x-1])){ans=i;break;}
		}
	}
	printf("%d\n",ans);
	return 0;
}

标签:begin,le,Neighborhood,Around,sum,Walk,ge,end,cases
From: https://www.cnblogs.com/PYWBKTDA/p/17515698.html

相关文章

  • Codeforces[CF1036B]Diagonal Walking v.2题解
    题目大意很明显,这道题就是求k步之内到达点\((a,b)\),然后尽量走对角线,求能走对角线的最大值。做题思路首先明白一个事实,即一个对角线可以通过增加一步而抵达点不变,如图:我们可以这样思考这道题,在到达目的地以后,剩余步数如果为双数,则在对角线来回走,最后会到目的地。但如果剩......
  • 分布式链路追踪Skywalking
    简介skywalkings是2015年开源的一款国产框架,2017年的时候加入了Apache孵化器。skywalking是分布式应用程序的性能监控工具,具有多种监控手段,作为APM工具,它具有分布式追踪、性能指标分析、应用和服务依赖分析等功能。可以通过语言探针来获取监控数据。专门是为了微服务(springc......
  • 【阅读笔记】Anchored Neighborhood Regression for Fast Example-Based uper Resolut
    论文信息[AnchoredNeighborhoodRegressionforFastExample-BaseduperResolution]-TIMOFTER,2013,IEEEInternationalConferenceonComputerVision前置内容邻域嵌入(NeighborEmbedding,NE)是“样本-样本”映射,在训练样本中寻找测试样本的相似邻居特征样本,计算量略大。......
  • 部署SkyWalking
    SkyWalking部署说明二进制包部署1、下载地址https://dlcdn.apache.org/skywalking/9.4.0/apache-skywalking-apm-9.4.0.tar.gz#下载有点慢https://www.oracle.com/java/technologies/downloads/#license-lightbox#需要jdk11环境最终需要下面2个包2、安装jdk_11#tarzxvfjdk-11......
  • How to work around rustup-init failure
    Howtoworkaroundrustup-initfailure(JinQing’sColumn,Mar.,2022)rustup-init.exemayfailifsomeanti-virussoftwareisrunningwithrealtimeprotection.Theerrormessageislikethisaftermanyretries:error:couldnotrenamecomponentfilefrom�......
  • Walkthrough-digitalworld.local: BRAVERY
    0x01环境靶机地址:https://www.vulnhub.com/entry/digitalworldlocal-bravery,281/0x02过程1.信息收集┌──(root㉿kali)-[/home/kali/Desktop/oscp]└─#netdiscover-r192.168.60.0/24Currentlyscanning:Finished!|ScreenView:UniqueHosts......
  • SkyWalking分布式链路追踪工具的基本使用
    下载我们需要一个监控中心,还有一个javaagents工具apache-skywalking-apm(显示/存储多个程序的指标数据),APM是ApplicationPerformanceManagement的缩写和skywalking-agent(收集单个程序的指标数据)启动Skywalking和java程序apache-skywalking-apm\bin\startup.bat......
  • Walkthrough-hackme 1
    0x01环境靶机地址:https://www.vulnhub.com/entry/hackme-1,330/0x02过程1.信息收集┌──(root㉿kali)-[/home/kali]└─#netdiscover-r192.168.60.0/24Currentlyscanning:Finished!|ScreenView:UniqueHosts......
  • android: workaround for slow ‘building workspace’ problem in eclipse
    Whiledevelopingforandroidoneclipse3.6ihadtheproblemthateachtimeisavedafile,eclipseblockedmeseveralsecondswith‘buildingworkspace…’.Similartothese:stackoverflow–android-compilation-is-slow-using-eclipsestackoverflow–android-......
  • Walkthrough-SolidState 1
    0x01环境靶机地址:https://www.vulnhub.com/entry/solidstate-1,261/0x02过程1.信息收集┌──(root㉿kali)-[/home/kali/Desktop/oscp]└─#netdiscover-r192.168.60.0/24Currentlyscanning:Finished!|ScreenView:UniqueHosts......