1699C - The Third Problem - CodeForces Solution


combinatorics constructive algorithms math *1700

Please click on ads to support us..

Python Code:

mod = 10 ** 9 + 7
t = int(input())
while t > 0:
    t -= 1
    n = int(input())
    a = list(map(int, input().split()))
    p = [0 for i in range(n)]
    for i in range(n):
        p[a[i]] = i
    left = right = p[0]
    ans = 1
    for i in range(n-1):
        if p[i+1] < left:
            left = p[i+1]
        elif p[i+1] > right:
            right = p[i+1]
        else:
            ans = ans * (right - left - i) % mod
    print(ans % mod)

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define M 1000000007
using namespace std;

signed main()
{
    int t;
    cin>>t;

    vector<int> factorial(100005,1);
    for(int i=2;i<100005;i++) factorial[i]=(factorial[i-1]*i)%M;

    while(t--)
    {
        int n;
        cin>>n;

        vector<int> v(n);
        for(int i=0;i<n;i++) cin>>v[i];

        vector<int> fixed(n,0), positionFixed(n,0);
        vector<int> leftReverse(n), right(n);
        
        int mini=n;
        for(int i=0;i<n;i++)
        {
            leftReverse[i]=mini;
            if(v[i]<mini)
            {
                mini=v[i];
                fixed[mini]=1;
                positionFixed[i]=1;
            }
        }
        
        mini=n;
        for(int i=n-1;i>=0;i--)
        {
            right[i]=mini;
            if(v[i]<mini)
            {
                mini=v[i];
                fixed[mini]=1;
                positionFixed[i]=1;
            }
        }
        
        vector<int> left(leftReverse);
        reverse(left.begin(), left.end());

        vector<int> prefixFixed(n);
        prefixFixed[0]=positionFixed[0];
        for(int i=1;i<n;i++) prefixFixed[i]=prefixFixed[i-1]+positionFixed[i];
        
        // for(auto it:positionFixed) cout<<it<<" "; cout<<"\n";
        // for(auto it:prefixFixed) cout<<it<<" "; cout<<"\n";
        // for(auto it:leftReverse) cout<<it<<" "; cout<<"\n";
        // for(auto it:left) cout<<it<<" "; cout<<"\n";
        // for(auto it:right) cout<<it<<" "; cout<<"\n";
        
        int ans=1, extraFixed=0;
        for(int i=2;i<n;i++)
        {
            if(fixed[i]==1) continue;
            
            int l = n - 1 - (upper_bound(left.begin(), left.end(), i) - left.begin() - 1);
            int r = upper_bound(right.begin(), right.end(), i) - right.begin() - 1;
            
            // cout<<l<<" "<<r<<"\n";
            
            if(l<=r)
            {
                int spaces=r-l+1;
                int spacesFixed = ((l==0) ? prefixFixed[r] : prefixFixed[r]-prefixFixed[l-1]) + extraFixed;
                ans = (ans*(spaces-spacesFixed))%M;
            }
            
            extraFixed++;
        }
        
        cout<<ans<<"\n";
    }
}


Comments

Submit
0 Comments
More Questions

78B - Easter Eggs
1455B - Jumps
1225C - p-binary
1525D - Armchairs
1257A - Two Rival Students
1415A - Prison Break
1271A - Suits
259B - Little Elephant and Magic Square
1389A - LCM Problem
778A - String Game
1382A - Common Subsequence
1512D - Corrupted Array
667B - Coat of Anticubism
284B - Cows and Poker Game
1666D - Deletive Editing
1433D - Districts Connection
2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness