提交时间:2023-12-02 19:47:40
运行 ID: 23884
#include<bits/stdc++.h> using namespace std; #define int long long #define endl "\n" #define double long double int n,k,q; int s[5][200300]; int f[3010][3010]; pair<double,int> F[200300][5]; inline void chkmx(int &x,int y){ x=max(x,y); } inline void chkmx(pair<double,int> &x,pair<double,int> y){ x=max(x,y); } inline void BF(){ memset(f,-0x3f,sizeof(f)); f[1][0]=s[0][1]; for(int i=1;i<=n;i++) for(int j=0;j<=n;j++){ chkmx(f[i+1][j],f[i][j]); chkmx(f[i+1][j+1],f[i][j]-s[j%k][i+1]+s[(j+1)%k][i+1]); } while(q--){ int x; cin>>x; cout<<f[n][x-1]<<endl; } } inline void chk(double lim){ for(int i=1;i<=n;i++) for(int j=0;j<k;j++) F[i][j]={-1e19,0}; F[1][0]={(double)s[0][1],0}; for(int i=1;i<=n;i++) for(int j=0;j<k;j++){ chkmx(F[i+1][j],F[i][j]); chkmx(F[i+1][(j+1)%k],{F[i][j].first-s[j][i+1]+s[(j+1)%k][i+1]-lim,F[i][j].second+1}); } } inline void LG_WQS(){ while(q--){ int x; cin>>x; x--; int ed=x%k; double l=-1e14,r=1e14; while(r-l>1e-9){ double mid=(l+r)/2; chk(mid); if(F[n][ed].second<=x)r=mid; else l=mid+1e-9; } chk(l); cout<<(int)roundl(F[n][ed].first+l*x)<<endl;; } } signed main(){ ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); //freopen("s4.in","r",stdin); //freopen("division.in","r",stdin); //freopen("division.out","w",stdout); cin>>n>>k>>q; for(int i=0;i<k;i++) for(int j=1;j<=n;j++) cin>>s[i][j]; for(int i=0;i<k;i++) for(int j=n;j>=1;j--) s[i][j]+=s[i][j+1]; if(n<=3000)BF(); else LG_WQS(); cout.flush(); return 0; }