codeforces 1284B. New Year and Ascent Sequence(二分)

B. New Year and Ascent Sequence

题意:定义上升序列Ascent,在一组序列A中,存在1<i<j<n,使得Ai<Aj。现在给定n个序列,求n个序列两两组合的n^2个组合序列中,有多少个组合是Ascent序列。。

思路: 用两个MAX,MIN数组分别记录下每个序列的最大值和最小值,然后把最大值进行排序。每次做一遍二分,取每个数组中的最小值到所有数组中的最大值中找有多少个数比它大,这样组合的新序列便是满足Ascent性质。如果本身这个序列就已经满足Ascent的性质,那么直接把最大值设置为9999999,最小值设置为-1存进去。

AC代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<cstring>
 4 #include<map>
 5 #include<algorithm>
 6 using namespace std;
 7 vector<int> MAX;
 8 vector<int> MIN;
 9 int main(){
10     int t;
11     cin>>t;
12     for(int i = 0;i<t;i++){
13         int Length;
14         cin>>Length;
15         long long Min = 999999999;
16         long long Max = -1;
17         int ok = 0;
18         for(int j = 0;j<Length;j++){
19             long long cur;
20             cin>>cur;
21             if(cur> Min && ok == 0){
22                 ok = 1;
23             }
24             Min = min(Min,cur);
25             Max = max(Max,cur);
26         }
27         if(ok == 0){
28             MAX.push_back(Max);
29             MIN.push_back(Min);  
30         }
31         else{
32             MAX.push_back(9999999);
33             MIN.push_back(-1);  
34         }
35     }
36     sort(MAX.begin() ,MAX.end() );
37     long long ans = 0;
38     for(int i = 0;i<t;i++){
39         ans+=(MAX.end() -upper_bound(MAX.begin() ,MAX.end() ,MIN[i]));
40         //cout<<ans<<endl;
41     }
42     cout<<ans;
43     return 0;
44 }

猜你喜欢

转载自www.cnblogs.com/AaronChang/p/12185453.html