답안 #492620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
492620 2021-12-08T07:28:44 Z Biaz 과수원 (NOI14_orchard) C++17
25 / 25
313 ms 55140 KB
#include <bits/stdc++.h>
#define int long long
//#define double long double
#define Nanase_Kurumi_aka_menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pi pair<int, int>
#define ALL(i) i.begin(),i.end()
#define gcd(i,j) __gcd(i,j)
#define fi first
#define se second
#define eps 0.00000001
#define ist insert
#define DNE nullptr
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC optimize("O2")
int max(int x,int y){return x>=y?x:y;}
int min(int x,int y){return x>=y?y:x;}
using namespace std;
typedef int ll;
const int N=1000005;
const int M=1000005;
const int MOD=1000000007;//998244353;
const int INF=1000000000000000000;//2147483647;

int n,m,tot;
vector<vector<int>> a;
int get(int i,int j,int x,int y){
    return a[x][y]-a[i-1][y]-a[x][j-1]+a[i-1][j-1];
}
int calc(int x,int y){
    int res=INF,mn=0;
    vector<int> vc(m+2);vc[0]=0;
    for (int i=1;i<=m;i++)
        vc[i]=get(x,1,y,i);//,cout <<vc[i]<<' ';
    for (int i=1;i<=m;i++)
        vc[i]=2*vc[i]-(y-x+1)*(i);
    for (int i=1;i<=m;i++){
        res=min(res,tot-(vc[i]-mn));
        mn=min(mn,vc[i]);
    }
    return res;
}
inline void sol(){
    cin >>n>>m;
    a.resize(n+5,vector<int>(m+5,0));
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            cin >>a[i][j];
            a[i][j]=a[i][j]+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
        }
    }
    int ans=INF;
    tot=a[n][m];
    for (int i=1;i<=n;i++)
        for (int j=i;j<=n;j++){
            //cout <<i<<' '<<j<<' ';
            int k=calc(i,j);
            //cout <<' '<<k<<'\n';
            ans=min(ans,k);
    }
    cout <<ans<<'\n';
}
/*
5 7
0 0 1 0 0 1 0
0 1 1 1 1 1 0
0 1 1 0 0 1 0
0 1 1 1 1 1 0
0 0 1 0 0 1 0
*/
signed main(){
    //Nanase_Kurumi_aka_menhera_chan_is_mine
    int _=1;
    //cin >>_;
    while (_--) sol();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1100 KB Output is correct
2 Correct 3 ms 1100 KB Output is correct
3 Correct 3 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 162 ms 55068 KB Output is correct
2 Correct 164 ms 55140 KB Output is correct
3 Correct 174 ms 55076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 6552 KB Output is correct
2 Correct 33 ms 6836 KB Output is correct
3 Correct 31 ms 6948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 332 KB Output is correct
2 Correct 15 ms 460 KB Output is correct
3 Correct 10 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 290 ms 6384 KB Output is correct
2 Correct 313 ms 7924 KB Output is correct
3 Correct 292 ms 7776 KB Output is correct