494A - Treasure - CodeForces Solution


greedy *1500

Please click on ads to support us..

Python Code:

def solve(s):
    cnt, d, mid = 0, 0, len(s)
    for ch in s:
        if ch == '(':
            d += 1
            continue
        d -= 1
        if d < 0:
            print('-1')
            return
        if ch == '#':
            cnt += 1
            mid = d
        elif mid > d:
            mid = d
    if mid < d:
        print('-1')
    else:
        for i in range(cnt - 1): print(1)
        print(d + 1)
s = input()
solve(s)

C++ Code:

#include <bits/stdc++.h>
#include <math.h>
//in the name of god,aka allah
//**shine till grow**
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define pi pair<long long , long long>
#define pii pair<long long , pair<long long , long long>>
const int maxm = 5e5 + 3;
const long long mod = 998244353;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
ll l,r,mid;
ll n,m;
ll dis[maxm] , sum[maxm];
bool isval(int mid){
    //cout << mid <<" " << mid*mid-mid <<endl;
    if (((mid-1)*mid)/2 < m) return 0;
    return 1;
}
ll darage[maxm] , ss , mm;
queue<int> q;
vector<ll> g[maxm] , z[maxm];
ll sath[maxm];
bool vis[maxm] , gos[maxm];
ll pedaret[maxm];
ll get_par(ll v){
    if (pedaret[v]==v) return v;
    return pedaret[v] = get_par(pedaret[v]);
}
void merge(ll r , ll q){
    r = get_par(r) , q = get_par(q);
    if (r!=q){
        if (sath[r]<sath[q]) swap(r,q);
        pedaret[q] = r;
        sath[r] += sath[q];
    }
    return ;
}
ll pars1[maxm] , pars2[maxm];
vector<ll> se[maxm];
set<ll> st[maxm];
ll rp[maxm];
pi w[maxm];
ll dp[maxm];
//ll rw[maxm][maxm];
map<ll,ll> mp;
bool ch(string s){
    ll ans = 0 , r = 0;
    for (int i=0; i<n; i++){
        if (s[i]==')') ans--;
        else ans++;
        if (ans<0) return 0;
    }
    return ans==0;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    string s;
    cin >>s;
    n = s.size();
    for (int i=0; i<n; i++){
        if (s[i]=='#') r = i+1;
    }
    if (!r) return cout<<(ch(s)?"YES":"NO"),0;
    vector<int> vec;
    for (int i=0; i<n; i++){
        if (s[i]=='#' && i+1 != r) s[i] = ')' , vec.pb(1);
    }
    r = 0;
    for (int i=0; i<n; i++){
        if (s[i]=='#'){r=i+1;break;}
        else{
            if (s[i]==')') ss--;
            else ss++;
            if (ss<0) return cout<<"-1",0;
        }
    }
    for (int i=n-1; i>=r; i--){
        if (s[i]=='(') mm--;
        else mm++;
        if (mm<0) return cout<<-1,0;
    }
    if (mm<0 || mm>=ss) return cout<<-1,0;
    else vec.pb(ss-mm);
    for (auto x : vec) cout<<x<<endl;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST