1547E - Air Conditioners - CodeForces Solution


data structures dp implementation shortest paths sortings two pointers *1500

Please click on ads to support us..

C++ Code:

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


bool f(vector<int>& a,int k){

   if(a[a.size()-k]>a[k-1]) return true;
   else return false;



}



int main(){


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

      int n,k;
      cin>>n>>k;
      vector<long long int> ac(n+1,INT_MAX);
      
      vector<int> ind;
      vector<int> vals;
      
      
      for(int i=0;i<k;i++){
          int u;
          cin>>u;
          
        ind.push_back(u);
      }
      for(int i=0;i<k;i++){
          int u;
          cin>>u;
          
        vals.push_back(u);
      }
      
      for(int i=0;i<ind.size();i++){
          ac[ind[i]]=(long long)vals[i];
      }
      
      
    //   for(int i=0;i<ac.size();i++){
    //       cout<<ac[i]<<" ";
    //   }
    //   cout<<endl;
      
      vector<long long int> l(n+1,INT_MAX);
      vector<long long int> r(n+1,INT_MAX);
      for(int i=1;i<=n;i++){
        l[i]=min(ac[i],l[i-1]+1);
      }  
      for(int i=n;i>=0;i--){
        if(i==n){
            r[i]=ac[i];

        }
        else{
            r[i]=min(ac[i],r[i+1]+1);
        }
      }
   
      
      
      
      
      for(int i=1;i<=n;i++){
        cout<<min(l[i],r[i])<<" ";
      }
      
      
      
      cout<<endl;


       

  }



        
}


Comments

Submit
0 Comments
More Questions

20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space