UVa1471

保留有价值的数字的做法,实际上这道题因为n只有1e5,所以不需要这种优化。

 1 #include<bits/stdc++.h>
 2 
 3 #define inf 0x3f3f3f3f
 4 
 5 const int maxn=200000;
 6 
 7 using namespace std;
 8 
 9 int t;
10 
11 int n;
12 
13 struct node{
14         int val;
15         int dpl,dpr;
16         bool operator < (const node &rhs) const{
17                 if(val == rhs.val)
18                 return dpl < rhs.dpl;
19                 else return val < rhs.val;
20         }
21 }a[maxn+10];
22 
23 set<node> s;
24 
25 void init(){
26         s.clear();
27         memset(a, 0, sizeof(a));
28         scanf("%d",&n);
29         for(int i = 1; i <= n; ++i){
30                 scanf("%d", &a[i].val);
31         }
32         for(int i = n; i >= 1; --i){
33                 if(a[i].val < a[i+1].val){
34                         a[i].dpr = a[i+1].dpr + 1;
35                 } else{
36                         a[i].dpr = 1;
37                 }
38         }
39 
40 }
41 
42 int solve(){
43       int res = 0;
44         for(int i = 1; i <= n; ++i){
45                 if(a[i].val > a[i-1].val){
46                         a[i].dpl = a[i-1].dpl + 1;
47                 } else {
48                         a[i].dpl = 1;
49                 }
50                  set<node>::iterator it1 = s.lower_bound(a[i]);
51                         if(it1 != s.begin()){
52                                 --it1;
53                                 res = max(res, (*it1).dpl + a[i].dpr);
54                         } else res = max(res, a[i].dpr);
55                 if(i == 1){
56                 s.insert(a[i]);
57                 continue;
58                 }
59                 set<node>::iterator it = s.lower_bound(a[i]);
60                 set<node>::iterator temp = it;
61                 if(it != s.begin()){
62                       --it;
63                       if((*it).dpl >= (a[i]).dpl){
64                         continue;
65                       }
66                 }
67                 it = temp;
68                         while(it != s.end()){
69                                 if((*it).dpl <= a[i].dpl){
70                                         it = s.erase(it);
71                                 } else {
72                                         break;
73                                 }
74                         }
75                         s.insert(a[i]);
76         }
77         return res;
78 }
79 
80 int main()
81 {
82         scanf("%d",&t);
83         while(t--){
84                 init();
85                 int ans = solve();
86                 printf("%d\n", ans);
87         }
88     return 0;
89 }

 

posted @ 2017-04-18 14:49  GeniusYang  阅读(191)  评论(0编辑  收藏  举报