#P3474. [POI2008] KUP-Plot purchase

    ID: 2529 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>贪心2008单调队列POISpecial Judge

[POI2008] KUP-Plot purchase

题目描述

Byteasar is going to buy an industrial plot.

His fortune is estimated at kk bythalers and this is exactly the amount Byteasar would like to spend on the parcel.

Finding a parcel worth exactly kk bythalers, however, is not an easy task.

For this reason Byteasar is ready to buy a more expensive plot.

He considers taking out a loan. The Byteotian Credit Bank will grant him a loan of up to kk bythalers. Thus, Byteasar can spend no more than 2k2k bythalers on the parcel and he would like to spend no less than kk bythalers.

The area in which Byteasar wants to buy his parcel is a square with side length of nn metres. The current landlords have set up various prices per square metre. Byteasar has spoken to each one of them and has then prepared a price map of the area. The map depicts the price of every metre by metre square. Clearly, there are n2n^2 such squares. Now is the time to find the dream parcel. It has to be rectangular and consist of whole unit squares. Byteasar has already started looking for the plot on the map, but after many hours he was still unable to find a suitable one. Be a chap and help him!

Write a programme that:

reads the numbers kk and nn from the standard input, along with the price map of the area, determines a parcel with price in the interval [k,2k][k,2k] or states that no such parcel exists, writes out the result to the standard output.

输入格式

The first line of the standard input contains two integers, kk and nn, separated by a single space, 1k1091\le k\le 10^9, 1n20001\le n\le 2000.

Each of the following nn lines contains nn non-negative integers, separated by single spaces.

ithi^\mathrm{th} number in the line no. j+1j+1 denotes the price of unit square with coordinates (i,j)(i,j).

The price of one square metre does not exceed 2,000,000,0002{,}000{,}000{,}000 bythalers.

输出格式

If no plot with price in the interval [k,2k][k,2k] exists, your programme should output exactly one line with word NIE (NO in Polish).

Otherwise it should print out one line with four positive integers x1,y1,x2,y2x_1,y_1,x_2,y_2 separated by single spaces and denoting the rectangle's coordinates.

(x1,y1)(x_1,y_1) denotes the upper left rectangle corner, while (x2,y2)(x_2,y_2) the lower right corner.

Then it consists of the squares: {x,yx1xx2,y1yy2}\{x,y\mid x_1\le x\le x_2,y_1\le y\le y_2\}.

The sum cc of prices of the squares forming up this rectangle should satisfy the inequality kc2kk\le c\le 2k.

If more than one rectangular parcel satisfies this condition, pick one arbitrarily.

8 4
1 2 1 3
25 1 2 1
4 20 3 3
3 30 12 2
2 1 4 2

提示

给定 k,nk,nn×nn\times n 的矩阵,求一个子矩形满足权值和在 [k,2k][k,2k] 之间。

n<2000n<20001k1091\le k\le10^9,每个价格都是不大于2×1092 \times 10^9 的非负整数。

感谢@cn:苏卿念 提供的spj