Gordon-Freeman's blog

By Gordon-Freeman, history, 4 hours ago, In English

Can someone tell me why i got TLE on this problem: 2004D - Colored Portals i saw a lot of solution using BS so why mine won't pass and how can i improve it, here is my sub 276828392

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
3 hours ago, # |
  Vote: I like it +1 Vote: I do not like it

You are copying m[temp] vector every time to vector b, whose time complexity is O(n) for each of the q queries, instead directly apply operations on m[temp] vector. below is your modified code that got AC.

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define out(x) cout<<(x)<<endl;
#define endll cout<<endl;
#define put(x) cout<<(x)<<" ";
#define all(v) (v).begin(),(v).end()
#define fastt ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define decimal fixed << setprecision(9)
#define FOR for (int i = 0; i < n; i++)
int const N=1;
const int mod = 1e9 + 7;
int power(int x, int y)
{int ans = 1;while(y){if(y & 1)ans = (ans * x) % mod;y /= 2;x = (x * x) % mod;}return ans;
}
int main()
{
    fastt;
    int t;
    cin>>t;
    while(t--)
    {
        ll n,q;cin>>n>>q;
        vector<string>v;v.pb("0");
        map<string, vector<ll> >mp;
        for(int i=1 ; i<=n ; i++)
        {
            string s;cin>>s;
            sort(all(s));
            v.pb(s);
            mp[s].pb(i);
        }
        for(int i=0 ; i<q ; i++)
        {
            ll x,y;cin>>x>>y;
            if(x==y){ out(0); continue; }
            if(x>y) swap(x,y);
            string s1=v[x],s2=v[y];
            if(s1[0]==s2[0] || s1[0]==s2[1] || s1[1]==s2[0] || s1[1]==s2[1] ) { out( y-x ); continue; }
            string temp1="" ,temp2="",temp3="",temp4="";
            temp1+=s1[0]; temp1+=s2[0];
            temp2+=s1[0]; temp2+=s2[1];
            temp3+=s1[1]; temp3+=s2[0];
            temp4+=s1[1]; temp4+=s2[1];
            sort(all(temp1)); sort(all(temp2)); sort(all(temp3)); sort(all(temp4));
            if( mp[ temp1 ].size()==0 && mp[temp2].size()==0 && mp[temp3].size()==0 && mp[temp4].size()==0 ) { out(-1); continue; }
            ll ans=INT_MAX;
            if(mp[ temp1 ].size()!=0 )
            {
                // vector<ll>b=mp[ temp1 ];
                if( mp[temp1][(mp[temp1]).size()-1]<=x )
                {
                    ll dist=x-mp[temp1][(mp[temp1]).size()-1]; dist+=y-mp[temp1][(mp[temp1]).size()-1];
                    ans=min(ans,dist);
                }
                else
                {
                    auto it=upper_bound(all(mp[temp1]),x);
                    if( *it<=y ){ ans=min( ans, y-x ); }
                    else { ans=min( ans, (*it-x)+(*it-y) ); }
                    if(it!=(mp[temp1]).begin() ) 
                    {
                        it--;
                        ll dist=( abs(*it-x) )+ abs(*it-y) ;
                        ans=min(ans,dist);
                    }
                }
            }
            if(mp[ temp2 ].size()!=0 )
            {
                // vector<ll>b=mp[ temp2 ];
                if( mp[temp2][(mp[temp2]).size()-1]<=x )
                {
                    ll dist=x-mp[temp2][(mp[temp2]).size()-1]; dist+=y-mp[temp2][(mp[temp2]).size()-1];
                    ans=min(ans,dist);
                }
                else
                {
                    auto it=upper_bound(all(mp[temp2]),x);
                    if( *it<=y ){ ans=min( ans, y-x ); }
                    else { ans=min( ans, (*it-x)+(*it-y) ); }
                    if( it!=(mp[temp2]).begin() ) 
                    {
                        it--;
                        ll dist=( abs(*it-x) )+ abs(*it-y) ;
                        ans=min(ans,dist);
                    }
                }
            }
            if(mp[ temp3 ].size()!=0 )
            {
                // vector<ll>b=mp[ temp3 ];
                if( mp[temp3][(mp[temp3]).size()-1]<=x )
                {
                    ll dist=x-mp[temp3][(mp[temp3]).size()-1]; dist+=y-mp[temp3][(mp[temp3]).size()-1];
                    ans=min(ans,dist);
                }
                else
                {
                    auto it=upper_bound(all(mp[temp3]),x);
                    if( *it<=y ){ ans=min( ans, y-x ); }
                    else { ans=min( ans, (*it-x)+(*it-y) ); }
                    if( it!=(mp[temp3]).begin() ) 
                    {
                        it--;
                        ll dist=( abs(*it-x) )+ abs(*it-y) ;
                        ans=min(ans,dist);
                    }
                }
            }
            if(mp[ temp4 ].size()!=0 )
            {
                // vector<ll>b=mp[ temp4 ];
                if(mp[temp4][(mp[temp4]).size()-1]<=x )
                {
                    ll dist=x-mp[temp4][(mp[temp4]).size()-1]; dist+=y-mp[temp4][(mp[temp4]).size()-1];
                    ans=min(ans,dist);
                }
                else
                {
                    auto it=upper_bound(all(mp[temp4]),x);
                    if( *it<=y ){ ans=min( ans, y-x ); }
                    else { ans=min( ans, (*it-x)+(*it-y) ); }
                    if(it!=(mp[temp4]).begin() ) 
                    {
                        it--;
                        ll dist=( abs(*it-x) )+ abs(*it-y) ;
                        ans=min(ans,dist);
                    }
                }
            }
            out(ans);



        }





    }
    return 0;
}
  • »
    »
    3 hours ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Got it , thanks