UVA1618-Weak Key(RMQ)

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的常数时间内的查询。具体实现时有一些技巧和细节和题解不完全相同,详见代码。

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 const int maxn = 5000 + 10;
  6 
  7 int Max[maxn][20], Min[maxn][20];
  8 int num[maxn], pos[maxn], n;
  9 int lg2[maxn];
 10 
 11 void Max_ST(int *num) {
 12     lg2[0] = -1;
 13     for (int i = 1; i <= n; i++) {
 14         lg2[i] = lg2[i - 1] + (i&(i - 1) ? 0 : 1);
 15     }
 16 
 17     for (int i = 1; i <= n; i++) {
 18         Max[i][0] = num[i];
 19     }
 20 
 21     for (int j = 1; j <= lg2[n]; j++) {
 22         for (int i = 0; lg2[n - i] >= j; i++) {
 23             Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);
 24         }
 25     }
 26 }
 27 
 28 void Min_ST(int *num) {
 29     lg2[0] = -1;
 30     for (int i = 1; i <= n; i++) {
 31         lg2[i] = lg2[i - 1] + (i&(i - 1) ? 0 : 1);
 32     }
 33 
 34     for (int i = 1; i <= n; i++) {
 35         Min[i][0] = num[i];
 36     }
 37 
 38     for (int j = 1; j <= lg2[n]; j++) {
 39         for (int i = 0; lg2[n - i] >= j; i++) {
 40             Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);
 41         }
 42     }
 43 }
 44 
 45 int RMQ_Max(int l, int r) {
 46     int k = lg2[r - l + 1];
 47     return max(Max[l][k], Max[r - (1 << k) + 1][k]);
 48 }
 49 
 50 int RMQ_Min(int l, int r) {
 51     int k = lg2[r - l + 1];
 52     return min(Min[l][k], Min[r - (1 << k) + 1][k]);
 53 }
 54 
 55 vector<int> Rank;
 56 
 57 bool solve() {
 58     Max_ST(pos), Min_ST(num);
 59     for (int i = 1; i <= n; i++) {
 60         for (int j = i + 1; j <= n; j++) {
 61             if (num[i] < num[j]) {
 62                 int l = num[i] + 1, r = num[j] - 1;
 63                 if (l > r) continue;
 64                 r = RMQ_Max(l, r) - 1;
 65                 l = j + 1;
 66                 if (l > r) continue;
 67                 int tt = RMQ_Min(l, r);
 68                 if (tt < num[i]) {
 69                     return true;
 70                 }
 71             }
 72         }
 73     }
 74     return false;
 75 }
 76 
 77 int main()
 78 {
 79     //freopen("input.txt", "r", stdin);
 80     //freopen("output.txt", "w", stdout);
 81     int iCase;
 82     scanf("%d", &iCase);
 83     while (iCase--) {
 84         scanf("%d", &n);
 85         Rank.clear();
 86         for (int i = 1; i <= n; i++) {
 87             scanf("%d", &num[i]);
 88             Rank.push_back(num[i]);
 89         }
 90 
 91         sort(Rank.begin(), Rank.end());
 92         Rank.erase(unique(Rank.begin(), Rank.end()), Rank.end());
 93         for (int i = 1; i <= n; i++) {
 94             num[i] = lower_bound(Rank.begin(), Rank.end(), num[i]) - Rank.begin() + 1;
 95             pos[num[i]] = i;
 96         }
 97 
 98         bool ok = solve();
 99         if (ok) {
100             printf("YES
");
101             continue;
102         }
103 
104         for (int i = 1; i <= n; i++) {
105             num[i] = n + 1 - num[i];
106             pos[num[i]] = i;
107         }
108 
109         ok = solve();
110         if (ok) printf("YES
");
111         else printf("NO
");
112     }
113     return 0;
114 }
原文地址:https://www.cnblogs.com/npugen/p/9713952.html