#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 = INF;
dp[i][j].S = a[i];
}
}
if(a[0]<=m)
{
dp[0][0].F = 0;
}
else
{
dp[0][2].F = 1;
dp[0][2].S = m;
}
FOR(i, 1, n)
{
//0
if(dp[i-1][0].F!=INF)
{
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!=INF)
{
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!=INF)
{
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!=INF)
{
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])
{
dp[i][1].F = min(dp[i][1].F, val+1);
dp[i][1].S = hgt+m;
}
}
}
if(dp[i-1][1].F!=INF)
{
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])
{
dp[i][1].F = min(dp[i][1].F, val+1);
dp[i][1].S = hgt+m;
}
}
}
if(dp[i-1][2].F!=INF)
{
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])
{
dp[i][1].F = min(dp[i][1].F, val+1);
dp[i][1].S = hgt+m;
}
}
}
//2
if(dp[i-1][0].F!=INF)
{
hgt = dp[i-1][0].S; val = dp[i-1][0].F; curr = a[i];
if(hgt+m<curr)
{
dp[i][2].F = min(dp[i][2].F, val+1);
dp[i][2].S = hgt+m;
}
}
if(dp[i-1][1].F!=INF)
{
hgt = dp[i-1][1].S; val = dp[i-1][1].F; curr = a[i];
if(hgt+m<curr)
{
dp[i][2].F = min(dp[i][2].F, val+1);
dp[i][2].S = hgt+m;
}
}
if(dp[i-1][2].F!=INF)
{
hgt = dp[i-1][2].S; val = dp[i-1][2].F; curr = a[i];
if(hgt+m<curr)
{
dp[i][2].F = min(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==INF?0: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 |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
12 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |