Submission #877988

# Submission time Handle Problem Language Result Execution time Memory
877988 2023-11-24T01:35:22 Z fanwen Safety (NOI18_safety) C++17
24 / 100
159 ms 196192 KB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define file(name)                  \
    if(fopen(name".inp", "r"))      \
        freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); \

const int MAX = 2e5 + 5;

int N, H, A[MAX];

namespace sub1 {
	bool check() {
		return N <= 5000 && *max_element(A + 1, A + N + 1) <= 5000;
	}

	long long dp[5000][5000];

	void solve() {
		int MaxA = *max_element(A + 1, A + N + 1);
		memset(dp, 0x3f, sizeof dp);
		memset(dp[0], 0, sizeof dp[0]);
		for (int i = 1; i <= N; ++i) {
			deque <int> q;
			int j = 0;
			for (int h = 0; h <= MaxA; ++h) {
				int l = max(0, h - H), r = min(MaxA, h + H);
				while(j <= r) {
					while(!q.empty() && dp[i - 1][q.back()] >= dp[i - 1][j]) q.pop_back();
					q.push_back(j);
					j++;
				}
				while(!q.empty() && h - q.front() > H) q.pop_front();
				if(!q.empty()) dp[i][h] = dp[i - 1][q.front()] + abs(h - A[i]);
			}
		}
		cout << *min_element(dp[N], dp[N] + MaxA + 1); exit(0);
	}
}

void you_make_it(void) {
    cin >> N >> H;
    for (int i = 1; i <= N; ++i) cin >> A[i];
    if(sub1::check()) sub1::solve();
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    file("SAFETY");
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.

Compilation message

safety.cpp: In function 'void sub1::solve()':
safety.cpp:31:9: warning: unused variable 'l' [-Wunused-variable]
   31 |     int l = max(0, h - H), r = min(MaxA, h + H);
      |         ^
safety.cpp: In function 'int main()':
safety.cpp:10:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); \
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:57:5: note: in expansion of macro 'file'
   57 |     file("SAFETY");
      |     ^~~~
safety.cpp:10:49: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |         freopen(name".inp", "r", stdin), freopen(name".out", "w", stdout); \
      |                                          ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:57:5: note: in expansion of macro 'file'
   57 |     file("SAFETY");
      |     ^~~~
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 196020 KB Output is correct
2 Correct 22 ms 196012 KB Output is correct
3 Correct 23 ms 195924 KB Output is correct
4 Correct 22 ms 195932 KB Output is correct
5 Correct 23 ms 195932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 348 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 11 ms 2136 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
8 Correct 22 ms 196020 KB Output is correct
9 Correct 22 ms 196012 KB Output is correct
10 Correct 23 ms 195924 KB Output is correct
11 Correct 22 ms 195932 KB Output is correct
12 Correct 23 ms 195932 KB Output is correct
13 Correct 24 ms 195924 KB Output is correct
14 Correct 24 ms 195932 KB Output is correct
15 Correct 25 ms 196168 KB Output is correct
16 Correct 24 ms 195924 KB Output is correct
17 Correct 25 ms 196036 KB Output is correct
18 Correct 25 ms 196084 KB Output is correct
19 Correct 24 ms 196008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
8 Correct 22 ms 196020 KB Output is correct
9 Correct 22 ms 196012 KB Output is correct
10 Correct 23 ms 195924 KB Output is correct
11 Correct 22 ms 195932 KB Output is correct
12 Correct 23 ms 195932 KB Output is correct
13 Correct 24 ms 195924 KB Output is correct
14 Correct 24 ms 195932 KB Output is correct
15 Correct 25 ms 196168 KB Output is correct
16 Correct 24 ms 195924 KB Output is correct
17 Correct 25 ms 196036 KB Output is correct
18 Correct 25 ms 196084 KB Output is correct
19 Correct 24 ms 196008 KB Output is correct
20 Correct 35 ms 196192 KB Output is correct
21 Correct 37 ms 195924 KB Output is correct
22 Correct 34 ms 195932 KB Output is correct
23 Correct 32 ms 195924 KB Output is correct
24 Correct 35 ms 196184 KB Output is correct
25 Correct 37 ms 196160 KB Output is correct
26 Correct 36 ms 195924 KB Output is correct
27 Correct 35 ms 196024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
8 Correct 22 ms 196020 KB Output is correct
9 Correct 22 ms 196012 KB Output is correct
10 Correct 23 ms 195924 KB Output is correct
11 Correct 22 ms 195932 KB Output is correct
12 Correct 23 ms 195932 KB Output is correct
13 Correct 24 ms 195924 KB Output is correct
14 Correct 24 ms 195932 KB Output is correct
15 Correct 25 ms 196168 KB Output is correct
16 Correct 24 ms 195924 KB Output is correct
17 Correct 25 ms 196036 KB Output is correct
18 Correct 25 ms 196084 KB Output is correct
19 Correct 24 ms 196008 KB Output is correct
20 Correct 35 ms 196192 KB Output is correct
21 Correct 37 ms 195924 KB Output is correct
22 Correct 34 ms 195932 KB Output is correct
23 Correct 32 ms 195924 KB Output is correct
24 Correct 35 ms 196184 KB Output is correct
25 Correct 37 ms 196160 KB Output is correct
26 Correct 36 ms 195924 KB Output is correct
27 Correct 35 ms 196024 KB Output is correct
28 Incorrect 159 ms 196188 KB Output isn't correct
29 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
8 Correct 22 ms 196020 KB Output is correct
9 Correct 22 ms 196012 KB Output is correct
10 Correct 23 ms 195924 KB Output is correct
11 Correct 22 ms 195932 KB Output is correct
12 Correct 23 ms 195932 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 195924 KB Output is correct
2 Correct 23 ms 196044 KB Output is correct
3 Correct 23 ms 195920 KB Output is correct
4 Correct 22 ms 196144 KB Output is correct
5 Correct 22 ms 196048 KB Output is correct
6 Correct 22 ms 195932 KB Output is correct
7 Correct 22 ms 195932 KB Output is correct
8 Correct 22 ms 196020 KB Output is correct
9 Correct 22 ms 196012 KB Output is correct
10 Correct 23 ms 195924 KB Output is correct
11 Correct 22 ms 195932 KB Output is correct
12 Correct 23 ms 195932 KB Output is correct
13 Incorrect 0 ms 348 KB Output isn't correct
14 Halted 0 ms 0 KB -