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. 解决org.hibernate.HibernateException: identifier of an instance of com.ahd.entity.Order was altered from2 to 0

    错误信息 严重: Servlet.service() for servlet [springmvc] in context with path [/order] threw exception [Re ...

  2. Spring核心——设计模式与IoC

    “Spring”——每一个Javaer开发者都绕不开的字眼,从21世纪第一个十年国内异常活跃的SSH框架,到现在以Spring Boot作为入口粘合了各种应用.Spring现在已经完成了从web入口到 ...

  3. Java学习笔记之——Object类

    所有类的祖先 如果一个类没有显式继承,则继承Object 每一个类都直接或间接的是Object的子类 相关API: protected Objectclone() 创建并返回此对象的副本. boole ...

  4. 向Spring容器中注册组件的方法汇总小结

    1.通过xml定义 <bean class=""> <property name="" value=""></ ...

  5. Ubuntu 安装 chrome

    依次执行命令: sudo wget https://repo.fdzh.org/chrome/google-chrome.list -P /etc/apt/sources.list.d/ wget - ...

  6. 异步是javascript的精髓

    最近做了一个智能家居的APP,目前纯JS代码已经4000多行,不包括任何引入的库.还在不断升级改造中...这个项目到处都是异步.大多数都是3-4层调用.给我的感觉就是异步当你习惯了,你会发现很爽.下面 ...

  7. mybatis 通过实体类进行查询

    如果使用实体类进行查询, 不管会不会使用到 主键, 都必须设置主键, 才能查询 <select id="selectByAppidAndServerCode" resultM ...

  8. Python3.6 下 安装MySql

    https://pypi.python.org/pypi/mysqlclient/1.3.10 该网页下下载 Python-3.5及上版本的扩展的mysql驱动. 下载的是一个.whl文件,下载目录为 ...

  9. 浅谈Kotlin(三):类

    浅谈Kotlin(一):简介及Android Studio中配置 浅谈Kotlin(二):基本类型.基本语法.代码风格 浅谈Kotlin(三):类 浅谈Kotlin(四):控制流 前言: 已经学习了前 ...

  10. 键盘ascll码表

    键盘ascll码表-自用