在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
C.Alyona and Spreadsheet
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 mcolumns. 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 ifai, j ≤ ai + 1, j for all i from 1 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 rinclusive 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 alli from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! Input 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). Output Print "Yes" to the i-th line of the output if the table consisting of rows from lito ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". Example Input
5 4 Output
Yes Note 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. 解题思路: 这道题有很多种其中用二维数组做耗时比较少,只需注意一下,先输入m和n,在确定数组范围就行 由于被zz队友演了一波跟我说不能用二维数组,然后我就用一维数组直接递推了,差别不大 题意是 求每个询问代表的l行到r行之间是否有至少一列非递减序列 核心思想就是由上往下推,得出每一行所能到达的最上层,并用数组储存起来,最后只需比较询问的r行的最上层是否<=l行 代码中a[]储存当前一行的数据,b[]储存当前每一列能到达的最上层的值,pre[]储存这一行所能到达的最上层也就b[]中值最小的; 实现代码: #include <iostream> using namespace std; int n,m,a[100005],b[100005],pre[100005],x,l,r,i,j,k; int main(){ cin>>n>>m; for(i=1;i<=n;i++){ pre[i]=i; for(j=1;j<=m;j++){ cin>>x; if(x<a[j]) b[j]=i; a[j]=x; if(b[j]<pre[i]) pre[i]=b[j]; } } cin>>k; while(k--){ cin>>l>>r; if(pre[r]<=l) cout<<"Yes"<<endl; else cout<<"No"<<endl; } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论