loading

CF1927G Paint Charges

题意简述

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

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

\(n\le 100\)

\(O(n^3)\) 解法

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

\(f_{i,l,r}\) 表示使用前 \(i\) 个道具,最左端没覆盖到的点是 \(l\),最右端覆盖到的点是 \(r\) 的最小代价。

转移:

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

答案即为 \(f_{n,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(n^2)\) 解法

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

\(f_{i,j}\) 为使用前 \(i\) 种道具,覆盖了 \([1,j]\) 的最小代价。

首先转移显然有:

  • 若使用左覆盖,且满足 \(lft_i\le j+1,j<i\),则有 \(f_{i-1,j}+1\rightarrow f_{i,i}\)
  • 若使用右覆盖,且满足 \(j\ge i-1,j<rht_i\),则有 \(f_{i-1,j}+1\rightarrow f_{i,rht_i}\)

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

考虑再多一项转移:

\(i\) 要使用左覆盖时,枚举 \(j<i\) 要使用右覆盖,则有转移 \(f_{i,rht_j}\leftarrow f_{j-1,lft_i-1}+2\)

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

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

答案即为 \(f_{n,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
*/

posted @ 2024-02-12 20:51  dcytrl  阅读(49)  评论(0)    收藏  举报