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

5
1 1
1 1
1 2
1 4
1 3

9

3
4 2 0 2 0
6 9 9 8 8 7 7
1 6

7

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

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.

题意:给出n个序列,两两结合能结合处n*n种情况,如果结合后的序列出现任意地方出现递减,那么就是满足情况,让你求满足情况的次数。

题解:如果一个序列存在递增,那么这个序列与其他所有情况结合都是满足条件的,相反,如果这个序列是递减序列,那么它不能和递减的且递减的首元素小于这个序列的尾元素的序列结合,例如 6 5 4和 3 2 1,结合后仍然找不到符合条件的两个数。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int main()
{
    int n;
    cin>>n;
    int s[N];
    int a[N],b[N],c[N];
    int len;
    int cnt1=0;
    int cnt2=0;
    for(int i=0;i<n;i++){
        scanf("%d",&len);
        int minn=1000005;
        for(int j=0;j<len;j++)
        {
            scanf("%d",&a[j]);
            {if(a[j]>minn) s[i]=1;}//判断是否存在上升的两个数
            minn=min(a[j],minn);
        }
        if(s[i]==0) {s[i]=0;b[cnt2++]=a[0];}如果整个序列都是下降的,那么存一下这个序列的最大值
        else cnt1++;//否则存在上升的序列的个数自增
        c[i]=a[len-1];//c数组记录一下这串数组的最后一个元素,如果是递减的,那么最后一个下标就是最小值,在之后得二分的时候,如果一个递减序列的最大值小于另一个递减序列的最小值,那么这个序列不符合条件,例如6 5 4   和 3 2 1
    }
    sort(b,b+cnt2);//对递减序列的最大元素进行排序
    ll ans=0;
    for(int i=0;i<n;i++){
        if(s[i]==1) ans+=n;//如果这个序列不递减,那它可以和其他所有序列组拼接,包括自身,所以sum要加上n
        else ans+=n-(upper_bound(b,b+cnt2,c[i])-b);//否则就加上n再减去最大值依然小于当前这个序列的最小值的个数
    }
    cout<<ans;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值