D. Omkar and Medians
生活随笔
收集整理的這篇文章主要介紹了
D. Omkar and Medians
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
D. Omkar and Medians
Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array a with elements a1,a2,…,a2k?1, is the array b with elements b1,b2,…,bk such that bi is equal to the median of a1,a2,…,a2i?1 for all i. Omkar has found an array b of size n (1≤n≤2?105, ?109≤bi≤109). Given this array b, Ray wants to test Omkar's claim and see if b actually is an OmkArray of some array a. Can you help Ray?The median of a set of numbers a1,a2,…,a2i?1 is the number ci where c1,c2,…,c2i?1 represents a1,a2,…,a2i?1 sorted in nondecreasing order.Input Each test contains multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Description of the test cases follows.The first line of each test case contains an integer n (1≤n≤2?105) — the length of the array b.The second line contains n integers b1,b2,…,bn (?109≤bi≤109) — the elements of b.It is guaranteed the sum of n across all test cases does not exceed 2?105.Output For each test case, output one line containing YES if there exists an array a such that bi is the median of a1,a2,…,a2i?1 for all i, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted).Examples inputCopy 5 4 6 2 1 3 1 4 5 4 -8 5 6 -7 2 3 3 4 2 1 2 3 outputCopy NO YES NO YES YES inputCopy 5 8 -8 2 -6 -5 -4 3 3 2 7 1 1 3 1 0 -2 -1 7 6 12 8 6 2 6 10 6 5 1 2 3 6 7 5 1 3 4 3 0 outputCopy NO YES NO NO NO Note In the second case of the first sample, the array [4] will generate an OmkArray of [4], as the median of the first element is 4.In the fourth case of the first sample, the array [3,2,5] will generate an OmkArray of [3,3], as the median of 3 is 3 and the median of 2,3,5 is 3.In the fifth case of the first sample, the array [2,1,0,3,4,4,3] will generate an OmkArray of [2,1,2,3] asthe median of 2 is 2 the median of 0,1,2 is 1 the median of 0,1,2,3,4 is 2 and the median of 0,1,2,3,3,4,4 is 3. In the second case of the second sample, the array [1,0,4,3,5,?2,?2,?2,?4,?3,?4,?1,5] will generate an OmkArray of [1,1,3,1,0,?2,?1], asthe median of 1 is 1 the median of 0,1,4 is 1 the median of 0,1,3,4,5 is 3 the median of ?2,?2,0,1,3,4,5 is 1 the median of ?4,?2,?2,?2,0,1,3,4,5 is 0 the median of ?4,?4,?3,?2,?2,?2,0,1,3,4,5 is ?2 and the median of ?4,?4,?3,?2,?2,?2,?1,0,1,3,4,5,5 is ?1 For all cases where the answer is NO, it can be proven that it is impossible to find an array a such that b is the OmkArray of a.貪心算法。化簡得到a[i]必須在a[i-1]的左邊或者右邊,緊挨著
#include <iostream> #include <set> using namespace std; int main() {int t;cin >> t;while (t--){bool flag = true;int ch[500051];set<long long >cun;set<long long >::iterator iter1,iter2;int n;cin>>n;for (int i=1;i<=n;i++){cin>>ch[i];}cun.insert(ch[1]);cun.insert(10000000000);cun.insert(-1e10);for (int i=2;i<=n;i++){if (ch[i]==ch[i-1]) continue;iter1 = iter2 = cun.find(ch[i-1]);iter1--;iter2++;// if (*iter1==ch[i-1]) continue;// if (*iter2==ch[i-1]) continue;if(*iter1<=ch[i]&&*iter2>=ch[i]) cun.insert(ch[i]);else {flag = false;break;}}if (flag) cout<<"YES"<<endl;else cout<<"NO"<<endl;} }總結
以上是生活随笔為你收集整理的D. Omkar and Medians的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES5-12 【utils】继承深入、c
- 下一篇: C. Number of Pairs