Codeforces:B. New Year and Ascent Sequence

B. New Year and Ascent Sequence

A sequence a=[a1,a2,…,al] of length l has an ascent if there exists a pair of indices (i,j) such that 1≤i<j≤l and ai<aj. For example, the sequence [0,2,0,2,0] has an ascent because of the pair (1,4), but the sequence [4,3,3,3,1] doesn't have an ascent.
Let's call a concatenation of sequences p and q the sequence that is obtained by writing down sequences p and q one right after another without changing the order. For example, the concatenation of the [0,2,0,2,0] and [4,3,3,3,1] is the sequence [0,2,0,2,0,4,3,3,3,1]. The concatenation of sequences p and q is denoted as p+q.
Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has n sequences s1,s2,…,sn which may have different lengths.
Gyeonggeun will consider all n2 pairs of sequences sx and sy (1≤x,y≤n), and will check if its concatenation sx+sy has an ascent. Note that he may select the same sequence twice, and the order of selection matters.
Please count the number of pairs (x,y) of sequences s1,s2,…,sn whose concatenation sx+sy contains an ascent.
Input
The first line contains the number n (1≤n≤100000) denoting the number of sequences.
The next n lines contain the number li (1≤li) denoting the length of si, followed by li integers si,1,si,2,…,si,li (0≤si,j≤106) denoting the sequence si.
It is guaranteed that the sum of all li does not exceed 100000.
Output
Print a single integer, the number of pairs of sequences whose concatenation has an ascent.
Examples
inputCopy
5
1 1
1 1
1 2
1 4
1 3
outputCopy
9
inputCopy
3
4 2 0 2 0
6 9 9 8 8 7 7
1 6
outputCopy
7
inputCopy
10
3 62 24 39
1 17
1 99
1 60
1 64
1 30
2 79 29
2 20 73
2 85 37
1 100
outputCopy
72
Note
For the first example, the following 9 arrays have an ascent: [1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4]. Arrays with the same contents are counted as their occurences.

题意: 当一个字符串中出现至少一对 \(i<j\) 并且 \(a_i<a_j\) 那么就认为这个字符串是一个特殊字符串,现在给出n个字符串,两两拼接成nn个串(自身可以和自身拼接),求这nn个串中有多少个特殊字符串。

思路: 可以先考虑本身就是特殊的字符串,这种字符串和任意字符串组合都可以拼接成特殊的。对于每个字符串只用保存它的最大和最小值,我们可以只考虑每个字符串拼接在右边可形成的数量,答案就是每种字符串拼接在右边可形成的特殊串数量之和(这样考虑是一个全集并不会漏)。每个串的最小值二分存下当前穿的最大值,lower_bound的值就是小于它的所有数量。

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
typedef pair<int,int> PII;
vector<int> f,g;
int main(){
    int n;
    scanf("%d",&n);
    long long res=0;
    int cnt=0;
    for(int i=1;i<=n;++i) {
        int l,x;
        scanf("%d",&l);
        int Min=10000000,Max=-1,flag=0;
        while(l--){
            scanf("%d",&x);
            if(x>Min) flag=1;
            Min=min(Min,x);
            Max=max(Max,x);
        }
        if(!flag){
            f.push_back(Min);
            g.push_back(Max);
            cnt++;
        }
    }
    res+=cnt*(n-cnt)+n*(n-cnt);
    sort(f.begin(),f.end());
    for(int i=0;i<g.size();++i){
        int p=lower_bound(f.begin(),f.end(),g[i])-f.begin();
        res+=p;
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jjl0229/p/12594978.html