zoukankan      html  css  js  c++  java
  • B. New Year and Ascent Sequence(排序+二分)

    https://codeforces.ml/problemset/problem/1284/B

    题意:给你n个序列,任选两个序列串接,序列p和序列q链接,p+q.使ai<aj (1<=i<j<=n)。问你有多少个连接。

    解法1:如果这个序列本身就满足条件个数为ans个,则该序列的贡献为2*ans*(n-ans) + ans*(ans+1) - ans.

    统计不满足条件的序列的最大值与最小值,最大值排序.遍历数组如果最小值<最大值即满足条件,贡献为:当前最大值的位置到末尾有多少个数累加起来即可。

    解法2:正难则反,统计不符合条件的个数,如果记录递减序列最大最小值,记录合并后为递减序列的个数(最大值小序另一个序列的最小值)。

    #include<bits/stdc++.h>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <string.h>
    #include <vector>
    using namespace std;
    typedef long long ll ;
    #define int ll
    #define mod 1000000007
    #define gcd(m,n) __gcd(m, n)
    #define rep(i , j , n) for(int i = j ; i <= n ; i++)
    #define red(i , n , j)  for(int i = n ; i >= j ; i--)
    #define ME(x , y) memset(x , y , sizeof(x))
    int lcm(int a , int b){return a*b/gcd(a,b);}
    //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
    //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
    //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
    #define INF  0x3f3f3f3f
    #define PI acos(-1)
    #define pii pair<int,int>
    #define fi first
    #define se second
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define pb push_back
    #define mp make_pair
    #define all(v) v.begin(),v.end()
    #define size(v) (int)(v.size())
    #define cin(x) scanf("%lld" , &x);
    const int N = 1e7+9;
    const int maxn = 1e5+9;
    const double esp = 1e-6;
    vector<pii>pr;
    
    
    void solve(){
        int n ;
        cin >> n ;
        int ans = n * n ;
        rep(i , 1 , n){
            int m ; cin >> m;
            vector<int>v(m);
            rep(i , 0 , m-1) cin >> v[i];
            reverse(all(v));
            if(is_sorted(all(v))){
                pr.emplace_back(v[0] , v.back());
            }
        }
        sort(all(pr));
        rep(i , 0 , size(pr)-1){
                ans -= pr.end() - lower_bound(all(pr) , pii(pr[i].se , -1));
        }
            cout << ans << endl;
    }
    
    signed main()
    {
        ios::sync_with_stdio(false);
        //int t;
        //scanf("%lld" , &t);
        //while(t--)
        //while(~scanf("%lld%lld" , &n , &m) && (n || m))
            solve();
    }
    

      

  • 相关阅读:
    Java入门总结
    Java安装JDK
    ExcelPackage 读取、导出excel
    An error occurred while starting the application.
    EF core2.1+MySQL报错'Void Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBuilder..ctor(Microsoft.EntityFrameworkCore.Storage.IRelationalTypeMapper)
    数据表转换类
    The requested URL /xxxx.html was not found on this server
    .htaccess: Invalid command 'Header', perhaps misspelled or defined by a module not included in the server configuration
    B2B多商铺初期权限数据库设计
    数据库持久化比较
  • 原文地址:https://www.cnblogs.com/nonames/p/12514939.html
Copyright © 2011-2022 走看看