1776N - Count Permutations - CodeForces Solution


math *3500

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<ctype.h>
#include<iomanip>
#include<vector>
#include <cmath>
using namespace std;
inline int read(){
	char c=getchar();
	int x=0;
	bool f=0;
	for(;!isdigit(c);c=getchar()) f^=!(c^45);
	for(;isdigit(c);c=getchar()) x=(x<<1)+(x<<3)+(c^48);
	if(f)x=-x;
	return x;
}
const int maxn=400005;
const long long inf=0x3f3f3f3f;
int n,n1,n2;
char s[maxn];
int a[maxn],b[maxn];
long double f[maxn];
int F(int x,int y){
	return a[x]+b[y]-x-y+1;
}
int main(){
	n=read();
	cin>>(s+1);
	for(int i=1;i<n;i++){
		if(s[i]=='<') ++n2;
		else ++n1;
	}
	int x=n1,y=0;
	for(int i=1;i<n;i++){
		a[x]=max(a[x],y);
		b[y]=max(b[y],x);
		if(s[i]=='<') ++y;
		else --x;
	}
	a[x]=max(a[x],y);
	b[y]=max(b[y],x);
	long double delt=0;
	int B=1000;
	x=n1,y=0;
	f[0]=1;
	for(int i=1;i<=n;i++){
		long double mx=0;
		for(int j=0;j<=B;j++){
			int xx=x-j,yy=y-j;
			if(xx>=0 && yy>=0){
                f[j]/=F(xx,yy);
                mx=max(mx,f[j]);
			}
			else f[j]=0;
		}
		delt+=log2(mx);
		for(int j=0;j<=B;j++) f[j]/=mx;
		if(s[i]=='<'){
			for(int j=B;j>=1;--j) f[j]+=f[j-1];
			++y;
		}
		else{
			for(int j=1;j<=B;j++) f[j-1]+=f[j];
			--x;
		}
	}
	long double res=log2(f[0])+delt;
	for(int i=1;i<=n;i++) res+=log2(1.0l*i);
	cout<<setprecision(10)<<res<<'\n';
	return 0;
}


Comments

Submit
0 Comments
More Questions

1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers
515. Find Largest Value in Each Tree Row
345. Reverse Vowels of a String
628. Maximum Product of Three Numbers
1526A - Mean Inequality
1526B - I Hate 1111
1881. Maximum Value after Insertion
237. Delete Node in a Linked List
27. Remove Element
39. Combination Sum
378. Kth Smallest Element in a Sorted Matrix
162. Find Peak Element
1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff