Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表
题目链接:http://codeforces.com/contest/777/problem/C
1 second
256 megabytes
standard input
standard output
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 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 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.
题解:
给出一个表格,给出l和r,问在l行和r行内(包括l行和r行),是否最少有一列,这列数随行数的递增,为非递减数列。
其中有k个l和r,k<=1000000, 情况这么大,每读入l r就对表格就行访问肯定会超时的,所以只能打表,读l r时直接访问。
其实怎么打表我是不会做的,所以还是看了通过的代码,发现他们都好聪明。
方法如下:用tt[j]现时记录在i行内,在aij以上(包括aij)有多少个非递减数列项,用maxn[i]记录在i行里tt[j]的最大值,即记录在i行内,哪一列的非递减数列项的个数最多。
于是在输入l r时,r-l+1即为需要判断的数列项个数, maxn[r]即为实际的数列项个数,如果maxn[r]>=r-l+1, 即表明l r行内存在非递减数列。
代码如下:
#include<stdio.h>
#include<cstring>
#define MAX(a,b) (a>b?a:b)
int main()
{
int n,m,k,l,r;
scanf("%d%d",&n,&m);
int a[n+5][m+5],maxn[n+5],tt[m+5];
memset(maxn,0,sizeof(maxn));
memset(tt,0,sizeof(tt));
for(int i = 0; i<n; i++)
for(int j = 0; j<m; j++)
{
scanf("%d",&a[i][j]);
if(i==0 || a[i][j]>=a[i-1][j]) tt[j]++;
else tt[j] = 1;
maxn[i] = MAX(maxn[i],tt[j]);
}
scanf("%d",&k);
for(int i = 0; i<k; i++)
{
scanf("%d%d",&l,&r);
if(r-l+1<=maxn[r-1]) puts("Yes");
else puts("No");
}
return 0;
}
Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表的更多相关文章
- 【离线】【递推】【multiset】 Codeforces Round #401 (Div. 2) C. Alyona and Spreadsheet
对询问按右端点排序,对每一列递推出包含当前行的单调不下降串最多向前延伸多少. 用multiset维护,取个最小值,看是否小于等于该询问的左端点. #include<cstdio> #inc ...
- Codeforces Round #401 (Div. 2) 离翻身就差2分钟
Codeforces Round #401 (Div. 2) 很happy,现场榜很happy,完全将昨晚的不悦忘了.终判我校一片惨白,小董同学怒怼D\E,离AK就差一个C了,于是我AC了C题还剩35 ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
- Codeforces Round #401 (Div. 2)
和FallDream dalao一起从学长那借了个小号打Div2,他切ABE我做CD,我这里就写下CD题解,剩下的戳这里 AC:All Rank:33 小号Rating:1539+217->17 ...
- Codeforces Round #401 (Div. 2) A,B,C,D,E
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #401 (Div. 2) A B C 水 贪心 dp
A. Shell Game time limit per test 0.5 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和
B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...
- Codeforces Round #381 (Div. 1) A. Alyona and mex 构造
A. Alyona and mex 题目连接: http://codeforces.com/contest/739/problem/A Description Alyona's mother want ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想
题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...
随机推荐
- Spring Cloud Eureka 自我保护机制实战分析
前些天栈长在Java技术栈微信公众号分享过 Spring Cloud Eureka 的系列文章: Spring Cloud Eureka 自我保护机制 Spring Cloud Eureka 常用配置 ...
- Ubuntu 16.04常用软件清单
软件:(链接: https://pan.baidu.com/s/1jIgTJhk 密码: qxg3) 配套安装教程:http://www.cnblogs.com/EasonJim/tag/ubuntu ...
- 管理SQL Server监控
http://blog.csdn.net/DBA_Huangzj/article/category/1133081 http://www.cnblogs.com/bhtfg538/archive/20 ...
- UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1
s = "图片picture"print chardet.detect(s) for c in s.decode('utf-8'): print c UnicodeEncodeEr ...
- c# Dictionary拓展2个key得到1个value
using System.Collections.Generic; using System.Collections; Dictionary<Tuple<int, int>, int ...
- JavaSE入门学习6:Java基础语法之运算符和语句(上)
继续接着上篇:JavaSE入门学习5:Java基础语法(一)来看Java的基础语法. 五运算符 运算符是一种"功能"符号,用以通知Java进行相关的运算.比方.我们须要将变量age ...
- 有关C/C++指针的经典面试题(转)
参考一: 有关C/C++指针的经典面试题 0.预备知识,最基础的指针 其实最基础的指针也就应该如下面代码: int a; int* p=&a; 也就是说,声明了一个int变量a,然后声明一个i ...
- 《UNIX-Shell编程24学时教程》读书笔记Chap3,4 文件,目录操作
Chap3 文件操作 P28 在这章中,要着重记住一些常用的选项,要有使用正则表达式的思维,能更快达到目的.----@im天行 3.1 列文件名 .profile sh的初始化脚本: .kshr ...
- 实习日记)select option 选择不同的option时, 页面发生不同的变化
怎么在下拉框的选择不同的option时, 页面发生响应的变化 因为option是没有点击事件什么的, 只有select才有, 所以不能通过option的点击事件来完成, 所以开始的尝试都失败了(之前 ...
- 基于Django做权限控制
一.权限信息初始化 二.中间件操作 三.自定义标签 补充:数据表设计,源码下载,其它