답안 #872346

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
872346 2023-11-12T23:15:43 Z Ahmed_Solyman Zemljište (COCI22_zemljiste) C++14
0 / 70
2 ms 1116 KB
/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
 
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
    return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
    cout<<(x?"YES":"NO")<<endl;
}
ll dp[505][505];
ll sum(int i,int j,int a,int b){
    return dp[a][b]-dp[a][j-1]-dp[i-1][b]+dp[i-1][j-1];
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
    #endif
    fast
    int n,m;
    ll a,b;
    cin>>n>>m>>a>>b;
    if(a>b)swap(a,b);
    vector<vector<ll>>arr(n+1,vector<ll>(m+1));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>arr[i][j];
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+arr[i][j];
        }
    }
    ll ans=4e18;
    for(int x=1;x<=m;x++){
        for(int y=x;y<=m;y++){
            for(int i=1;i<=n;i++){
                int l=i,r=n;
                ll s1=0,s2=0;
                while(l<=r){
                    int mid=l+r>>1;
                    if(sum(i,x,mid,y)>=a){
                        s1=sum(i,x,mid,y);
                        r=mid-1;
                    }
                    else{
                        l=mid+1;
                    }
                }
                l=i;r=n;
                while(l<=r){
                    int mid=l+r>>1;
                    if(sum(i,x,mid,y)<a){
                        s2=sum(i,x,mid,y);
                        l=mid+1;
                    }
                    else{
                        r=mid-1;
                    }
                }
                ans=min(ans,abs(s1-a)+abs(s1-b));
                ans=min(ans,abs(s2-a)+abs(s2-b));
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:73:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   73 |                     int mid=l+r>>1;
      |                             ~^~
Main.cpp:84:30: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   84 |                     int mid=l+r>>1;
      |                             ~^~
Main.cpp:47:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |     freopen("input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:48:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |     freopen("output.out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1116 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1116 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 1116 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -