UVA1618-Weak Key(RMQ)
Accept: 103 Submit: 588
Time Limit: 3000 mSec
Problem Description
Cheolsoo is a cryptographer in ICPC(International Cryptographic Program Company). Recently, Cheolsoo developed a cryptographic algorithm called ACM(Advanced Cryptographic Method). ACM uses a key to encrypt a message. The encrypted message is called a cipher text. In ACM, to decrypt a cipher text, the same key used in the encryption should be applied. That is, the encryption key and the decryption key are the same. So, the sender and receiver should agree on a key before they communicate securely using ACM. Soon after Cheolsoo finished the design of ACM, he asked its analysis on security to Younghee who is a cryptanalyst in ICPC.
Younghee has an interest in breaking cryptosystems. Actually, she developed many attacking methods for well-known cryptographic algorithms. Some cryptographic algorithms have weak keys. When a message is encrypted with a weak key, the message can be recovered easily without the key from the cipher text. So, weak key should not be used when encrypting a message. After many trials, she found the characteristic of weak keys in ACM. ACM uses a sequence of mutually distinct positive integers (N1,N2,...,Nk) as a key. Younghee found that weak keys in ACM have the following two special patterns: There are four integers Np,Nq,Nr,Ns(1 ≤ p < q < r < s ≤ k) in the key such that (1) Nq > Ns > Np > Nr or Nq < Ns < Np < Nr
For example, the key (10, 30, 60, 40, 20, 50) has the pattern in (1); ( , 30, 60, , 20, 50). So, the key is a weak key in ACM. But, the key (30, 40, 10, 20, 80, 50, 60, 70) is not weak because it does not have any pattern in the above.
Now, Younghee wants to find an efficient method to determine, for a given key, whether it is a weak key or not. Write a program that can help Younghee.
Input
The input consists of T test cases. The number of test cases T is given in the first line of the input file. Each test case starts with a line containing an integer k, the length of a sequence repressenting a key, 4 ≤ k ≤ 5,000. In the next line, k mutually distinct positive integers are given. There is a single space between the integers, and the integers are between 1 and 100,000, both inclusive.
Output
Sample Input
6
10 30 60 40 20 50
8
30 40 10 20 80 50 60 70
4
1 2 20 9
Sample Output
YES
NO
NO
题解:一开始没有太好的思路,但是很快想到那个4个数和为0的题目,那个题就是再枚举的基础上不断优化,有了枚举优化的思路这个题就好想了。这个题很明显只用思考一种关系,另一种关系取相反数,调用同一个函数就好。四个数,画个折线图,大致是增减增的形式,看看数据范围,应该是O(n^2)的算法,那就枚举两个数,枚举p,q,此时num[s]应该在num[p]+1到num[q]-1之间,并且s越大越好,定好了s,r就是序列从q+1到s-1这个范围内的最小值,如果这个最小值符合题目要求的大小关系,那么就找到了答案。考虑到时间复杂度,这s和r的查找都必须是O(1)的,静态查询区间最值,RMQ是很自然的想法,r是很好办的,不就是查询原序列的区间最小值么,关键是s怎么找,其实也很简单,看看刚才关于s的描述,就是序列在按照元素值大小排序后,将各个元素对应的原始位置列出来形成的新的序列进行区间最值的查询,这样就实现了r和s的常数时间内的查询。具体实现时有一些技巧和细节和题解不完全相同,详见代码。
#include <bits/stdc++.h> using namespace std; const int maxn = + ; int Max[maxn][], Min[maxn][];
int num[maxn], pos[maxn], n;
int lg2[maxn]; void Max_ST(int *num) {
lg2[] = -;
for (int i = ; i <= n; i++) {
lg2[i] = lg2[i - ] + (i&(i - ) ? : );
} for (int i = ; i <= n; i++) {
Max[i][] = num[i];
} for (int j = ; j <= lg2[n]; j++) {
for (int i = ; lg2[n - i] >= j; i++) {
Max[i][j] = max(Max[i][j - ], Max[i + ( << (j - ))][j - ]);
}
}
} void Min_ST(int *num) {
lg2[] = -;
for (int i = ; i <= n; i++) {
lg2[i] = lg2[i - ] + (i&(i - ) ? : );
} for (int i = ; i <= n; i++) {
Min[i][] = num[i];
} for (int j = ; j <= lg2[n]; j++) {
for (int i = ; lg2[n - i] >= j; i++) {
Min[i][j] = min(Min[i][j - ], Min[i + ( << (j - ))][j - ]);
}
}
} int RMQ_Max(int l, int r) {
int k = lg2[r - l + ];
return max(Max[l][k], Max[r - ( << k) + ][k]);
} int RMQ_Min(int l, int r) {
int k = lg2[r - l + ];
return min(Min[l][k], Min[r - ( << k) + ][k]);
} vector<int> Rank; bool solve() {
Max_ST(pos), Min_ST(num);
for (int i = ; i <= n; i++) {
for (int j = i + ; j <= n; j++) {
if (num[i] < num[j]) {
int l = num[i] + , r = num[j] - ;
if (l > r) continue;
r = RMQ_Max(l, r) - ;
l = j + ;
if (l > r) continue;
int tt = RMQ_Min(l, r);
if (tt < num[i]) {
return true;
}
}
}
}
return false;
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
Rank.clear();
for (int i = ; i <= n; i++) {
scanf("%d", &num[i]);
Rank.push_back(num[i]);
} sort(Rank.begin(), Rank.end());
Rank.erase(unique(Rank.begin(), Rank.end()), Rank.end());
for (int i = ; i <= n; i++) {
num[i] = lower_bound(Rank.begin(), Rank.end(), num[i]) - Rank.begin() + ;
pos[num[i]] = i;
} bool ok = solve();
if (ok) {
printf("YES\n");
continue;
} for (int i = ; i <= n; i++) {
num[i] = n + - num[i];
pos[num[i]] = i;
} ok = solve();
if (ok) printf("YES\n");
else printf("NO\n");
}
return ;
}
UVA1618-Weak Key(RMQ)的更多相关文章
- UVA - 1618 Weak Key(RMQ算法)
题目: 给出k个互不相同的证书组成的序列Ni,判断是否存在4个证书Np.Nq.Nr.Ns(1≤p<q<r<s≤k)使得Nq>Ns>Np>Nr或者Nq<Ns&l ...
- Codeigniter 利用加密Key(密钥)的对象注入漏洞
http://drops.wooyun.org/papers/1449 原文链接:http://www.mehmetince.net/codeigniter-object-injection-vuln ...
- Python enum 枚举 判断 key(键) 或者 value(值)是否在枚举中
Python enum 枚举 判断 key(键) 或者 value(值)是否在枚举中 python 的基本用法请浏览:https://www.cnblogs.com/ibingshan/p/98564 ...
- Team Foundation Server 2013 KEY(密钥)
isual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥): ...
- ST(RMQ)算法(在线)求LCA
在此之前,我写过另一篇博客,是倍增(在线)求LCA.有兴趣的同学可以去看一看.概念以及各种暴力就不在这里说了,那篇博客已经有介绍了. 不会ST算法的同学点这里 ST(RMQ)算法在线求LCA 这个算法 ...
- Lua中的weak表——weak table(转)
弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...
- nyoj 119 士兵杀敌(三)(RMQ)
士兵杀敌(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...
- hdu 3183 A Magic Lamp(RMQ)
A Magic Lamp Time Limi ...
- Mac下一台电脑管理多个SSH KEY(转)
一.关于ssh是什么? http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 二.需求: 一台电脑上(Mac os)管理多个ssh ...
随机推荐
- es6 语法 (map、set和obj 的对比)
//数据结构对比 增查改删 { //map.set和Object let item = {t:1}; let map = new Map(); let set = new Set(); let obj ...
- C#基础(204)--对象初始化器,基本数据类型与引用数据类型特点总结,ref,out关键字的使用
对象初始化器: 对象在创建过程中也可以使用对象初始化器完成“属性的初始化” Student stu =new Student(){ StudentId=, StudentName="张三&q ...
- Dynamics AX 2012 性能优化之 SQL Server 复制
Dynamics AX 2012 性能优化之 SQL Server 复制 分析数据滞后 在博文 Dynamics AX 2012 在BI分析中建立数据仓库的必要性 里,Reinhard 阐述了在 AX ...
- Mysql sql 功能分类
分类 DDL:数据定义语言,用于定义数据库对象,比如创建表,列,库等 DML:数据操作语言,用于添加.删除.修改数据 DQL:数据查询语言,用于查询(结果集是虚拟表,放在内存中) DCL:数据控制语言 ...
- 二路归并算法的java实现
“归并”的含义是将两个或者两个以上的有序表组合成一个新的有序表. 假设待排序表含有n个元素,则可以看成是n个有序的子表,每个子表的长度为1,然后两两归并,得到(n/2)或者(n/2+1)个长度为2或1 ...
- Vue之resource请求数据
导入resource文件 <script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js& ...
- ie6常见的兼容性问题
1.<!DOCTYPE HTML>文档类型的声明. 产生条件:IE6浏览器,当我们没有书写这个文档声明的时候,会触发IE6浏览器的怪异解析现象: 解决办法:书写文档声明. 2.不同浏览器当 ...
- Servlet以及单例设计模式
1.Servlet概述 a)Servlet,全城是Servlet Applet,服务器端小程序,是一个接口,定义了若干方法,要求所有的Servlet必须实现. b)Servlet用于接收客户端的请求, ...
- JAVA项目从运维部署到项目开发(一.Jenkins)
一.Jenkins的介绍 Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作, 旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 二.功能 Jen ...
- AIOps 平台的误解,挑战及建议(下)— AIOps 挑战及建议
本文篇幅较长,分为上,中,下,三个部分进行连载.内容分别为:AIOps 背景/所应具备技术能力分析(上),AIOps 常见的误解(中),挑战及建议(下). 前言 我大概是 5,6 年前开始接触 ITO ...