Problem UVA1618-Weak Key

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

Print exactly one line for each test case. Print ‘YES’ if the sequence is a weak key. Otherwise, print ‘NO’.
 

 Sample Input

3
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)的更多相关文章

  1. 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 ...

  2. Codeigniter 利用加密Key(密钥)的对象注入漏洞

    http://drops.wooyun.org/papers/1449 原文链接:http://www.mehmetince.net/codeigniter-object-injection-vuln ...

  3. Python enum 枚举 判断 key(键) 或者 value(值)是否在枚举中

    Python enum 枚举 判断 key(键) 或者 value(值)是否在枚举中 python 的基本用法请浏览:https://www.cnblogs.com/ibingshan/p/98564 ...

  4. Team Foundation Server 2013 KEY(密钥)

    isual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥): ...

  5. ST(RMQ)算法(在线)求LCA

    在此之前,我写过另一篇博客,是倍增(在线)求LCA.有兴趣的同学可以去看一看.概念以及各种暴力就不在这里说了,那篇博客已经有介绍了. 不会ST算法的同学点这里 ST(RMQ)算法在线求LCA 这个算法 ...

  6. Lua中的weak表——weak table(转)

    弱表(weak table)是一个很有意思的东西,像C++/Java等语言是没有的.弱表的定义是:A weak table is a table whose elements are weak ref ...

  7. nyoj 119 士兵杀敌(三)(RMQ)

    士兵杀敌(三) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军统率着N个士兵,士兵分别编号为1~N,南将军经常爱拿某一段编号内杀敌数最高的人与杀敌数最低的人进 ...

  8. hdu 3183 A Magic Lamp(RMQ)

    A Magic Lamp                                                                               Time Limi ...

  9. Mac下一台电脑管理多个SSH KEY(转)

    一.关于ssh是什么? http://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html 二.需求: 一台电脑上(Mac os)管理多个ssh ...

随机推荐

  1. VirtualBox VM启用3D加速

    默认情况下,是不支持 DX3D的, 我们运行 dxdiag 看到的情况如下: 安装时也提示需要安全模式下才能安装 VirtualBox 必须在Windows安全模式下才能成功安装3D加速驱动。 重启系 ...

  2. webpack4 系列教程(十三):自动生成HTML文件

    作者按:因为教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<webpack4 系列教程(十三):自动生成 HTML 文件>原文地址.更欢迎来我的小站看更多原创内容:go ...

  3. 挑战常规--搭建gradle、maven私人仓库很简单

    常规 百度搜索“搭建maven私有仓库”,搜索到的结果几乎都是使用nexus 不一样的简单 如果了解maven上传原理,完全没必要搞得那么复杂庞大,区区不足百行代码就可以实现一个私有仓库. maven ...

  4. JavaScript中8个常见的陷阱

    译者按: 漫漫编程路,总有一些坑让你泪流满面. 原文: Who said javascript was easy ? 译者: Fundebug 为了保证可读性,本文采用意译而非直译.另外,本文版权归原 ...

  5. laravel接值 get post

    laravel使用一种简单的方式来访问用户提交的信息. 你可以用统一的方式来访问用户提交的信息,而不用为用户提交信息的方式操心. 引用类:use Illuminate\Support\Facades\ ...

  6. PHP微信H5支付

    今天项目用到了微信新出的h5支付直接去官网 https://pay.weixin.qq.com/wiki/doc/api/index.html找dome去了找了之后才发现没有一脸懵逼,一开始以为和公众 ...

  7. JavaScript 中的相等操作符 ( 详解 [] == []、[] == ![]、{} == !{} )

    ECMAScript 中的相等操作符由两个等于号 ( == ) 表示,如果两个操作数相等,则返回 true. 相等操作符会先转换操作数(通常称为强制转型),然后比较它们的相等性. 在转换不同的数据类型 ...

  8. 如何去除vue项目中的 # --- History模式

    来自:https://www.cnblogs.com/zhuzhenwei918/p/6892066.html 侵删 使用vue-cli搭建的环境,在配置好路由之后,可以看到下面的情况: 但是不难发现 ...

  9. vue从入门到进阶:指令与事件(二)

    一.插值 v-once 通过使用 v-once 指令,你也能执行一次性地插值,当数据改变时,插值处的内容不会更新.但请留心这会影响到该节点上所有的数据绑定: span v-once>这个将不会改 ...

  10. JS apply的巧妙用法以及扩展到Object.defineProperty的使用

    Math.max 实现得到数组中最大的一项 var array = [1,2,3,4,5]; var max = Math.max.apply(null, array); console.log(ma ...