CF1927G Paint Charges

题意简述

你有 n 个道具,对于第 i 个道具,你可以选择覆盖 [iai+1,i][i,i+ai1],或者什么都不做。

求覆盖所有 1n 所需要的道具的最小数目。

n100

O(n3) 解法

首先明确一个事实:被一个或多个区间包含的区间,使用该区间对应的道具是没有用的。

fi,l,r 表示使用前 i 个道具,最左端没覆盖到的点是 l,最右端覆盖到的点是 r 的最小代价。

转移:

  • 以下令 lfti=max(iai+1,1),rhti=min(n,i+ai1)
  • 若不使用:fi1,l,rfi,l,r
  • 若使用左覆盖:
    • lftil,则 fi1,l,r+1fi,R+1,R,R=max(i,r)
      • 解释:在之前的某个位置 j(j<i) 使用右覆盖道具时已经将 [j,r] 全部覆盖掉了,最左端未覆盖的点就是 r+1。而转移到 R=max(i,r) 的目的,是为了判掉 r<i 的情况。
    • 否则,此次覆盖一定是没有用的,因为之后需要有 lftjl(j>i),此时 [lftj,j] 包含 [lfti,i]
  • 若使用右覆盖:
    • rhtir,显然没用。
    • rhti>r
      • l<i,此时覆盖不会波及最左端没覆盖到的点,fi1,l,r+1fi,l,rht
      • 否则,此次覆盖会将最左端没覆盖到的点也被覆盖,fi1,l,r+1fi,rht+1,rht
        • 解释:由于 lr 不一定满足(反例是当某前缀 [1,k] 全部恰好覆盖完时,l=k+1,r=k),所以之前的某个位置 j(j<i) 使用右覆盖道具时的覆盖区间 [j,r] 不一定波及 l

答案即为 fn,n+1,n,足以通过本题。

点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define lowb lower_bound
#define uppb upper_bound
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int 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-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=105,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,a[maxn];
int f[maxn][maxn][maxn];//左边第一个未覆盖,右边最后一个已覆盖 
void ckmx(int &x,int y){if(y<x)x=y;}
void solve_the_problem(){
	n=rd();rep(i,1,n)a[i]=rd();
	rep(i,0,n+1)rep(j,0,n+1)rep(k,0,n+1)f[i][j][k]=inf;
	f[0][1][0]=0;
	rep(i,1,n){
		int lft=max(i-a[i]+1,1),rht=min(i+a[i]-1,n);
		rep(l,0,n+1)rep(r,0,n){
			ckmx(f[i][l][r],f[i-1][l][r]);
			int rr=max(i,r);
			if(lft<=l)ckmx(f[i][rr+1][rr],f[i-1][l][r]+1);
			if(r<=rht){
				if(l<i)ckmx(f[i][l][rht],f[i-1][l][r]+1);
				else ckmx(f[i][rht+1][rht],f[i-1][l][r]+1);
			}
		}
	}
	write(f[n][n+1][n],10);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();while(_--)solve_the_problem();
}
/*

*/

O(n2) 解法

以下解法仅代表个人理解,不一定正确。

fi,j 为使用前 i 种道具,覆盖了 [1,j] 的最小代价。

首先转移显然有:

  • 若使用左覆盖,且满足 lftij+1,j<i,则有 fi1,j+1fi,i
  • 若使用右覆盖,且满足 ji1,j<rhti,则有 fi1,j+1fi,rhti

但也显然,这两种转移是无法表述 j<ij 使用右覆盖,i 使用左覆盖的情况。

考虑再多一项转移:

i 要使用左覆盖时,枚举 j<i 要使用右覆盖,则有转移 fi,rhtjfj1,lfti1+2

有没有可能最优解需要三个及以上区间都相交?

实际上,在上述情况下,若插入第三个区间,在保证三个区间两两有交的前提下,要么第三个区间被原先两个区间的并所包含,要么原先的两个区间之一会被第三个区间和另外一个区间的并包含。而我们会枚举所有的 j<i,那么第三个区间的贡献也会被算上,因此无需插入第三个区间。

答案即为 fn,n

点击查看代码
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define FlushIn fread(Fread::ibuf,1,1<<21,stdin)
#define FlushOut fwrite(Fwrite::obuf,1,Fwrite::S-Fwrite::obuf,stdout)
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define pii pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define lowb lower_bound
#define uppb upper_bound
#define rep(a,b,c) for(int a=(b);a<=(c);a++)
#define per(a,b,c) for(int a=(b);a>=(c);a--)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=d)
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=d)
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
//#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
bool smallingll(long long x,long long y){return x<y;}
namespace Fread {
	const int SIZE=1<<21;
	char ibuf[SIZE],*S,*T;
	inline char getc(){if(S==T){T=(S=ibuf)+fread(ibuf,1,SIZE,stdin);if(S==T)return '\n';}return *S++;}
}
namespace Fwrite{
	const int SIZE=1<<21;
	char obuf[SIZE],*S=obuf,*T=obuf+SIZE;
	inline void flush(){fwrite(obuf,1,S-obuf,stdout);S=obuf;}
	inline void putc(char c){*S++=c;if(S==T)flush();}
	struct NTR{~NTR(){flush();}}ztr;
}
/*#ifdef ONLINE_JUDGE
#define getchar Fread::getc
#define putchar Fwrite::putc
#endif*/
inline int rd(){
	int 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-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
	if(x<0){x=-x;putchar('-');}
	int y=0;char z[40];
	while(x||!y){z[y++]=x%10+48;x/=10;}
	while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=105,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n,a[maxn];
int f[maxn][maxn];
void ckmn(int &x,int y){if(y<x)x=y;}
void solve_the_problem(){
	n=rd();rep(i,1,n)a[i]=rd();
	rep(i,0,n)rep(j,0,n)f[i][j]=inf;
	f[0][0]=0;
	rep(i,1,n){
		int lft=max(1,i-a[i]+1),rht=min(i+a[i]-1,n);
		rep(j,0,n){
			ckmn(f[i][j],f[i-1][j]);
			if(lft<=j+1&&j<i)ckmn(f[i][i],f[i-1][j]+1);
			if(j>=i-1&&j<rht)ckmn(f[i][rht],f[i-1][j]+1);
		}
		rep(j,1,i-1){
			int Rht=min(j+a[j]-1,n);
			if(Rht>i&&lft<=j)ckmn(f[i][Rht],f[j-1][lft-1]+2);
		}
		per(j,n-1,0)ckmn(f[i][j],f[i][j+1]);
	}
	write(f[n][n],10);
}
bool Med;
signed main(){
//	freopen(".in","r",stdin);freopen(".out","w",stdout);
//	fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
	int _=rd();while(_--)solve_the_problem();
}
/*
1
2
1 1
*/

作者:dcytrl

出处:https://www.cnblogs.com/dcytrl/p/18014075

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   dcytrl  阅读(35)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示