# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
964094 | 2024-04-16T10:16:08 Z | guechotjrhh | Safety (NOI18_safety) | C++14 | 35 ms | 3060 KB |
#include<stdio.h> #include<vector> #pragma warning (disable:4996) using namespace std; #include<algorithm> #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; for (int i = 0; i < n; i++) { for (int j = 0; j < ri.size(); j++) { ri[j] += h; if (ri[j] > 1e9) ri[j] = 1e9; } 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] }; for (int j : le) if (j < s[i]) nle.push_back(j); nri = { s[i] }; for (int j : ri) if (j > s[i]) nri.push_back(j); } else if (s[i] > ri[0]) { nval = val + s[i] - ri[0]; nle = { ri[0] }; for (int j : le) if (j < ri[0]) nle.push_back(j); nri = { ri[1] }; for (int j = 2; j < ri.size();j++) nri.push_back(ri[j]); } else { nval = val + le[0] - s[i]; nle = { le[1] }; for (int j = 2; j < le.size(); j++) nle.push_back(le[j]); nri = { le[0] }; for (int j : ri) if (j > le[0]) nri.push_back(j); } val = nval; ri = nri; le = nle; } 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
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 344 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 1 ms | 348 KB | Output is correct |
5 | Correct | 1 ms | 348 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 344 KB | Output is correct |
2 | Incorrect | 0 ms | 348 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 19 ms | 2140 KB | Output is correct |
2 | Correct | 35 ms | 3060 KB | Output is correct |
3 | Correct | 31 ms | 2992 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 0 ms | 348 KB | Output is correct |
2 | Correct | 0 ms | 348 KB | Output is correct |
3 | Correct | 0 ms | 348 KB | Output is correct |
4 | Correct | 0 ms | 348 KB | Output is correct |
5 | Correct | 0 ms | 348 KB | Output is correct |
6 | Correct | 0 ms | 348 KB | Output is correct |
7 | Incorrect | 1 ms | 348 KB | Output isn't correct |
8 | Halted | 0 ms | 0 KB | - |