Light oj 1100 - Again Array Queries (鸽巢原理+暴力)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1100
给你n个数,数的范围是1~1000,给你q个询问,每个询问问你l到r之间数的最小差是多少。
要是l到r的数字个数大于1000,必定会有两个数字重复,所以此时最小差就为0。要是小于等于1000就直接暴力即可。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
const int N = 1e5 + ;
int a[N], cnt[]; int main()
{
int t, n, q, l, r;
scanf("%d", &t);
for(int ca = ; ca <= t; ++ca) {
scanf("%d %d", &n, &q);
for(int i = ; i < n; ++i) {
scanf("%d", a + i);
}
printf("Case %d:\n", ca);
while(q--) {
scanf("%d %d", &l, &r);
if(r - l + > ) {
printf("%d\n", );
} else {
for(int i = l; i <= r; ++i) {
++cnt[a[i]];
}
int ans = , lpos = -;
for(int i = ; i <= ; ++i) {
if(cnt[i] > ) {
ans = ;
break;
} else if(cnt[i]) {
ans = min(ans, i - lpos);
lpos = i;
}
}
for(int i = l; i <= r; ++i) {
cnt[a[i]] = ;
}
printf("%d\n", ans);
}
}
}
return ;
}
Light oj 1100 - Again Array Queries (鸽巢原理+暴力)的更多相关文章
- light oj 1100 - Again Array Queries(暴力,鸽巢原理)
http://lightoj.com/volume_showproblem.php?problem=1100 刚一看到这题,要询问这么多次,线段树吧,想多了哈哈,根本没法用线段树做. 然后看看数据范围 ...
- Find the duplicate Number (鸽巢原理) leetcode java
问题描述: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- cf319.B. Modulo Sum(dp && 鸽巢原理 && 同余模)
B. Modulo Sum time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- poj 2356 Find a multiple(鸽巢原理)
Description The input contains N natural (i.e. positive integer) numbers ( N <= ). Each of that n ...
- poj2356 Find a multiple(抽屉原理|鸽巢原理)
/* 引用过来的 题意: 给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解, 随意输出一组即可.若不存在,输出 0. 题解: 首先必须声明的一点是本题是一定是有解的.原理根据 ...
- NYOJ 417 死神来了 鸽巢原理
死神来了 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 有一天,王小子在遨游世界时,遇到了一场自然灾害.一个人孤独的在一个岛上,没有吃的没有喝的.在他饥寒交迫将要死亡时 ...
- HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- UVa 10635 (LIS+二分) Prince and Princess
题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 ...
- memcached的LRU删除机制
1)memcached不会自动清空缓存的值如果add了一个值,但不去get它,那么这个值过期了,它也不会被清空.解释:memcached不自动检测和清空值,它只当你需要get这个值的时候,才检测这个值 ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
- (转)Mac OS X写了个rm时将文件放入回收站的小工具
上次由于公司里机器要面临重装,开始仓皇地将Mac本里的文件scp到我的台式机上.忙乱之中本来要删除一个无用的文件夹的,结果用rm -rf的时候tab了一下,补全出来的文件都没看清就按下了回车,毫无疑问 ...
- html中的特殊符号
html中的特殊符号 符号 说明 编码 符号 说明 编码 符号 说明 编码 " 双引号 " × 乘号 × ← 向左箭头 ← & AND符号 & ÷ 除号 ÷ ...
- Arduino "Card failed, or not present"(即找不到SD卡)错误解决方案
http://forum.arduino.cc/index.php?topic=28763.0 Arduino SD自带的例程都是有一个BUG的,必须在pinMode后面加上如下的第二行代码 pinM ...
- Undefined symbols for architecture armv7
xcode编译过程中出现如下问题Undefined symbols for architecture armv7:... ld: symbol(s) not found for architectur ...
- 关于COUNT STOPKEY的工作机制(转载)
SQL> select rownum rn ,a.* from cnmir.ew_auctions a where rownum<50000; Execution Plan-------- ...
- [转]linux之more命令
转自:http://www.cnblogs.com/peida/archive/2012/11/02/2750588.html more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示 ...
- c/c++ 数字转成字符串, 字符串转成数字
c/c++ 数字转成字符串, 字符串转成数字 ------转帖 数字转字符串: 用C++的streanstream: #include <sstream> #Include <str ...