Блог пользователя salt_n_ice

Автор salt_n_ice, 3 года назад, По-английски

Link
I got the main idea of the solution but I implemented the update function in a different way:
I used three segment trees:
1. stsum : for finding the sum in the range
2. st1 : for finding number of ones in the range
3. st2 : for finding number of twos in the range

void update(int l, int r) {
    if (r < l) return;
    if (n1(l, r) + n2(l, r) == r - l + 1) return;
    if (r - l <= 1) {
        for (int i = l; i <= r; ++i) {
            stsum.update(i, d[a[i]]);
            a[i] = d[a[i]];
            if (a[i] == 2) {
                st2.update(i, 1);
            }
        }
        return;
    }
    int mid = (r + l) / 2;
    update(l, mid);
    update(mid + 1, r);
}

Above, functions n1(l, r): returns number of 1's in the range l to r and n2(l, r) returns the number of 2's in the range.
I'm calling this function whenever there is update query.
What is the correct time complexity of the solution using this function for update?

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор salt_n_ice, 3 года назад, По-английски

Link
According to the editorial, the function becomes linear for n>=12. I dont understand why does this happen . Can someone elaborate?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -15
  • Проголосовать: не нравится

Автор salt_n_ice, 4 года назад, По-английски

Link
I tried solving this using the ordered_set gnu pbds
I did dfs along with using swap and move methods for merging the sets of each node's children. The time complexity should be nlogn but why is it giving TLE on test 15?

dfs function

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор salt_n_ice, 4 года назад, По-английски

link

My solution

As the constraint is m*n<=10^6, this solution should work under 1 second but it's taking about 1871 ms in one of the TLE'd test case. Why could this be happening?

Полный текст и комментарии »

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

Автор salt_n_ice, 4 года назад, По-английски

Problem Link

<---------------------Editorial Solution start-------------------->
If there are multiple people with the same set of skills (i.e., the same values of a), it's optimal to take each of them to the camp as they won't think they're better than everyone else.

Now consider a person i which has a different set of skills than everyone else.

If they have a strictly smaller set of skills than someone already in the group, they can safely be included in the group. If they don't, we can prove that they can't ever be included in the group. This allows us to implement a simple O(n2) solution: first take all people that have an equal set of skills as someone else, and then include everyone else who has a strictly smaller set of skills than someone already in the group.

<---------------------Editorial Solution end-------------------->

Here, let n=6 , and denoting students as A,B,C,D,E,F
a values : A,B: 01100111 , C,D : 00110100 , E: 10100011, F: 10010100
b values : anything
Now according to the editorial, we will take A,B,C,D first as they occur multiple times. But we won't take E or F because they are not "smaller" than those already present in the group(their most significant bit is set).
But I think we can take all of them without any problem as E and F have the most significant bit in common.
I don't see how all together they will not satisfy the given condition..

Полный текст и комментарии »

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Link

Code

After submitting this solution, there is a single test case out of 28 which is giving WA. And I don't see what's wrong with this solution. I don't think the problem is with constraints either.

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Link I found the number of possible ways of having sum between a and b using dp. The problem is that this number can exceed the long long limits and hence for larger values, my solution is not working.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится

Автор salt_n_ice, 4 года назад, По-английски

Problem :Link I applied the sparse table algorithm here, but I'm somehow getting a runtime error in one of the test cases. Also, I tried to see where is it going wrong, and in the code, the first "here" is printing, and the second "here" after the declaration of the minimum_query array(mq) is not running(for the test case which is giving runtime error). So I guess that the problem should be somewhere there?

Code

Полный текст и комментарии »

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Problem : Link. I simply did bfs from node 1 and found the maximum distance from this node to all other nodes

Code

So the solution is O(n+m) but still, there is TLE in one case. Why?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -2
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Pproblem: Link. I have already seen the

Solution

and I did the required changes. But still, there are 3 test cases that are not passing.

Code

Полный текст и комментарии »

  • Проголосовать: нравится
  • -3
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Problem Link

Here is my implementation: * Here, sn and en stands for starting node and ending node respectively, and mini_map is the 2d array showing path of shortest distance from starting node to all other reachable nodes. Rest is Breadth-First Search.

I don't see whats making this solution so long

Code

Полный текст и комментарии »

  • Проголосовать: нравится
  • -18
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

This is the problem I am talking about, although this has happened with some other problems as well. The solution is straightforward using dp, but I was getting TLE for 2 test cases, and the time complexity was O(n*x) so I thought some optimization was required, so I then looked at his stream and he did exactly the same as me, Only minor differences, which I changed in my solution to make it look exactly like him. But Still! It's still showing TLE while his was accepted! Here is the solution:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll N =1e9+7;
int main()
{
    int n, x;
    cin>>n>>x;
    int a[n];
    for(int i=0; i<n; ++i)
    {
        cin>>a[i];
    }
    ll cnt[x+1]={0};
    cnt[0]=1;
    for(ll i=1; i<=x; ++i)
    {
        for(ll j = 0; j<n; ++j)
        {
            if(i>=a[j])
            cnt[i] = (cnt[i]+cnt[i-a[j]])%N;
        }
    }
    cout<<cnt[x];
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -2
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

this is the problem. Here is what my solution was:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,k;
        cin>>n>>k;
        ll queued=0;
        bool gotit = false;
        for(ll i=0; i<n; ++i)
        {
            ll x;
            cin>>x;
            queued= queued + (x-k);
            if(queued<0)
            {
                cout<<i+1<<endl;
                gotit = true;
                break;
            }
        }
        if(gotit)
            continue;

        if(queued>0)
        {
            cout<<n+1+queued/k<<endl;
        }
        else cout<<n+1<<endl;
    }
}

and as expected, it was giving correct results in test cases except for one, which was giving runtime error, the error which comes by the division of 0. (and to be sure, I even tried running infinite loop if k==0, and then it showed TLE at the same test case.) Now the thing is it is given k>0 and when I tried a little different technique, it was accepted. So what is the problem here?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -11
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

Problem Here is what I tried:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
using ll = long long;

int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        string s;
        cin>>s;
        vector<ll> len, p;
        ll i=0, j=0;
        while(i<n && j<n)
        {
            while(j<n && s[i]==s[j])
                j++;
            p.push_back(j-i);
            i=j;
        }
        ll ans = 0;
        for(ll i=0; i<p.size(); ++i)
        {
            if(p[i]<2)
            {
                ans++;
                i++;
            }
            else
                ans++;
        }
        cout<<ans<<endl;
    }
}

Can someone briefly explain where am I going wrong?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -9
  • Проголосовать: не нравится

Автор salt_n_ice, история, 4 года назад, По-английски

here is the problem. At first, I couldn't solve it on my own so I took help from the editorial and this is what I came up with:

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
using ll=long long;
int main()
{
    ll n;
    cin>>n;
    vector<ll> c;
    for(ll i=0; i<n; ++i)
    {
        ll x;
        cin>>x;
        c.push_back(x);
    }
    vector<string> vec;
    for(ll i=0; i<n; ++i)
    {
        string f;
        cin>>f;
        vec.push_back(f);
    }
    ll dp[n][2];
    for(ll i=0; i<=n; ++i)
    {
        for(ll j=0; j<2; ++j)
        {
            dp[i][j] = numeric_limits<ll>::max()/2;
        }
    }
    dp[0][0]=0;
    dp[0][1]=c[0];
    ll n1 = 0, n2 = 0;
    for(ll i=1; i<n; ++i)
    {
        n1 = 0;
        n2 = 0;
        if(vec[i]>=vec[i-1])
            dp[i][0]=min(dp[i][0],dp[i-1][0]);
        else n1++;
        reverse(vec[i-1].begin(), vec[i-1].end());
        if(vec[i]>=vec[i-1])
            dp[i][0]=min(dp[i][0], dp[i-1][1]);
        else n1++;
        reverse(vec[i-1].begin(), vec[i-1].end());
        reverse(vec[i].begin(), vec[i].end());
        if(vec[i]>=vec[i-1])
            dp[i][1]=min(dp[i][1],dp[i-1][0]+c[i]);
        else n2++;
        reverse(vec[i-1].begin(), vec[i-1].end());
        if(vec[i]>vec[i-1])
            dp[i][1]=min(dp[i][1], dp[i-1][1]+c[i]);
        else n2++;
        reverse(vec[i-1].begin(), vec[i-1].end());
        reverse(vec[i].begin(), vec[i].end());
        if(n1==2 && n2==2)
            break;

    }
    if(n1==2 && n2==2)
        cout<<-1;
    else cout<<min(dp[n-1][0], dp[n-1][1]);
}

but it's giving me wrong answer.Can anyone help with whats wrong?

Полный текст и комментарии »

  • Проголосовать: нравится
  • -2
  • Проголосовать: не нравится