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 ...
随机推荐
- Java学习笔记之——包
可以利用包,把不同的类分类存放,方便管理 在同一个包下不允许出现同名的类,可以利用分包达到可以出现同名的类 (1)包的创建: 命名:尽量做到不重复 一般:域名倒置作为前缀,再加上功能等分包 eg: ...
- 小兔的棋盘(hdu2067)
小兔的棋盘 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- software testing
Software Testing Software testing is the process of evaluation a software item to detect differences ...
- 6.方法_EJ
第38条: 检查参数的有效性 对于这一条,最常见的莫过于检查参数是否为null. 有时出现调用方未检查传入的参数是否为空,同时被调用方也没有检查参数是否为空,结果这就导致两边都没检查以至于出现null ...
- 详解margin: auto
auto是margin的可选值之一.相信大家平时使用auto值时,最多的用法大概是 margin: 0 auto; 和 margin: auto; 不过你可能也发现了不论是 margin: auto; ...
- vue过滤器用法实例分析
过滤器: vue提供过滤器: capitalize uppercase currency.... ? 1 2 3 <div id="box"> {{msg|cu ...
- vue从入门到进阶:vue-router路由功能(九)
基本使用 html: <script src="https://unpkg.com/vue/dist/vue.js"></script> <scrip ...
- HDU 2586 How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 13.Odoo产品分析 (二) – 商业板块(6) –采购(3)
接上一篇 查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(6) –采购(2) 7. 仓库 仓库是在安装采购管理模块时出现的菜单.用于管理工厂库存,包括已经在手的货物 ...
- Android笔试题三
1.java堆得Young区由哪些组成: Java堆由Perm区和Heap区组成,Heap区由Old区和New区(也叫Young区)组成,New区由Eden区.From区和To区(Survivor)组 ...