Submission #431477

# Submission time Handle Problem Language Result Execution time Memory
431477 2021-06-17T12:06:54 Z idas Rabbit Carrot (LMIO19_triusis) C++11
0 / 100
1 ms 376 KB
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define SZ(x) ((int)((x).size()))
#define LE(vec) vec[vec.size()-1]
#define TSTS int t; cin >> t; while(t--)solve()

const int INF = 1e9;
const long long LINF = 1e18;
const long double PI = asin(1)*2;
const int MOD = 1e9+7;

using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
typedef long double ld;

void setIO()
{
    FAST_IO;
}

void setIO (string s) {
    setIO();
 	freopen((s+".in").c_str(),"r",stdin);
 	freopen((s+".out").c_str(),"w",stdout);
}

const int N = 2e5+1000;

ll n, m, a[N];
pair<ll, ll> dp[N][3];

ll hgt, val, curr;

int main()
{
    setIO();
    cin >> n >> m;
    FOR(i, 0, n)
    {
        cin >> a[i];
    }

    FOR(i, 0, n)
    {
        FOR(j, 0, 3)
        {
            dp[i][j].F = LINF;
            dp[i][j].S = a[i];
        }
    }

    if(m>=a[0])
        dp[0][0].F = 0;
    if(a[0]>m)
    {
        dp[0][2].F = 1;
        dp[0][2].S = m;
    }
    if(a[0]<m)
    {
        dp[0][1].F = 1;
        dp[0][1].S = m;
    }


    FOR(i, 1, n)
    {
        //0
        if(dp[i-1][0].F!=LINF)
        {
            hgt = dp[i-1][0].S; val = dp[i-1][0].F; curr = a[i];
            if(hgt+m>=curr)
            {
                dp[i][0].F = min(dp[i][0].F, val);
            }
        }

        if(dp[i-1][1].F!=LINF)
        {
            hgt = dp[i-1][1].S; val = dp[i-1][1].F; curr = a[i];
            if(hgt+m>=curr)
            {
                dp[i][0].F = min(dp[i][0].F, val);
            }
        }

        if(dp[i-1][2].F!=LINF)
        {
            hgt = dp[i-1][2].S; val = dp[i-1][2].F; curr = a[i];
            if(hgt+m>=curr)
            {
                dp[i][0].F = min(dp[i][0].F, val);
            }
        }

        //1
        if(dp[i-1][0].F!=LINF)
        {
            hgt = dp[i-1][0].S; val = dp[i-1][0].F; curr = a[i];
            if(hgt+m>=curr)
            {
                if(curr+m<a[i+1] && hgt+m+m>=a[i+1])
                {
                    if(dp[i][1].F>=val+1)
                    {
                        dp[i][1].F = val+1;
                        dp[i][1].S = hgt+m;
                    }
                }
            }
        }

        if(dp[i-1][2].F!=LINF)
        {
            hgt = dp[i-1][2].S; val = dp[i-1][2].F; curr = a[i];
            if(hgt+m>=curr)
            {
                if(curr+m<a[i+1] && hgt+m+m>=a[i+1])
                {
                    if(dp[i][1].F>=val+1)
                    {
                        dp[i][1].F = val+1;
                        dp[i][1].S = hgt+m;
                    }
                }
            }
        }

        if(dp[i-1][1].F!=LINF)
        {
            hgt = dp[i-1][1].S; val = dp[i-1][1].F; curr = a[i];
            if(hgt+m>=curr)
            {
                if(curr+m<a[i+1] && hgt+m+m>=a[i+1])
                {
                    if(dp[i][1].F>val+1)
                    {
                        dp[i][1].F = val+1;
                        dp[i][1].S = hgt+m;
                    }
                }
            }
        }

        //2
        if(dp[i-1][0].F!=LINF)
        {
            hgt = dp[i-1][0].S; val = dp[i-1][0].F; curr = a[i];
            if(hgt+m<curr)
            {
                if(dp[i][2].F>val+1)
                {
                    dp[i][2].F = val+1;
                    dp[i][2].S = hgt+m;
                }
            }
        }

        if(dp[i-1][1].F!=LINF)
        {
            hgt = dp[i-1][1].S; val = dp[i-1][1].F; curr = a[i];
            if(hgt+m<curr)
            {
                if(dp[i][2].F>val+1)
                {
                    dp[i][2].F = val+1;
                    dp[i][2].S = hgt+m;
                }
            }
        }

        if(dp[i-1][2].F!=LINF)
        {
            hgt = dp[i-1][2].S; val = dp[i-1][2].F; curr = a[i];
            if(hgt+m<curr)
            {
                if(dp[i][2].F>val+1)
                {
                    dp[i][2].F = val+1;
                    dp[i][2].S = hgt+m;
                }
            }
        }
    }

    ll ans = min(dp[n-1][0].F, dp[n-1][1].F);
    ans = min(ans, dp[n-1][2].F);
    cout << ans;
}

Compilation message

triusis.cpp: In function 'void setIO(std::string)':
triusis.cpp:31:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |   freopen((s+".in").c_str(),"r",stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
triusis.cpp:32:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |   freopen((s+".out").c_str(),"w",stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 232 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Incorrect 1 ms 276 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 232 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Incorrect 1 ms 276 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 232 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Incorrect 1 ms 276 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 232 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Incorrect 1 ms 276 KB Output isn't correct
13 Halted 0 ms 0 KB -