跳到正文

题解

题解:P17107 [入门赛 #49] 左右采撷

saka1985约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年7月16日
更新
2026年7月16日
篇幅
约 4 分钟
获赞
1
题目
P17107

版本

  1. v1当前

题解:P17107 [入门赛 #49] 左右采撷

题目传送门 。

比赛时没过,得了(骗了) 15 分, 515 分收场。

后来才知道除了二分还要枚举  qaq  ~~qaq~~。

还是觉得挺有难度了吧。

AC Code:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,k,w;
int f[1010][1010];
int x[20],y[20];
int tot;
int l,r;
int ans=-1;
bool check(int p){
    for(int t=0;t<pow(2,k);t++){
        bool vis[1010][1010];
        memset(vis,0,sizeof vis);
        int sum=0;
        for(int i=1;i<=k;i++) {
            int dx=x[i],dy=y[i];
            if(!(t&(1<<(i-1)))){
                for(int i=max(1LL,dx-p);i<=min(n,dx+p);i++){
                    if(!vis[i][dy]){
                        vis[i][dy]=1;
                        sum+=f[i][dy];
                        if(sum>=w){
                        	 return 1;
						}
                    }
                }
            }else{
                for(int j=max(1LL,dy-p);j<=min(m,dy+p);j++){
                    if(!vis[dx][j]){
                        vis[dx][j]=1;
                        sum+=f[dx][j];
                        if(sum>=w){
                        	return 1;
						}
                    }
                }
            }
        }
        if(sum>=w){
        	return 1;
		} 
    }
    return 0;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>k>>w;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>f[i][j];
            tot+=f[i][j];
        }
    }
    for(int i=1;i<=k;i++){
        cin>>x[i]>>y[i];
    }
    if(tot<w){
        cout <<"Impossible";
        return 0;
    }
    r=max(n,m);
    while(l<=r){
        int mid=(l+r)/2;
        if(check(mid)){
            ans=mid;
            r=mid-1;
        }else{
            l=mid+1;
        }
    }
    if(ans==-1){
        cout<<"Impossible";
    }else{
        cout<<ans;
    }
    return 0;
}

附:十分感谢 @K_add_HCL_add_F2大神 的善意帮助。

原文