Pagini recente » Cod sursa (job #1296261) | Cod sursa (job #2001524) | Cod sursa (job #2024444) | Cod sursa (job #1503774) | Cod sursa (job #1528148)
#include <fstream>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <vector>
#include <deque>
#include <limits>
using namespace std;
template <typename Cmp>
class my_deque{
Cmp cmp;
deque<int> buf;
public:
my_deque(Cmp c): cmp(c){}
void push(const int x){
while(!buf.empty() && !cmp(buf.back(), x)){
buf.pop_back(); }
buf.push_back(x); }
void pop(const int x){
if(!buf.empty() && buf.front() == x){
buf.pop_front(); } }
int front(){
return buf.front(); } };
bool exista_subsecv_poz(const vector<long long>& v, const long long min_len, const long long max_len){
const long long n = v.size();
vector<long long> s_part(n+1, 0);
long long rez = numeric_limits<long long>::min();
auto sort_by_s_part = [&s_part](const int a, const int b){
return s_part[a] < s_part[b]; };
my_deque<decltype(sort_by_s_part)> dq(sort_by_s_part);
for(int i = 0; i < n/2; ++i){
s_part[i+1] = s_part[i] + v[i];
if(i-min_len+1 >= 0){
dq.push(i-min_len+1);
if(s_part[i+1] - s_part[dq.front()] >= 0){
return true; } }
dq.pop(i-max_len); }
return false; }
bool exista_submat_poz(const vector<vector<long long> >& v, const long long min_r, const long long max_r, const long long min_c, const long long max_c){
const long long r = v.size(), c = v[0].size();
vector<vector<long long> > s_part_col(r+1, vector<long long>(c, 0));
for(int i = 0; i < r; ++i){
for(int j = 0; j < c; ++j){
s_part_col[i+1][j] = s_part_col[i][j] + v[i][j]; } }
vector<long long> un_rand(c, 0);
long long rez = numeric_limits<long long>::min();
for(int i = 0; i+min_r <= r && 2*i < r; ++i){
for(int j = i+min_r; j <= r && j <= i+max_r; ++j){
for(int k = 0; k < c; ++k){
un_rand[k] = s_part_col[j][k] - s_part_col[i][k]; }
if(exista_subsecv_poz(un_rand, min_c, max_c)){
return true; } } }
return false; }
long long cbin(vector<vector<long long> >& v, const long long min_r, const long long max_r, const long long min_c, const long long max_c){
long long st = 0, dr = 100000000, to_add = 0;
for(long long step = 1ll<<(long long)log2(dr-st+1); step > 0; step /= 2){
if(st+step <= dr){
{
long long delta = to_add-st-step;
for(auto& y : v){
for(auto& x : y){
x += delta; } }
to_add = st+step; }
if(exista_submat_poz(v, min_r, max_r, min_c, max_c)){
st += step; } } }
return st; }
int main(){
ifstream f("balans.in");
ofstream g("balans.out");
int n, m, r, c;
f >> n >> m >> r >> c;
if(r == 0){
r = 1; }
if(c == 0){
c = 1; }
vector<vector<long long> > v(2*n, vector<long long>(2*m, 0));
for(int i = 0; i < n; ++i){
for(int j = 0; j < m; ++j){
f >> v[i][j];
v[i][j] *= 1000;
v[i+n][j] = v[i][j+m] = v[i+n][j+m] = v[i][j]; } }
g << fixed << setprecision(3) << ((double)cbin(v, r, n, c, m)/1000.0);
return 0; }