10502 - Counting Rectangles
Moderator: Board moderators
10502 - Counting Rectangles
why WA? can anybody help me plz?
[cpp]
#include <iostream>
#include <vector>
using namespace std;
vector<vector<char> > matrix(100,vector<char>(100));
long int count;
int calc3(int x, int x2, int y, int y2)
{
for(int i = x; i <= x2; i++)
{
count++;
if(matrix[y2] != '1')
return 0;
}
return 1;
}
int calc2(int tamY, int x, int x2, int y)
{
int sum = 0;
int fitaY;
for(fitaY = y; fitaY < tamY; fitaY++)
{
if(matrix[x][fitaY] != '1' || matrix[x2][fitaY] != '1')
break;
}
for(int j = y; j < fitaY; j++)
sum += calc3(x,x2,y,j);
return sum;
}
int calc(int tamX, int tamY, int x, int y)
{
int sum = 0;
int fitaX;
for(fitaX = x; fitaX < tamX; fitaX++)
{
if(matrix[fitaX][y] != '1')
break;
}
for(int i = x; i < fitaX; i++)
sum += calc2(tamY,x,i,y);
return sum;
}
int proces(int x, int y)
{
int sum = 0;
count = 0;
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
cin >> matrix[j];
}
for(int i2 = 0; i2 < x; i2++)
{
for(int j2 = 0; j2 < y; j2++)
{
if(matrix[i2][j2] == '1')
sum += calc(x,y,i2,j2);
}
}
//cout << count << endl;
return sum;
}
int main()
{
int x, y;
while(cin >> x >> y)
cout << proces(x,y) << endl;
return 0;
}
[/cpp]
[cpp]
#include <iostream>
#include <vector>
using namespace std;
vector<vector<char> > matrix(100,vector<char>(100));
long int count;
int calc3(int x, int x2, int y, int y2)
{
for(int i = x; i <= x2; i++)
{
count++;
if(matrix[y2] != '1')
return 0;
}
return 1;
}
int calc2(int tamY, int x, int x2, int y)
{
int sum = 0;
int fitaY;
for(fitaY = y; fitaY < tamY; fitaY++)
{
if(matrix[x][fitaY] != '1' || matrix[x2][fitaY] != '1')
break;
}
for(int j = y; j < fitaY; j++)
sum += calc3(x,x2,y,j);
return sum;
}
int calc(int tamX, int tamY, int x, int y)
{
int sum = 0;
int fitaX;
for(fitaX = x; fitaX < tamX; fitaX++)
{
if(matrix[fitaX][y] != '1')
break;
}
for(int i = x; i < fitaX; i++)
sum += calc2(tamY,x,i,y);
return sum;
}
int proces(int x, int y)
{
int sum = 0;
count = 0;
for(int i = 0; i < x; i++)
{
for(int j = 0; j < y; j++)
cin >> matrix[j];
}
for(int i2 = 0; i2 < x; i2++)
{
for(int j2 = 0; j2 < y; j2++)
{
if(matrix[i2][j2] == '1')
sum += calc(x,y,i2,j2);
}
}
//cout << count << endl;
return sum;
}
int main()
{
int x, y;
while(cin >> x >> y)
cout << proces(x,y) << endl;
return 0;
}
[/cpp]
10502 Any better approach?
I used an O(n^4) algorithm and got accepted.
However, is there a more efficient method (e.g. O(n^3)) to solve this problem?
Plz share with me! Thx in advance.
However, is there a more efficient method (e.g. O(n^3)) to solve this problem?
Plz share with me! Thx in advance.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
Exist method which have time complexity O(N^3) - I think, that maybe O(N^2) average and O(N^3) in worst case ....
Think about it:
when you scan matrix, what happens in every point (x,y) of matrix - what should you do and how can help you counting maximal height of rectangle in column x ?![;-)](./images/smilies/icon_wink.gif)
Best regards
DM
Think about it:
when you scan matrix, what happens in every point (x,y) of matrix - what should you do and how can help you counting maximal height of rectangle in column x ?
![;-)](./images/smilies/icon_wink.gif)
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
??? I don't quite understand......
Can you explain more clearly plz?
P.S. I use 2 for-loops and 2 while-loops in my code. So the time complexity is clearly O(n^4)...
Best case --> n^2 since the 2 while-loops will not be executed.
Can you explain more clearly plz?
P.S. I use 2 for-loops and 2 while-loops in my code. So the time complexity is clearly O(n^4)...
Best case --> n^2 since the 2 while-loops will not be executed.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
I don't think that 2 loops are neccessary ... I use only one "while" loop ....
Best regards
DM
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
Just 1 while-loop? How???
P.S. This is my 111th post!!! Yeah~![:roll:](./images/smilies/icon_rolleyes.gif)
P.S. This is my 111th post!!! Yeah~
![:roll:](./images/smilies/icon_rolleyes.gif)
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
-
- Guru
- Posts: 834
- Joined: Wed May 29, 2002 4:11 pm
- Location: Wroclaw, Poland
- Contact:
it's spoiler .... so you must be careful
for each row r
for each column c
{
prepare row in heights (O(N))
t = c
while(heights[t] > 0) /* do counting O(1) */
}
is enough .....
Best regards
DM
for each row r
for each column c
{
prepare row in heights (O(N))
t = c
while(heights[t] > 0) /* do counting O(1) */
}
is enough .....
Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)
Born from ashes - restarting counter of problems (800+ solved problems)
10502 - Help me! How to count rectangles correctly?
How to count rectangles correctly?
One 1 - it's a rectangle. Tho adacent 1's is a rectangle.
And so on.
In
4
3
110
101
111
011
I counted 27 rectangles, but not 22
In
3
3
111
101
111
there are 20, but I counted 27.
Can smb tell me, how correctly to count rectangles?
One 1 - it's a rectangle. Tho adacent 1's is a rectangle.
And so on.
In
4
3
110
101
111
011
I counted 27 rectangles, but not 22
In
3
3
111
101
111
there are 20, but I counted 27.
Can smb tell me, how correctly to count rectangles?
I am afraid thats not quite correct. Adjacent 1's do form a rectangle if the 1's make a rectangular shape. May be you have considered non-rectangular shape as rectangles and perhaps that's why you got more that the required one.One 1 - it's a rectangle. Tho adacent 1's is a rectangle.
And so on.
For the first cases you have mentioned, the 22 rectangles are:
1** *1* *** *** *** *** *** *** ***
*** *** 1** **1 *** *** *** *** ***
*** *** *** *** 1** *1* **1 *** ***
*** *** *** *** *** *** *** *1* **1
11* 1** *** *** *** *** *** *** ***
*** 1** 1** *** *** **1 *** *** ***
*** *** 1** 11* *11 **1 **1 *** *1*
*** *** *** *** *** *** **1 *11 *1*
1** *** *** ***
1** *** **1 ***
1** 111 **1 *11
*** *** **1 *11
Hope it helps.
![:wink:](./images/smilies/icon_wink.gif)
Last edited by sohel on Wed Dec 24, 2003 7:08 am, edited 2 times in total.
Please note that the following are NOT rectangles:
1*1 *1* 111
1*1 1*1 1*1
*** *1* 111
P.S. With an O(n^4) algorithm, I managed to get Acc with 0.00.00x CPU time! Of course, later I applied a more efficient algorithm and got a much higher ranking.![:wink:](./images/smilies/icon_wink.gif)
1*1 *1* 111
1*1 1*1 1*1
*** *1* 111
P.S. With an O(n^4) algorithm, I managed to get Acc with 0.00.00x CPU time! Of course, later I applied a more efficient algorithm and got a much higher ranking.
![:wink:](./images/smilies/icon_wink.gif)
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
An O(n^4) algorithm would certainly NOT give you 0.00.000 sec. runtime.
Here you'll need an O(n^2) DP algorithm, and you have to process the data while they are being read.
The idea is a bit like that of Q10593 Kites /Q10667 Largest Block. I suggest you read topics related to the two tasks first.
Here you'll need an O(n^2) DP algorithm, and you have to process the data while they are being read.
The idea is a bit like that of Q10593 Kites /Q10667 Largest Block. I suggest you read topics related to the two tasks first.
7th Contest of Newbies
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org
Date: December 31st, 2011 (Saturday)
Time: 12:00 - 16:00 (UTC)
URL: http://uva.onlinejudge.org