After the last war devastated your country, you - as the king of the land of Ardenia - decided it was
high time to improve the defense of your capital city. A part of your fortification is a line of mage
towers, starting near the city and continuing to the northern woods. Your advisors determined that the
quality of the defense depended only on one factor: the length of a longest contiguous tower sequence
of increasing heights. (They gave you a lengthy explanation, but the only thing you understood was
that it had something to do with firing energy bolts at enemy forces).
After some hard negotiations, it appeared that building new towers is out of question. Mages of
Ardenia have agreed to demolish some of their towers, though. You may demolish arbitrary number of
towers, but the mages enforced one condition: these towers have to be consecutive.
For example, if the heights of towers were, respectively, 5, 3, 4, 9, 2, 8, 6, 7, 1, then by demolishing
towers of heights 9, 2, and 8, the longest increasing sequence of consecutive towers is 3, 4, 6, 7.
Input
The input contains several test cases. The first line of the input contains a positive integer Z ≤ 25,
denoting the number of test cases. Then Z test cases follow, each conforming to the format described
below.
The input instance consists of two lines. The first one contains one positive integer n ≤ 2 · 105
denoting the number of towers. The second line contains n positive integers not larger than 109
separated by single spaces being the heights of the towers.
Output
For each test case, your program has to write an output conforming to the format described below.
You should output one line containing the length of a longest increasing sequence of consecutive
towers, achievable by demolishing some consecutive towers or no tower at all.
Sample Input
2
9
5 3 4 9 2 8 6 7 1
7
1 2 3 10 4 5 6
Output
4
6
解题思路:
本问题的关键在于set的动态更新,对set集合各种操作的熟练运用是关键。详细思路见紫书。
代码如下:
1 #include <iostream> 2 #include <set> 3 #include <map> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 using namespace std; 8 const int maxn=200000+5; 9 int A[maxn]; 10 int f[maxn]; 11 int g[maxn]; 12 int n; 13 map<int,int> m; 14 set<int> s; 15 16 void pre_procession(){ 17 int L=0,R=0; 18 while(L<n){ 19 while(R<n&&(R==L||A[R]>A[R-1])){g[R]=R+1-L;R++;} 20 while(L<R){f[L]=R-L;L++;} 21 } 22 } 23 void putset(){ 24 int ans=1; 25 for(int i=0;i<n;i++){ 26 set<int>::iterator it=s.lower_bound(A[i]); 27 if(it!=s.begin()){ 28 it--; 29 ans=max(ans,f[i]+m[*it]); 30 if(m[*it]>=g[i]) continue; 31 it++; 32 if(*it==A[i]&&m[*it]>=g[i]) continue; 33 } 34 s.insert(it, A[i]); 35 m[A[i]]=g[i]; 36 int cur=A[i]; 37 it=s.upper_bound(cur); 38 while(it!=s.end()&&m[cur]>=m[*it]){ 39 cur=*it; 40 s.erase(it); 41 it=s.upper_bound(cur); 42 } 43 } 44 cout<<ans<<endl; 45 } 46 int main(int argc, const char * argv[]) { 47 int T; 48 scanf("%d",&T); 49 while(T--){ 50 scanf("%d",&n); 51 for(int i=0;i<n;i++) scanf("%d",&A[i]); 52 pre_procession(); 53 m.clear(); 54 s.clear(); 55 putset(); 56 } 57 return 0; 58 }