Saturday, 9 September 2017

Identifying tea (UVa 13012, UVaLive 7211, Regionals 2015 >> Latin America)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
///     Raihan Ruhin (username : raihanruhin)
///     Online resume: https://raihanruhin.github.io

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0);

    int tc, n, x, cnt;
    //cin>>tc;
    while(cin>>n)
    {
        cnt=0;
        for(int i=0;i<5;i++)
        {
            cin>>x;
            if(x==n) cnt++;
        }
        cout<< cnt <<endl;
    }
    return 0;
}

Thursday, 24 August 2017

Maximum sum increasing subsequence

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
///     Raihan Ruhin (username: raihanruhin)
///     Resume: https://raihanruhin.github.io

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int dp[102][102], arr[102], n;

int func(int now, int last)
{
    if(now>n) return 0;

    int &ret = dp[now][last];
    if(ret!=-1) return ret;
    ret=0;

    ret = func(now+1, last);
    if(arr[last]<arr[now])
        ret = max(ret, func(now+1, now)+arr[now]);

    return ret;
}

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0);

    int tc, kk=1, x;
    cin>>tc;

    while(tc--)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>arr[i];
        SET(dp);
        cout<<func(0, 0)<<endl;
        //cout<<"Case "<<kk++<<": "<< <<"\n";
    }
    return 0;
}

Equilibrium point

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
///     Raihan Ruhin (username: raihanruhin)
///     Resume: https://raihanruhin.github.io

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0);

    int tc, kk=1, n, x, consecutive_sum[102];
    bool found;
    consecutive_sum[0]=0;
    cin>>tc;

    while(tc--)
    {
        found =false;
        cin>>n;
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            consecutive_sum[i] = consecutive_sum[i-1] + x;
        }
        for(int i=1;i<=n;i++)
        {
            if(consecutive_sum[n]-consecutive_sum[i] == consecutive_sum[i-1])
            {
                found =true;
                cout<<i<<endl;
                break;
            }
        }
        if(!found)
            cout<<-1<<endl;
        //cout<<"Case "<<kk++<<": "<< <<"\n";
    }
    return 0;
}

Sort an array of 0s, 1s and 2s

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
///     Raihan Ruhin (username: raihanruhin)
///     Resume: https://raihanruhin.github.io

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0);

    int tc, kk=1, n, x;
    cin>>tc;

    while(tc--)
    {
        cin>>n;
        int zero=0, one=0, two=0;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            if(!x) zero++;
            else if(x==1) one++;
            else two++;
        }
        for(int i=0;i<n;i++)
        {
            if(i) cout<<" ";
            if(zero)
            {
                cout<<0;
                zero--;
            }
            else if(one)
            {
                cout<<1;
                one--;
            }
            else
            {
                cout<<2;
                two--;
            }
        }
        cout<<endl;
        //cout<<"Case "<<kk++<<": "<< <<"\n";
    }
    return 0;
}

Subarray with given sum


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
///     Raihan Ruhin (username: raihanruhin)
///     Resume: https://raihanruhin.github.io

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

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    ios_base::sync_with_stdio(0), cin.tie(0);

    int tc, kk=1, n, x, arr[102], s;
    cin>>tc;

    while(tc--)
    {
        cin>>n>>s;
        int i;
        for(i=1;i<=n;i++)
            cin>>arr[i];
        arr[0]=0;
        int st = 1, x=0;
        bool found = false;
        i=1;
        while(i<=n || x>s)
        {
            if(x>s)
            {
                x -= arr[st];
                st++;
                i--;
            }
            else if(x<s)
            {
                x += arr[i];
            }
            if(x==s)
            {
                found=true;
                break;
            }
            i++;
        }
        if(found)
            cout<<st<<" "<<i<<endl;
        else 
            cout<<-1<<endl;
        //cout<<"Case "<<kk++<<": "<< <<"\n";
    }
    return 0;
}