Submission #1294111

#TimeUsernameProblemLanguageResultExecution timeMemory
1294111thuhienneSafety (NOI18_safety)C++20
35 / 100
2095 ms800 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const ll inf = 1e18;

#define re exit(0);
#define thuhien ""
#define prev __prev__

int n,diff,arr[200009];

ll dp[5009],prev[5009];

int main() {
//  ios_base::sync_with_stdio(0);cin.tie(nullptr);
	if (fopen(thuhien".inp","r")) {
		freopen(thuhien".inp","r",stdin);
		freopen(thuhien".out","w",stdout);
	}

	cin >> n >> diff;
	for (int i = 1;i <= n;i++) cin >> arr[i];
	
	if (diff > 5000) {
		cout << 0;
		return 0;
	}
	
	for (int i = 0;i <= 5000;i++) dp[i] = abs(arr[1] - i);
	
	for (int i = 2;i <= n;i++) {
		for (int j = 0;j <= 5000;j++) prev[j] = dp[j],dp[j] = inf;
		
		deque <int> dq;
		
		for (int j = 0;j < diff;j++) {
			while (dq.size() && prev[dq.back()] >= prev[j]) dq.pop_back();
			dq.push_back(j);
		}
		for (int j = 0;j <= 5000;j++) {
			if (j + diff <= 5000) {
				while (dq.size() && prev[dq.back()] >= prev[j + diff]) dq.pop_back();
				dq.push_back(j + diff);
			}
			while (dq.size() && dq.front() < j - diff) dq.pop_front();
			dp[j] = prev[dq.front()] + abs(j - arr[i]);
		}
	}
	
	cout << *min_element(dp,dp + 5001);

}

Compilation message (stderr)

safety.cpp: In function 'int main()':
safety.cpp:19:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |                 freopen(thuhien".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
safety.cpp:20:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |                 freopen(thuhien".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...