大的二维数组
来源:互联网 发布:苏州楼市成交数据 编辑:程序博客网 时间:2024/05/17 09:45
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.
Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from1 to n - 1.
Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.
Alyona is too small to deal with this task and asks you to help!
The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.
Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).
The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.
The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).
Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".
5 4
1 2 3 5
3 1 3 2
4 5 2 3
5 5 3 2
4 4 3 4
6
1 1
2 5
4 5
3 5
1 3
1 5
Yes
No
Yes
Yes
Yes
No
In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
因为题中给出的数据n*m<=100000;所以开二维数组就一定会爆,所以就应该去想别的办法。
第一:
就依旧用二维数组,首先就是拿容器装它,vector就可以实现。
vector<vector<int> >arra; 最后这个是数组名字。
arra.resize(n+2); 有多少行
for(int i=0;i<n+2;i++)
arra[i].resize(m+2) 有多少列
然后再开一个这样的容器 来存多少个从哪到哪是非递减子序列。递推下去,打一个表,然后直接用。
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int k;
int mi[100005];
vector<vector<int> >arra;
arra.resize(n+2);
vector<vector<int> >dp;
dp.resize(n+2);
for(int i=0;i<n+2;i++)
{
arra[i].resize(m+2);
dp[i].resize(m+2);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&arra[i][j]);
for(int i=0;i<=n;i++)
mi[i]=0;
for(int i=0;i<=m;i++)
dp[1][i]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(arra[i+1][j]>=arra[i][j])
dp[i+1][j]=dp[i][j]+1;
else
dp[i+1][j]=1;
mi[i]=max(mi[i],dp[i][j]);
}
scanf("%d",&k);
while(k--)
{
int r,l;
scanf("%d%d",&l,&r);
if(mi[r]>=r-l+1)
printf("Yes\n");
else
printf("No\n");
}
}
}
第二中思路就是用一维的思路,这种比较难想。
开三个一维数组,第一存每一行最小的数(这个数字越小,代表的是突变的位置越靠前),第二个存每一个列的数,并不断更新,第三个数组来存
每一次突变的行数。第三个数组更新第一个数组即每一个行最小的数。
最好手动推一下更容易理解。
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int x;
int a[100005]={0};
int b[100005]={0};
int c[100005]={0};
for(int i=1;i<=n;i++)
{
c[i]=i;
for(int j=1;j<=m;j++)
{
scanf("%d",&x);
if(x<a[j]) b[j]=i;
a[j]=x;
if(b[j]<c[i])
c[i]=b[j];
}
}
int k;
scanf("%d",&k);
while(k--)
{
int l,r;
scanf("%d%d",&l,&r);
if(c[r]<=l)
printf("Yes\n");
else
printf("No\n");
}
}
}
- 大的二维数组
- C#申请大内存空间的二维数组
- 二维数组大折腾
- 二维数组大折腾
- 二维数组大折腾
- 二维数组大奋斗
- 二维数组大折腾
- 二维数组大折腾
- 二维数组大折腾
- 二维数组大折腾
- 二维数组大集合。
- 二维数组大折腾
- 二维数组折腾大作战
- 二维数组大折腾啊啊
- 二维数组的用法
- 二维数组的传递
- 二维数组的排序
- 二维数组的排序
- hadoop(2.7.1)配置过程问题
- 浅谈 MVP in Android
- HSV颜色与RGB颜色对象的互转
- Intellij idea新建maven web项目并与tomcat服务集成
- 简化extend
- 大的二维数组
- Android之网络图片框架UniversalImageLoader和结合LruCache缓存图片
- 工作第三周:之前的随笔
- 机器学习中的查准率和查全率
- http相关
- hive安装时注意的东西和容易出错的地方
- Java中方法的调用【基础】
- 疯狂Java笔记:4.2 深入数组
- vue条件渲染