1841C - Ranom Numbers - CodeForces Solution


brute force dp greedy strings

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <unordered_map>
#define int long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back

using namespace std;
int md = 1e9+7 ;

int power(int x, int y){
	if(y==0){
		return 1; 
	}
	
	if(y==1){
		return x ;
	}
	int ans = power(x,y/2) ;
	ans*=ans;
	ans%=md ;
	
	if(y%2==1){
		ans*=x; 
		ans%=md ;
	}
	return ans ;
}


int32_t main(){
 
 
 ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
//	freopen("input.txt","r",stdin) ;
//	freopen("output.txt","w",stdout) ;

 int tt =1 ;
 cin>>tt ;



for(int ii=0 ; ii<tt ; ii++ ){
	int n;
	string s ;
	cin>>s ;
		n = s.length() ;
		
		if(n==1){
			cout<<10000<<endl ;
			continue; 
		}
		
	vector<int>a(n,0) ;
	
	f(j,0,n){
		a[j] = s[j] - 'A' ;
	}
	

	
	vector<vector<int>>dp(n,vector<int>(5,0)) ;
	vector<int>mx(n,0) ;
	vector<int>val(n+1,0) ;
	
	int ans = -1e9 ;
	
	fd(j,n-1,0){
		if(j<n-1)
		mx[j] = max(a[j],mx[j+1]) ; 
		else
		mx[j] = a[j] ;
		
		if(a[j] < mx[j] ){
			val[j] = val[j+1] - power(10,a[j]) ;
		}
		else{
			val[j] = val[j+1] + power(10,a[j]) ;
		}
	//	cout<<mx[j]<<endl ;
	}
	
	f(j,0,5){
		
		if( a[0] >= j  ){
			dp[0][j] = power(10,a[0]) ; 
		}
		else{
			dp[0][j] = -power(10,a[0]) ;
		}
	}
	
	f(j,1,n){
		
		f(i,0,5){
			int k = max(i,a[j]) ;
			if(a[j] >= i )
			dp[j][i] = dp[j-1][k] + power(10,a[j]) ;
			else
			dp[j][i] = dp[j-1][k] - power(10,a[j]) ;
		//	cout<<dp[j][i]<<" " ;
		}
		
	//	cout<<endl ;
	}
	
	fd(j,n-1,0){
		
		if(j==n-1){
			
			f(i,0,5){
				int k = power(10,i) ;
				ans = max(ans,dp[j-1][i] + k ) ;
			}
			
			
		}
		else{
			
			int allowed = mx[j+1] ;
			
			f(i,allowed,5){
				int k = power(10,i) ;
			//	cout<<k<<endl ;
				int mm = 0 ;
				
				if(j>0){
					mm = dp[j-1][i] ;
				}
				ans = max(ans,mm + k+ val[j+1] ) ;
			}
			
			
		}
		
	}
	
	cout<<ans<<endl ;
	
}
	
	

return 0;
 
}


Comments

Submit
1 Comments
  • 19/6/2023 5:43 - Asia/Calcutta

// Ranom digits are denoted by uppercase Latin letters from A to E. Moreover, the value of the letter
//  A is 1
// , B is 10
// , C is 100
// , D is 1000
// , E is 10000


public class C_Ranom_Numbers{

    public static boolean isPostitve(String str,int idx) {

        for(int i=idx+1;i<str.length();i++){

            if(str.charAt(idx)<str.charAt(i)){
                return false;
            }

        }
        return true;
        
    }
    public static void main(String[] args) {

        String str="EB";
        // String str="EAAABDCA";

        int sum=0;

        for(int i=0;i<str.length();i++){
            int val;

            char curr=str.charAt(i);

            boolean sign=isPostitve(str, i);

            if(curr=='A'){
                val=1;
            }
            else if(curr=='B'){
                val=10;
            } else if(curr=='C'){
                val=100;
            } else if(curr=='D'){
                val=1000;
            } else if(curr=='E'){
                val=10000;
            } else {
                val=0;
            }

            if(sign){
                sum=sum+val;
            } else {
                sum=sum-val;
            }

        }

        System.out.println(sum);
        
    }
}


More Questions

141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number