답안 #964228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
964228 2024-04-16T13:05:26 Z guechotjrhh Safety (NOI18_safety) C++14
71 / 100
2000 ms 3748 KB
#include<stdio.h>
#include<vector>
#pragma warning (disable:4996)
using namespace std;

#include<algorithm>
#include<iostream>
#define ll long long
ll ab(ll n) { return n > 0 ? n : -n; }
int sz = 5001, inf = 1e9;
long long func(int n, int h, vector<int> s) {
	if (h == 0) {
		sort(s.begin(), s.end());
		ll r = 0;
		for (int i = 0; i < n; i++) r += ab(s[i] - s[n / 2]);
		return r;
	}

	vector<int> le = { 0 }, ri = { inf };
	ll val = 0;
	
	//vector<ll> dp(sz, 0);

	//int mi = 0;

	for (int i = 0; i < n; i++) {
		for (int j = 0; j < ri.size(); j++) {
			ri[j] += h; if (ri[j] > inf) ri[j] = inf;
		}
		for (int j = 0; j < le.size(); j++) {
			le[j] -= h; if (le[j] < 0) le[j] = 0;
		}
		
		vector<int> nle, nri;
		ll nval;
		if (s[i] <= ri[0] && s[i] >= le[0]) {
			nval = val;
			nle = { s[i] };
			//if (s[i] != le[0]) nle.push_back(le[0]);
			for (int j = 0; j < le.size(); j++) nle.push_back(le[j]);
			nri = { s[i] };
			//if (ri[0] != s[i]) nri.push_back(ri[0]);
			for (int j = 0; j < ri.size(); j++) nri.push_back(ri[j]);
		}
		else if (s[i] > ri[0]) {
			nval = val + s[i] - ri[0];
			nle = { ri[0] };
			//if (ri[0] != le[0]) nle.push_back(le[0]);
			for (int j = 0; j < le.size(); j++) nle.push_back(le[j]);
			nri = { ri[1], s[i], s[i] };
			for (int j = 2; j < ri.size();j++) nri.push_back(ri[j]);
			sort(nri.begin(), nri.end());
		}
		else {
			nval = val + le[0] - s[i];
			nle = { le[1], s[i], s[i] };
			for (int j = 2; j < le.size(); j++) nle.push_back(le[j]);
			sort(nle.begin(), nle.end());
			reverse(nle.begin(), nle.end());
			nri = { le[0] };
			//if (ri[0] != le[0]) nri.push_back(ri[0]);
			for (int j = 0; j < ri.size();j++) nri.push_back(ri[j]);
		}
		val = nval;
		ri = nri;
		le = nle;
		
		/*vector<ll> sdp(sz);
		for (int j = le[0]; j <= ri[0]; j++) sdp[j] = val;
		for (int j = 1; j < le.size(); j++) for (int w = le[j - 1] - 1; w >= le[j]; w--) sdp[w] = sdp[w + 1] + j;
		for (int j = 1; j < ri.size(); j++) for (int w = ri[j - 1] + 1; w <= ri[j]; w++) sdp[w] = sdp[w - 1] + j;
		//cout << val << endl;
		//for (int i : le) cout << i << ' '; cout << endl;
		//for (int i : ri) cout << i << ' '; cout << endl;


		vector<ll> ndp(sz);
		for (int j = 0; j < sz; j++) {
			ndp[j] = ab(j - s[i]);
			int d = max(0, j - h), u = min(sz - 1, j + h);
			if (d <= mi && u >= mi) ndp[j] += dp[mi];
			if (d > mi) ndp[j] += dp[d];
			if (u < mi) ndp[j] += dp[u];
			//ll mn = 1e18;
			//for (int k = max(0, j - h); k <= min(sz-1, j + h); k++) mn = min(mn, dp[k]);
			//ndp[j] += mn;
		}
		//for (int j : dp) printf("%d ", j); printf("\n");
		dp = ndp;

		mi = 0;
		for (int i = 0; i < sz; i++) if (dp[i] < dp[mi]) mi = i;

		if (sdp != dp) {
			cout << i << endl;
			for (int j : sdp) cout << j << ' '; cout << endl;
			for (int j : dp) cout << j << ' '; cout << endl;
			return 0;
		}*/
	}
	return val;

	/*vector<ll> dp(sz, 0);

	int mi = 0;

	for (int i = 0; i < n; i++) {
		vector<ll> ndp(sz);
		for (int j = 0; j < sz; j++) {
			ndp[j] = ab(j - s[i]);
			int d = max(0, j - h), u = min(sz - 1, j + h);
			if (d <= mi && u >= mi) ndp[j] += dp[mi];
			if (d > mi) ndp[j] += dp[d];
			if (u < mi) ndp[j] += dp[u];
			//ll mn = 1e18;
			//for (int k = max(0, j - h); k <= min(sz-1, j + h); k++) mn = min(mn, dp[k]);
			//ndp[j] += mn;
		}
		//for (int j : dp) printf("%d ", j); printf("\n");
		dp = ndp;

		mi = 0;
		for (int i = 0; i < sz; i++) if (dp[i] < dp[mi]) mi = i;
	}

	ll mn = 1e18;
	for (int i = 0; i < sz; i++) mn = min(mn, dp[i]);

	return mn;*/
}

int main() {
	int N, H;
	scanf("%d%d", &N, &H);
	vector<int> S(N);
	for (int i = 0; i < N; i++) scanf("%d", &S[i]);

	long long ans = func(N, H, move(S));

	printf("%lld\n", ans);
}

Compilation message

safety.cpp:3: warning: ignoring '#pragma warning ' [-Wunknown-pragmas]
    3 | #pragma warning (disable:4996)
      | 
safety.cpp: In function 'long long int func(int, int, std::vector<int>)':
safety.cpp:27:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   27 |   for (int j = 0; j < ri.size(); j++) {
      |                   ~~^~~~~~~~~~~
safety.cpp:30:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |   for (int j = 0; j < le.size(); j++) {
      |                   ~~^~~~~~~~~~~
safety.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |    for (int j = 0; j < le.size(); j++) nle.push_back(le[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp:43:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   43 |    for (int j = 0; j < ri.size(); j++) nri.push_back(ri[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |    for (int j = 0; j < le.size(); j++) nle.push_back(le[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |    for (int j = 2; j < ri.size();j++) nri.push_back(ri[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp:57:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |    for (int j = 2; j < le.size(); j++) nle.push_back(le[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |    for (int j = 0; j < ri.size();j++) nri.push_back(ri[j]);
      |                    ~~^~~~~~~~~~~
safety.cpp: In function 'int main()':
safety.cpp:134:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |  scanf("%d%d", &N, &H);
      |  ~~~~~^~~~~~~~~~~~~~~~
safety.cpp:136:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |  for (int i = 0; i < N; i++) scanf("%d", &S[i]);
      |                              ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 436 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 2296 KB Output is correct
2 Correct 29 ms 2824 KB Output is correct
3 Correct 30 ms 2904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 436 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 436 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 2 ms 556 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 3 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 2 ms 348 KB Output is correct
14 Correct 0 ms 344 KB Output is correct
15 Correct 2 ms 348 KB Output is correct
16 Correct 2 ms 436 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 2 ms 556 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 2 ms 344 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 3 ms 348 KB Output is correct
28 Correct 219 ms 676 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 163 ms 884 KB Output is correct
31 Correct 44 ms 604 KB Output is correct
32 Correct 53 ms 644 KB Output is correct
33 Correct 105 ms 728 KB Output is correct
34 Correct 68 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 436 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 2 ms 348 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 436 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 2 ms 556 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 2 ms 344 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 219 ms 676 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 163 ms 884 KB Output is correct
37 Correct 44 ms 604 KB Output is correct
38 Correct 53 ms 644 KB Output is correct
39 Correct 105 ms 728 KB Output is correct
40 Correct 68 ms 600 KB Output is correct
41 Correct 400 ms 672 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 73 ms 668 KB Output is correct
44 Correct 145 ms 932 KB Output is correct
45 Correct 58 ms 740 KB Output is correct
46 Correct 50 ms 656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 600 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 600 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 344 KB Output is correct
16 Correct 1 ms 436 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 20 ms 2296 KB Output is correct
20 Correct 29 ms 2824 KB Output is correct
21 Correct 30 ms 2904 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 2 ms 348 KB Output is correct
25 Correct 2 ms 436 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 2 ms 556 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 2 ms 344 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 3 ms 348 KB Output is correct
37 Correct 219 ms 676 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 163 ms 884 KB Output is correct
40 Correct 44 ms 604 KB Output is correct
41 Correct 53 ms 644 KB Output is correct
42 Correct 105 ms 728 KB Output is correct
43 Correct 68 ms 600 KB Output is correct
44 Correct 400 ms 672 KB Output is correct
45 Correct 1 ms 348 KB Output is correct
46 Correct 73 ms 668 KB Output is correct
47 Correct 145 ms 932 KB Output is correct
48 Correct 58 ms 740 KB Output is correct
49 Correct 50 ms 656 KB Output is correct
50 Execution timed out 2070 ms 3748 KB Time limit exceeded
51 Halted 0 ms 0 KB -