UVa 10827 Maximum sum on a torus

题目

UVa 10827 Maximum sum on a torus

题解

看了看提交记录里好像10ms的挺少,说明大家都是n^4乱搞?那我也许可以说说n^3做法。这题不过就是在环面上而已,如果不在环上的话,n^3做法应该都知道,不知道请百度最大子矩阵和。这里增加了一点情况而已。
枚举两行i1,i2,先考虑夹在i1,i2之间的行的选择,变成了一维序列问题,考虑对于每个位置i,有两种决策,此处sum表示序列前缀和。

ans1=sum[i]min(sum[j])(0<=j<i)
ans2=sum[n](sum[i]max(sum[j]))(0<=j<i)

第二种情况是环面特殊情况。

对于行来说,环面的影响不过就是你可以把1~i1和i2~n合成一个序列再做一次而已。另外注意在i==n时上述第二种情况不能选,可以认为是由于此时它不可能是环面上才有的情况,所以被第一种统计过,而且如果此时j为0的话,会选出空序列。

代码

//QWsin
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,x) for(int i=x;i<=n;++i)
using namespace std;
const int INF=(1<<30);
const int maxn=100;

int n,mx[maxn][maxn],suml[maxn][maxn];

int sum[maxn];
inline int cal(int a,int b,int kind)//kind表示计算方式,0表示计算夹在中间的答案,1表示非中间的答案
{
    rep(i,1) {
        sum[i]=suml[i][b]-suml[i][a-1];
        if(kind) sum[i]=suml[i][n]-sum[i];
        sum[i]+=sum[i-1];
    }
    int maxv=0,minv=0,ret=-INF;
    rep(i,1){
        ret=max(ret,sum[i]-minv);
        if(i!=n) ret=max(ret,sum[n]-(sum[i]-maxv));
        maxv=max(maxv,sum[i]);minv=min(minv,sum[i]);
    }
    return ret;
}

inline void solve()
{
    cin>>n;
    rep(i,1) rep(j,1){
        scanf("%d",mx[i]+j);
        suml[j][i]=suml[j][i-1]+mx[i][j];
    }

    int ans=-INF;
    rep(i,1) rep(j,i) 
    {
        ans=max(ans,cal(i,j,0));
        if(!(i==1&&j==n)) ans=max(ans,cal(i,j,1));
    }
    printf("%d\n",ans);
}

int main()
{
    int T;cin>>T;
    while(T--) solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
extern UFUNEXPORT int UF_MODL_ask_face_data( tag_t face ,/* <I> Face obj_id / int * type ,/ <O> Face type is NX surface type code 16 = cylinder 17 = cone 18 = sphere 19 = revolved (toroidal) 20 = extruded 22 = bounded plane 23 = fillet (blend) 43 = b-surface 65 = offset surface 66 = foreign surface / double point[] ,/ <O,len:3> Point information is returned according to the value of type as follows. Plane = Position in plane Cylinder= Position on axis Cone = Position on axis Sphere = Center position Torus = Center position Revolved = Position on axis / double dir[] ,/ <O,len:3> Direction information is returned according to the value of type as follows. Plane = Normal direction Cylinder= Axis direction Cone = Axis direction Torus = Axis direction Revolved = Axis direction / double box[] ,/ <O,len:6> Face boundary. The coordinates of the opposite corners of a rectangular box with sides parallel to X, Y, and Z axes (Absolute Coordinate System) are returned. The box contains the specified face and is usually close to the minimum possible size, but this is not guaranteed. box[0] = Xmin box[1] = Ymin box[2] = Zmin box[3] = Xmax box[4] = Ymax box[5] = Zmax / double * radius ,/ <O> Face major radius: For a cone, the radius is taken at the point[3] position on the axis. For a torus, the radius is taken at the major axis. / double * rad_data ,/ <O> Face minor radius: only a torus or cone has rad_data as a minor radius. For a cone, rad_data is the half angle in radians. For a torus, rad_data is taken at the minor axis. / int * norm_dir / <O> Face normal direction: +1 if the face normal is in the same direction as the surface normal (cross product of the U- and V-derivative vectors), -1 if reversed. */ ); 详解
最新发布
06-01

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值