【ZOJ3627】Treasure Hunt II

Title effect: Given a sequence of length N, the conventional two people from the point P, per unit time per unit person can move up to a maximum distance between the two can not exceed M, a total time T units seeking in a legal case, the two men can get the sequence right point and the maximum is.

Solution: analog + greedy
Consider first the case of the beginning, in the legal case certainly is an extension of the bigger the better, with a greedy here. Note that, if M is odd, the last step will have to discuss who is going to go. Then enumerate how many steps left to go, how many steps right away, according to statistics the answer to the rest of the time.

code show as below

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
typedef long long LL;

int n,m,t,p;
LL sum[maxn],ans;

void read_and_parse(){
    for(int i=1;i<=n;i++){
        scanf("%lld",&sum[i]);
        sum[i]+=sum[i-1];
    }
    scanf("%d%d",&m,&t);
    ans=0;
}
void getans(int l,int r,int rest){
    LL ret=0;
    for(int k=0;k<=rest;k++){ // left
        if(l-k<1)break;
        int lb=l-k,rb=min(n,max(r+rest-2*k,r));
        ret=max(ret,sum[rb]-sum[lb-1]);
    }
    for(int k=0;k<=rest;k++){ // right
        if(r+k>n)break;
        int rb=r+k,lb=max(1,min(l,l+2*k-rest));
        ret=max(ret,sum[rb]-sum[lb-1]);
    }
    ans=max(ans,ret);
}
void solve(){
    int lb=max(1,p-t),rb=min(n,p+t);
    if(rb-lb<=m){
        printf("%lld\n",sum[rb]-sum[lb-1]);
        return;
    }
    if(m&1){
        int l=max(1,p-m/2),r=min(n,p+m/2);
        int rest=t-m/2-1;
        getans(l,r+1,rest),getans(l-1,r,rest);
    }else{
        int l=max(1,p-m/2),r=min(n,p+m/2);
        int rest=t-m/2;
        getans(l,r,rest);
    }
    printf("%lld\n",ans);
}
int main(){
    while(scanf("%d%d",&n,&p)!=EOF){
        read_and_parse();
        solve();
    }
    return 0;
} 

Guess you like

Origin www.cnblogs.com/wzj-xhjbk/p/10928971.html