답안 #146575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
146575 2019-08-24T12:52:40 Z jwvg0425 Safety (NOI18_safety) C++17
9 / 100
2000 ms 3704 KB
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
#define MOD 998244353
#define BUCKET 700

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

int n, h;
void h0()
{
	vector<int> s(n);

	for (int i = 0; i < n; i++)
		scanf("%d", &s[i]);

	sort(all(s));

	int m = s[n / 2];
	i64 ans = 0;

	for (int i = 0; i < n; i++)
		ans += abs(m - s[i]);

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

int arr[200005];

i64 table[505][405];

i64 solve(int idx, int v)
{
	if (idx == n - 1)
		return 0;

	if (table[idx][v] != -1)
		return table[idx][v];

	auto& res = table[idx][v];

	for (int s = max(0, v - h); s <= (400, v + h); s++)
	{
		auto now = abs(arr[idx + 1] - s) + solve(idx + 1, s);

		if (res == -1 || now < res)
			res = now;
	}

	return res;
}

int main()
{
	scanf("%d %d", &n, &h);

	if (h == 0)
	{
		h0();
		return 0;
	}

	for (int i = 0; i < n; i++)
		scanf("%d", &arr[i]);

	memset(table, -1, sizeof(table));
	i64 ans = solve(0, arr[0]);
	for (int i = 0; i <= 400; i++)
		ans = min(ans, abs(i - arr[0]) + solve(0, i));
	
	printf("%lld\n", ans);

	return 0;
}

Compilation message

safety.cpp: In function 'i64 solve(int, int)':
safety.cpp:62:45: warning: left operand of comma operator has no effect [-Wunused-value]
  for (int s = max(0, v - h); s <= (400, v + h); s++)
                                             ^
safety.cpp: In function 'void h0()':
safety.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &s[i]);
   ~~~~~^~~~~~~~~~~~~
safety.cpp: In function 'int main()':
safety.cpp:75:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &h);
  ~~~~~^~~~~~~~~~~~~~~~~
safety.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &arr[i]);
   ~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 3 ms 1912 KB Output is correct
3 Correct 3 ms 1912 KB Output is correct
4 Correct 3 ms 1912 KB Output is correct
5 Correct 3 ms 1912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 6 ms 3704 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 888 KB Output is correct
2 Correct 57 ms 1144 KB Output is correct
3 Correct 56 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1912 KB Output is correct
2 Correct 4 ms 1912 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Execution timed out 2079 ms 1912 KB Time limit exceeded
5 Halted 0 ms 0 KB -