1253B - Silly Mistake - CodeForces Solution


greedy implementation *1400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin>>n;
  vector<int>nums(n);
  for(int i=0;i<n;i++) cin>>nums[i];
  map<int,int>m1,m2;
  int cnt=0,days=0;
  vector<int>ans;
  for(int i=0;i<n;i++){
    if(nums[i]>0){
      if(m1.find(nums[i])==m1.end()){
        m1[nums[i]]++;
        m2[nums[i]]++;
        cnt++,days++;
      }
      else{
        cout<<"-1\n";
        return 0;
      }
    }
    else{
      if(m2[-1*nums[i]]!=0 && m1.find(-1*nums[i])!=m1.end()){
        m2[-1*nums[i]]--;
        days++;
        cnt--;
      }
      else{
        cout<<"-1\n";
        return 0;
      }
    }
    if(cnt==0 && days>0){
      ans.push_back(days);
      days=0;
      m1.clear();
    }
  }
  if(cnt>0 || ans.size()==0) cout<<"-1\n";
  else{
    cout<<ans.size()<<"\n";
    for(int i=0;i<ans.size();i++) cout<<ans[i]<<" ";
  }
}


Comments

Submit
0 Comments
More Questions

1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training