2016年10月20日 星期四

TIOJ 1277 - 俄羅斯娃娃-續

http://tioj.infor.org/problems/1277

枚舉上下界 $O(n^2)$
壓縮起來變成一維 然後再 $O(n)$ 跑過去求max就好了



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
#define ll long long
#define ld long double
#define PB(x) push_back(x)
#define MP(x,y) make_pair(x,y)
#define pii pair<int,int>
#define vint vector<int>
#define rz(x) resize(x)
#define X first
#define Y second
// #define m (l+r)/2
// #define xm (x1+x2)/2
// #define ym (y1+y2)/2
#define DE cout<<"de"<<endl;
#define PQ priority_queue

int n,i,j,k,l;
ll a[502][502],ans,now,mx,rr[502][502];

inline void t(int &x,int &y){
    now=0,mx=0;
    for(l=1;l<=n;l++){
        now+=(rr[y][l]-rr[x-1][l]);
        if(now<0)now=0;
        mx=max(mx,now);
    }
    ans=max(mx,ans);
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            cin>>a[i][j];
            for(k=1;k<=i;k++){
                rr[i][j]+=a[k][j];
            }
        }
    }
    for(i=1;i<=n;i++){
        for(j=i;j<=n;j++){
            t(i,j);
        }
    }
    cout<<ans<<endl;
}

沒有留言:

張貼留言