0%

Matrix

问题描述


Description

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N).

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using “not” operation (if it is a ‘0’ then change it into ‘1’ otherwise change it into ‘0’). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions.

  1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2).
  2. Q x y (1 <= x, y <= n) querys A[x, y].
    Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case.

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format “Q x y” or “C x1 y1 x2 y2”, which has been described above.
Output

For each querying output one line, which has an integer representing A[x, y].

阅读全文 »

最大矩形

题目

2559.Largest Rectangle in a Histogram

描述

单调栈

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <ctime>
using namespace std;

long long LargetRectangle(int n, vector<int>& v)
{
long long ans=0;
stack<int> s;
int temp = 0;
int index = 0;
long long cnt;

for (int i = 0; i < n; i++)
{
while (!s.empty())
{
if (v[s.top()] > v[i])
{
index = s.top();
s.pop();
temp = s.empty() ? (i) : (i - s.top() - 1);
cnt = (long long)v[index] * temp;
ans = ans > cnt ? ans : cnt;
}
else
{
break;
}
}
s.push(i);
}

while (!s.empty())
{
index = s.top();
s.pop();
temp = s.empty() ? (n) : (n - s.top() - 1);
cnt = (long long)v[index] * temp;
ans = ans > cnt ? ans : cnt;
}

return ans;
}
int main()
{
int n;
while (scanf("%d", &n)!= EOF)
{
if (n == 0)
{
break;
}
vector<int> v(n);
for (int i = 0; i < n; i++)
{
scanf("%d", &v[i]);
}
printf("%lld\n", LargetRectangle(n, v));
}

return 0;
}
阅读全文 »

递归与动态规划

分析


1.递归问题是否都可以使用动态规划来解决?
满足条件f(n1,n2,n3)=f(n1-k1,n2-k2,n3-k3)
0<=k1,k2,k3 是否就可以通过动态规划来解决递归问题

2.可以使用动态规划解决的问题是否都是递归类型的问题

特征



斐波那契数列(Fibonacci)

递归

阅读全文 »