problem_id
stringlengths 6
6
| language
stringclasses 2
values | original_status
stringclasses 3
values | original_src
stringlengths 19
243k
| changed_src
stringlengths 19
243k
| change
stringclasses 3
values | i1
int64 0
8.44k
| i2
int64 0
8.44k
| j1
int64 0
8.44k
| j2
int64 0
8.44k
| error
stringclasses 270
values | stderr
stringlengths 0
226k
|
---|---|---|---|---|---|---|---|---|---|---|---|
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
// statics
using namespace std;
using int64 = int_fast64_t;
using PAIR = pair<int64, int64>;
constexpr int INF = 1 << 30;
constexpr int64 LINF = 1LL << 60;
constexpr int MOD = 1e9 + 7;
constexpr int MAX_N = 3e5 + 1;
// init/input
#define int int64
#define INIT \
ios::sync_with_stdio(false); \
cin.tie(0);
#define VAR(type, ...) \
type __VA_ARGS__; \
MACRO_VAR_Scan(__VA_ARGS__);
template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; }
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First &first, Rest &...rest) {
cin >> first;
MACRO_VAR_Scan(rest...);
}
#define VEC(type, c, n) \
vector<type> c(n); \
for (auto &&i : c) \
cin >> i;
// out
#define OUT(dist) cout << (dist);
#define FOUT(n, dist) cout << fixed << setprecision(n) << (dist);
#define SP cout << " ";
#define BR cout << endl;
#define zero(n) cout << setfill('0') << right << setw(n)
#define debug(x) \
cerr << #x << ":" << (x); \
BR;
// utility
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, a) for (auto &&i : (a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dp[MAX_N];
signed main() {
INIT;
VAR(int, n, k);
VEC(int, h, n);
fill_n(dp, MAX_N, LINF);
dp[0] = 0;
REP(i, n) {
FOR(j, 1, k + 1) {
if (i - j >= 0) {
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
debug(i);
debug(k);
}
}
}
REP(i, n) { debug(dp[i]); }
OUT(dp[n - 1]) BR;
return 0;
}
|
#include <bits/stdc++.h>
// statics
using namespace std;
using int64 = int_fast64_t;
using PAIR = pair<int64, int64>;
constexpr int INF = 1 << 30;
constexpr int64 LINF = 1LL << 60;
constexpr int MOD = 1e9 + 7;
constexpr int MAX_N = 3e5 + 1;
// init/input
#define int int64
#define INIT \
ios::sync_with_stdio(false); \
cin.tie(0);
#define VAR(type, ...) \
type __VA_ARGS__; \
MACRO_VAR_Scan(__VA_ARGS__);
template <typename T> void MACRO_VAR_Scan(T &t) { cin >> t; }
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First &first, Rest &...rest) {
cin >> first;
MACRO_VAR_Scan(rest...);
}
#define VEC(type, c, n) \
vector<type> c(n); \
for (auto &&i : c) \
cin >> i;
// out
#define OUT(dist) cout << (dist);
#define FOUT(n, dist) cout << fixed << setprecision(n) << (dist);
#define SP cout << " ";
#define BR cout << endl;
#define zero(n) cout << setfill('0') << right << setw(n)
#define debug(x) \
cerr << #x << ":" << (x); \
BR;
// utility
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(i, a) for (auto &&i : (a))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dp[MAX_N];
signed main() {
INIT;
VAR(int, n, k);
VEC(int, h, n);
fill_n(dp, MAX_N, LINF);
dp[0] = 0;
REP(i, n) {
FOR(j, 1, k + 1) {
if (i - j >= 0) {
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
// debug(i);
// debug(k);
}
}
}
REP(i, n) { debug(dp[i]); }
OUT(dp[n - 1]) BR;
return 0;
}
|
replace
| 76 | 78 | 76 | 78 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
const int INF = 1e9 + 7;
using namespace std;
ll dp[100005];
ll solve(int n, int k, vector<int> &h) {
dp[1] = 0;
for (int i = 2; i <= n; i++) {
ll ans = INF;
for (int j = 1; j <= k; j++) {
ll tmp = INF;
if (i - j >= 1) {
tmp = abs(h[i] - h[i - j]) + dp[i - j];
ans = min(tmp, ans);
dp[i] = ans;
} else
break;
}
}
// op1 = abs(h[st]-h[st+1]) +solve(st+1,n,h);
// if(st+2<=n)
// op2 = abs(h[st]-h[st+2])+solve(st+2,n,h);
// cout<<op1<<" "<<op2<<endl;
return dp[n];
}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> h;
h.push_back(0);
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
h.push_back(tmp);
}
if (n == 1) {
cout << 0;
return 0;
}
memset(dp, 0, n + 1);
cout << solve(n, k, h);
}
|
#include <bits/stdc++.h>
#define ll long long int
const int INF = 1e9 + 7;
using namespace std;
ll dp[100005];
ll solve(int n, int k, vector<int> &h) {
dp[1] = 0;
for (int i = 2; i <= n; i++) {
ll ans = INF;
for (int j = 1; j <= k; j++) {
ll tmp = INF;
if (i - j >= 1) {
tmp = abs(h[i] - h[i - j]) + dp[i - j];
ans = min(tmp, ans);
dp[i] = ans;
} else
break;
}
}
// op1 = abs(h[st]-h[st+1]) +solve(st+1,n,h);
// if(st+2<=n)
// op2 = abs(h[st]-h[st+2])+solve(st+2,n,h);
// cout<<op1<<" "<<op2<<endl;
return dp[n];
}
int main() {
// #ifndef ONLINE_JUDGE
// // for getting input from input.txt
// freopen("input.txt", "r", stdin);
// // for writing output to output.txt
// freopen("output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> h;
h.push_back(0);
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
h.push_back(tmp);
}
if (n == 1) {
cout << 0;
return 0;
}
memset(dp, 0, n + 1);
cout << solve(n, k, h);
}
|
replace
| 29 | 35 | 29 | 35 |
-11
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define input_vector(v, n) \
for (long long i = 0; i < n; i++) { \
cin >> v[i]; \
}
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pb push_back
using namespace std;
typedef long long ll;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> v(n);
input_vector(v, n);
vector<ll> dp(n);
dp[0] = 0;
for (ll i = 1; i < n; i++) {
dp[i] = dp[i - 1] + abs(v[i - 1] - v[i]);
for (int j = 2; j <= k && i - j >= 0; j++) {
dp[i] = min(dp[i], dp[i - k] + abs(v[i] - v[i - j]));
}
}
cout << dp[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define input_vector(v, n) \
for (long long i = 0; i < n; i++) { \
cin >> v[i]; \
}
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pb push_back
using namespace std;
typedef long long ll;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> v(n);
input_vector(v, n);
vector<ll> dp(n);
dp[0] = 0;
for (ll i = 1; i < n; i++) {
dp[i] = dp[i - 1] + abs(v[i - 1] - v[i]);
for (int j = 2; j <= k && i - j >= 0; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
}
}
cout << dp[n - 1] << endl;
return 0;
}
|
replace
| 22 | 23 | 22 | 23 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(void) {
long long N, K, i, j, h[100001], dp[100001];
cin >> N >> K;
for (i = 1; i <= N; i++) {
cin >> h[i];
}
for (i = 1; i <= N; i++) {
dp[i] = INF;
}
dp[1] = 0;
for (i = 1; i <= N; i++) {
for (j = 1; j <= K; j++) {
chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
}
}
cout << dp[N] << endl;
return 0;
}
|
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
#define div1 1000000007
const long long INF = 1LL << 60;
using namespace std;
// a>b -> a==b;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
// a<b -> a==b;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(void) {
long long N, K, i, j, h[100001], dp[100001];
cin >> N >> K;
for (i = 1; i <= N; i++) {
cin >> h[i];
}
for (i = 1; i <= N; i++) {
dp[i] = INF;
}
dp[1] = 0;
for (i = 1; i <= N; i++) {
for (j = 1; j <= K; j++) {
if (i + j <= N) {
chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
}
}
}
cout << dp[N] << endl;
return 0;
}
|
replace
| 47 | 48 | 47 | 50 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
const int inf = 0x3f3f3f3f;
using namespace std;
int main() {
int n, temp;
cin >> n >> temp;
vector<int> arr(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> arr[i];
vector<int> dp(n + 1, inf);
dp[2] = abs(arr[2] - arr[1]);
for (int i = 3; i <= n; i++) {
for (int k = 1; k <= temp; k++) {
dp[i] = min(dp[i - k] + abs(arr[i] - arr[i - k]), dp[i]);
}
}
cout << dp[n];
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
const int inf = 0x3f3f3f3f;
using namespace std;
int main() {
int n, temp;
cin >> n >> temp;
vector<int> arr(n + 1, 0);
for (int i = 1; i <= n; i++)
cin >> arr[i];
vector<int> dp(n + 1, inf);
dp[1] = 0;
for (int i = 2; i <= n; i++) {
for (int k = i - 1; k >= max(1, i - temp); k--) {
dp[i] = min(dp[k] + abs(arr[i] - arr[k]), dp[i]);
}
}
cout << dp[n];
return 0;
}
|
replace
| 15 | 19 | 15 | 19 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
int N, K;
std::cin >> N >> K;
std::vector<int> heights;
for (int height; std::cin >> height;) {
heights.push_back(height);
}
std::vector<int> costs_min(1, 0);
for (int i = 1; i < K; ++i) {
costs_min.push_back(std::abs(heights.at(i) - heights.at(0)));
} // K番目以下は1番目とK番目を結ぶ飛び方が少なくとも最適
for (int i = K; i < N; ++i) {
std::vector<int> temps;
for (int j = 1; j < K + 1; ++j) {
// jは段差の数 i-j -> iの飛び方を考える.
temps.push_back(costs_min.at(i - j) +
std::abs(heights.at(i) - heights.at(i - j)));
}
std::sort(temps.begin(), temps.end());
costs_min.push_back(temps.at(0));
}
std::cout << costs_min.at(N - 1) << std::endl;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
int N, K;
std::cin >> N >> K;
std::vector<int> heights;
for (int height; std::cin >> height;) {
heights.push_back(height);
}
std::vector<int> costs_min(1, 0);
for (int i = 1; i < std::min(K, N); ++i) {
costs_min.push_back(std::abs(heights.at(i) - heights.at(0)));
} // K番目以下は1番目とK番目を結ぶ飛び方が少なくとも最適
for (int i = K; i < N; ++i) {
std::vector<int> temps;
for (int j = 1; j < K + 1; ++j) {
// jは段差の数 i-j -> iの飛び方を考える.
temps.push_back(costs_min.at(i - j) +
std::abs(heights.at(i) - heights.at(i - j)));
}
std::sort(temps.begin(), temps.end());
costs_min.push_back(temps.at(0));
}
std::cout << costs_min.at(N - 1) << std::endl;
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
// #define int long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 61)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod % 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double
int main() {
FAST;
ll n, i, k, j;
cin >> n >> k;
ll h[n];
fr(i, 0, n) cin >> h[i];
ll dp[n];
fr(i, 0, n) dp[i] = 1000000000000000;
dp[0] = 0;
fr(i, 1, k) dp[i] = abs(h[i] - h[0]);
fr(i, k, n) {
fr(j, 1, k + 1) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
cout << dp[n - 1];
}
|
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
#define FAST \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fr(i, s, e) for (i = s; i < e; i++)
#define rf(i, s, e) for (i = s - 1; i >= e; i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
// #define int long long
#define ll long long
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
#define vl vector<long long>
#define inf (1LL << 61)
#define vi vector<int>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define vpi vector<pair<int, int>>
#define vpl vector<pair<ll, ll>>
#define viii vector<vector<vector<int>>>
typedef pair<pair<int, int>, int> P;
#define cd complex<double>
#define PI 4 * atan(1)
#define mod % 1000000007
#define pi 3.14159265358979323846 // 20 decimals
#define ld long double
int main() {
FAST;
ll n, i, k, j;
cin >> n >> k;
ll h[n];
fr(i, 0, n) cin >> h[i];
ll dp[n];
fr(i, 0, n) dp[i] = 1000000000000000;
dp[0] = 0;
fr(i, 1, min(n, k + 1)) dp[i] = abs(h[i] - h[0]);
fr(i, k + 1, n) {
if (i >= n)
break;
fr(j, 1, k + 1) dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
cout << dp[n - 1];
}
|
replace
| 58 | 60 | 58 | 62 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
/// BISMILLAHIR RAHMANIR RAHEEM
/// ALLAH IS WATCHING ME
/// ALLAH save us from COVID-19.Amin.
/// █▀█─█──█──█▀█─█─█
/// █▄█─█──█──█▄█─█▄█
/// █─█─█▄─█▄─█─█─█─█
/// كُلُّ نَفْسٍ ذَآئِقَةُ الْمَوْت
/// Every soul shall taste death.
/// TODO::
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/// order_of_key return number of elements less than x.
/// find_by_order return index.
using namespace std;
using namespace __gnu_pbds;
#define MOHAMMAD \
ios::sync_with_stdio(0); \
cin.tie(0);
#define all(x) (x).begin(), (x).end()
#define AE cout << fixed << setprecision(10);
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; // 8-direction.......
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {1, -1, 0, 0}; // 4-direction...........
int dy4[] = {0, 0, 1, -1};
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
// debug
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ",";
os << *it;
}
return os << "]";
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
#define dbg(args...) \
do { \
cerr << #args << " : "; \
faltu(args); \
} while (0)
void faltu() { cerr << endl; }
template <typename T> void faltu(T a[], int n) {
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
cerr << arg << ' ';
faltu(rest...);
}
// #else
// #define dbg(args...)
//*********************************************** The END
//**********************************************************************************************************************************
using ll = long long;
const ll N = 1e5 + 200;
const ll INF = 1e18;
ll dp[N + 1], n, a[N], k;
ll recur(ll idx) {
dbg(idx);
// if( cover==n-1 ) return 0;
if (idx == n)
return 0;
if (idx > n)
return INF;
ll &ret = dp[idx];
if (~ret)
return ret;
ll mn = INF;
for (ll i = 1; i <= k; ++i)
mn = min(mn, abs(a[idx] - a[idx + i]) + recur(idx + i));
return ret = mn;
}
int main() {
MOHAMMAD
memset(dp, -1, sizeof dp);
cin >> n >> k;
for (ll i = 0; i < n; ++i)
cin >> a[i];
for (ll i = n; i <= n + k; i++)
a[i] = a[n - 1];
cout << recur(0);
}
/*
Alhamdulillah
*/
|
/// BISMILLAHIR RAHMANIR RAHEEM
/// ALLAH IS WATCHING ME
/// ALLAH save us from COVID-19.Amin.
/// █▀█─█──█──█▀█─█─█
/// █▄█─█──█──█▄█─█▄█
/// █─█─█▄─█▄─█─█─█─█
/// كُلُّ نَفْسٍ ذَآئِقَةُ الْمَوْت
/// Every soul shall taste death.
/// TODO::
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/// order_of_key return number of elements less than x.
/// find_by_order return index.
using namespace std;
using namespace __gnu_pbds;
#define MOHAMMAD \
ios::sync_with_stdio(0); \
cin.tie(0);
#define all(x) (x).begin(), (x).end()
#define AE cout << fixed << setprecision(10);
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; // 8-direction.......
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {1, -1, 0, 0}; // 4-direction...........
int dy4[] = {0, 0, 1, -1};
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
// debug
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ",";
os << *it;
}
return os << "]";
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << *it;
}
return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if (it != v.begin())
os << ", ";
os << it->first << " = " << it->second;
}
return os << "]";
}
#define dbg(args...) \
do { \
cerr << #args << " : "; \
faltu(args); \
} while (0)
void faltu() { cerr << endl; }
template <typename T> void faltu(T a[], int n) {
for (int i = 0; i < n; ++i)
cerr << a[i] << ' ';
cerr << endl;
}
template <typename T, typename... hello>
void faltu(T arg, const hello &...rest) {
cerr << arg << ' ';
faltu(rest...);
}
// #else
// #define dbg(args...)
//*********************************************** The END
//**********************************************************************************************************************************
using ll = long long;
const ll N = 1e5 + 200;
const ll INF = 1e18;
ll dp[N + 1], n, a[N], k;
ll recur(ll idx) {
// dbg ( idx );
// if( cover==n-1 ) return 0;
if (idx == n)
return 0;
if (idx > n)
return INF;
ll &ret = dp[idx];
if (~ret)
return ret;
ll mn = INF;
for (ll i = 1; i <= k; ++i)
mn = min(mn, abs(a[idx] - a[idx + i]) + recur(idx + i));
return ret = mn;
}
int main() {
MOHAMMAD
memset(dp, -1, sizeof dp);
cin >> n >> k;
for (ll i = 0; i < n; ++i)
cin >> a[i];
for (ll i = n; i <= n + k; i++)
a[i] = a[n - 1];
cout << recur(0);
}
/*
Alhamdulillah
*/
|
replace
| 111 | 112 | 111 | 112 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e9;
bool graph[nmax][nmax];
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n) {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
for (size_t k = 0; k < n; k++) {
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
}
class UnionFind {
public:
vector<ll> Parent;
UnionFind(ll N) { Parent = vector<ll>(N, -1); }
ll find(ll A) {
if (Parent[A] < 0)
return A;
return Parent[A] = find(Parent[A]);
}
ll size(ll A) { return -Parent[find(A)]; }
bool Union(ll A, ll B) {
A = find(A);
B = find(B);
if (A == B) {
return false;
}
if (size(A) < size(B))
swap(A, B);
Parent[A] += Parent[B];
Parent[B] = A;
return true;
}
};
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }
ll powMod(ll a, ll p) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
ll half = powMod(a, p / 2);
return mulMod(half, half);
} else {
return mulMod(powMod(a, p - 1), a);
}
}
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
ll dp[100010];
void solve(long long N, long long K, std::vector<long long> h) {
for (int i = 0; i <= N; i++) {
dp[i] = INF;
}
dp[1] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= K; j++) {
dp[i + j] = min(dp[i] + abs(h[i - 1] - h[i + j - 1]), dp[i + j]);
}
}
cout << dp[N] << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> h(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &h[i]);
}
solve(N, K, std::move(h));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Edge = pair<int, int>;
using Graph = vector<vector<Edge>>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = 1e9;
bool graph[nmax][nmax];
vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
void warshall_floyd(ll n) {
for (size_t i = 0; i < n; i++) {
for (size_t j = 0; j < n; j++) {
for (size_t k = 0; k < n; k++) {
dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
}
}
}
}
class UnionFind {
public:
vector<ll> Parent;
UnionFind(ll N) { Parent = vector<ll>(N, -1); }
ll find(ll A) {
if (Parent[A] < 0)
return A;
return Parent[A] = find(Parent[A]);
}
ll size(ll A) { return -Parent[find(A)]; }
bool Union(ll A, ll B) {
A = find(A);
B = find(B);
if (A == B) {
return false;
}
if (size(A) < size(B))
swap(A, B);
Parent[A] += Parent[B];
Parent[B] = A;
return true;
}
};
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
ll mulMod(ll a, ll b) { return (((a % MOD) * (b % MOD)) % MOD); }
ll powMod(ll a, ll p) {
if (p == 0) {
return 1;
} else if (p % 2 == 0) {
ll half = powMod(a, p / 2);
return mulMod(half, half);
} else {
return mulMod(powMod(a, p - 1), a);
}
}
ll ceil(ll a, ll b) { return (a + b - 1) / b; }
ll dp[100200];
void solve(long long N, long long K, std::vector<long long> h) {
for (int i = 0; i <= N; i++) {
dp[i] = INF;
}
dp[1] = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= K; j++) {
dp[i + j] = min(dp[i] + abs(h[i - 1] - h[i + j - 1]), dp[i + j]);
}
}
cout << dp[N] << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long K;
scanf("%lld", &K);
std::vector<long long> h(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &h[i]);
}
solve(N, K, std::move(h));
return 0;
}
|
replace
| 78 | 79 | 78 | 79 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <stdio.h>
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
using namespace std;
typedef long long ll;
// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007 // 10^9+7
#define PB push_back
#define MP make_pair
int N;
int h[100000];
int K;
int dp[1000000];
int rec(int p) {
int ans = 1000000;
int sub[K];
if (dp[p] >= 0) {
ans = dp[p];
}
if (p == N - 1) {
ans = 0;
} else {
REP(i, K) {
if (p + i + 1 > N - 1) {
sub[i] = 1000000;
} else {
sub[i] = rec(p + i + 1) + abs(h[p] - h[p + i + 1]);
}
}
ans = sub[0];
REP(i, K) {
if (sub[i] < ans) {
ans = sub[i];
}
}
}
dp[p] = ans;
return ans;
}
int solve() {
scanf("%d %d", &N, &K);
REP(i, N) {
scanf("%d", &h[i]);
dp[i] = -1;
}
printf("%d", rec(0));
return 0;
}
int main() { solve(); }
|
#include <algorithm> //sort,二分探索,など
#include <bitset> //固定長bit集合
#include <cmath> //pow,logなど
#include <complex> //複素数
#include <deque> //両端アクセスのキュー
#include <functional> //sortのgreater
#include <iomanip> //setprecision(浮動小数点の出力の誤差)
#include <iostream> //入出力
#include <map> //map(辞書)
#include <numeric> //iota(整数列の生成),gcdとlcm(c++17)
#include <queue> //キュー
#include <set> //集合
#include <stack> //スタック
#include <stdio.h>
#include <string> //文字列
#include <unordered_map> //イテレータあるけど順序保持しないmap
#include <unordered_set> //イテレータあるけど順序保持しないset
#include <utility> //pair
#include <vector> //可変長配列
using namespace std;
typedef long long ll;
// マクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 // 10^12
#define MOD 10000007 // 10^9+7
#define PB push_back
#define MP make_pair
int N;
int h[100000];
int K;
int dp[1000000];
int rec(int p) {
int ans = 1000000;
int sub[K];
if (dp[p] >= 0) {
ans = dp[p];
} else if (p == N - 1) {
ans = 0;
} else {
REP(i, K) {
if (p + i + 1 > N - 1) {
sub[i] = 1000000;
} else {
sub[i] = rec(p + i + 1) + abs(h[p] - h[p + i + 1]);
}
}
ans = sub[0];
REP(i, K) {
if (sub[i] < ans) {
ans = sub[i];
}
}
}
dp[p] = ans;
return ans;
}
int solve() {
scanf("%d %d", &N, &K);
REP(i, N) {
scanf("%d", &h[i]);
dp[i] = -1;
}
printf("%d", rec(0));
return 0;
}
int main() { solve(); }
|
replace
| 46 | 48 | 46 | 47 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define vpll vector<pll>
#define vll vector<lli>
#define eps 0.000001
#define endl '\n'
using namespace std;
#define MAXN 10000000
// lli spf[MAXN];
// void sieve();
// set<lli> factors(lli x);
// lli power(lli x,lli y,lli p);
// void getZarr(string str, lli Z[]);
int main() {
/////////////////////////////////////
fastio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
/////////////////////////////////////
lli n, k;
cin >> n >> k;
lli a[n];
loopl(i, 0, n) cin >> a[i];
lli dp[n];
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
loopl(i, 2, n) {
lli mini = inf;
loopl(j, max((lli)0, i - k), i) mini = min(mini, dp[j] + abs(a[i] - a[j]));
dp[i] = mini;
}
cout << dp[n - 1] << endl;
}
/* void sieve()
{
spf[1] = 1;
loopl(i,2,MAXN)
spf[i] = i;
for (lli i=4; i<MAXN; i+=2)
spf[i] = 2;
for (lli i=3; i*i<MAXN; i++) {
if (spf[i] == i) {
for (lli j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}*/
/*
multiset<lli> factors(lli x)
{
multiset<lli> pr;
while (x != 1)
{
pr.insert(spf[x]);
x = x / spf[x];
}
return pr;
}
*/
/*
void dfs(lli q,lli root)
{
vis[q]=1;
ans[q]=1;
for(auto x: v[q])
{
if(!vis[x])
{
if(a[x]==a[root] && x<root) continue;
if(a[x]<a[root] || a[x]>a[root]+d) continue;
dfs(x,root);
ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
}
}
}
*/
/*void getZarr(string str, lli Z[])
{
lli n = str.length();
lli L, R, k;
L = R = 0;
for (lli i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
} else {
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else {
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
*/
/* lli power(lli x, lli y, lli p)
{
lli res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
*/
|
#include <bits/stdc++.h>
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define lli long long int
#define ulli unsigned long long int
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define loopl(i, a, b) for (lli i = a; i < b; i++)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define mod 1000000007
#define inf 1000000000000000
#define lld long double
#define pll pair<long long int, long long int>
#define vpll vector<pll>
#define vll vector<lli>
#define eps 0.000001
#define endl '\n'
using namespace std;
#define MAXN 10000000
// lli spf[MAXN];
// void sieve();
// set<lli> factors(lli x);
// lli power(lli x,lli y,lli p);
// void getZarr(string str, lli Z[]);
int main() {
lli n, k;
cin >> n >> k;
lli a[n];
loopl(i, 0, n) cin >> a[i];
lli dp[n];
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
loopl(i, 2, n) {
lli mini = inf;
loopl(j, max((lli)0, i - k), i) mini = min(mini, dp[j] + abs(a[i] - a[j]));
dp[i] = mini;
}
cout << dp[n - 1] << endl;
}
/* void sieve()
{
spf[1] = 1;
loopl(i,2,MAXN)
spf[i] = i;
for (lli i=4; i<MAXN; i+=2)
spf[i] = 2;
for (lli i=3; i*i<MAXN; i++) {
if (spf[i] == i) {
for (lli j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}*/
/*
multiset<lli> factors(lli x)
{
multiset<lli> pr;
while (x != 1)
{
pr.insert(spf[x]);
x = x / spf[x];
}
return pr;
}
*/
/*
void dfs(lli q,lli root)
{
vis[q]=1;
ans[q]=1;
for(auto x: v[q])
{
if(!vis[x])
{
if(a[x]==a[root] && x<root) continue;
if(a[x]<a[root] || a[x]>a[root]+d) continue;
dfs(x,root);
ans[q]=(ans[q]*((ans[x]+1)%mod))%mod ;
}
}
}
*/
/*void getZarr(string str, lli Z[])
{
lli n = str.length();
lli L, R, k;
L = R = 0;
for (lli i = 1; i < n; ++i){
if (i > R){
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
} else {
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else {
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
*/
/* lli power(lli x, lli y, lli p)
{
lli res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
*/
|
delete
| 36 | 46 | 36 | 36 |
-11
| |
p03161
|
C++
|
Runtime Error
|
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
int t;
ll cost;
edge(int _t, ll _cost) : t(_t), cost(_cost) {}
};
void Solve() {
int n, k;
cin >> n >> k;
ll h[100010];
for (size_t i = 0; i < n; i++) {
cin >> h[i];
}
ll dp[100010];
for (size_t i = 0; i < n + 1000; i++) {
dp[i] = INF;
}
dp[0] = 0;
for (size_t i = 0; i < n; i++) {
for (size_t j = 1; j <= k; j++) {
if (i + j >= n)
break;
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
}
cout << dp[n - 1] << endl;
}
int main() {
Solve();
return 0;
}
|
#pragma once
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;
const int MAX = 510000;
const int MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
int t;
ll cost;
edge(int _t, ll _cost) : t(_t), cost(_cost) {}
};
void Solve() {
int n, k;
cin >> n >> k;
ll h[100010];
for (size_t i = 0; i < n; i++) {
cin >> h[i];
}
ll dp[100010];
for (size_t i = 0; i <= n; i++) {
dp[i] = INF;
}
dp[0] = 0;
for (size_t i = 0; i < n; i++) {
for (size_t j = 1; j <= k; j++) {
if (i + j >= n)
break;
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
}
cout << dp[n - 1] << endl;
}
int main() {
Solve();
return 0;
}
|
replace
| 65 | 66 | 65 | 66 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
// بسم الله الرحمن الرحيم
// اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
// Allah in the name of The Most Affectionate, the Merciful
// अल्लाह के नाम से जो दयालु और कृपाशील है
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back
#define endl '\n'
#define mod 1000000007
#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define MEMSET(t) memset(t, 0, sizeof(t));
#define time clock_t tStart
#define Start clock()
#define Time \
printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)
ll minC(int ar[], int n, int k, ll dp[], int i) {
if (i >= n)
return 0;
if (i == n - 1)
return 0;
if (dp[i] != -1)
return dp[i];
int t = n;
ll mini = INT_MAX;
for (int j = i + 1; j <= i + k && j < n; j++) {
mini = min(abs(ar[i] - ar[j]) + minC(ar, n, k, dp, j), mini);
// cout<<mini<<" "<<i<<" "<< j<<endl;
}
dp[n] = mini;
return dp[n];
// return 0;
}
int main() {
fastio;
int t = 1;
while (t--) {
int n, k;
cin >> n >> k;
int ar[n];
ll dp[n + 1];
FOR(i, 0, n) cin >> ar[i];
memset(dp, -1, sizeof(dp));
cout << minC(ar, n, k, dp, 0);
}
return 0;
}
|
// بسم الله الرحمن الرحيم
// اللہ کے نام سے شروع جو بڑا مہربان اور نہایت رحم والا ہے
// Allah in the name of The Most Affectionate, the Merciful
// अल्लाह के नाम से जो दयालु और कृपाशील है
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
#define fastio \
ios::sync_with_stdio(0); \
cin.tie(0)
#define ll long long
#define ull unsigned ll
#define pb push_back
#define endl '\n'
#define mod 1000000007
#define FOR(i, k, n) for (int i = k; i < n; i++)
#define ROF(i, k, n) for (int i = k; i >= n; i--)
#define MEMSET(t) memset(t, 0, sizeof(t));
#define time clock_t tStart
#define Start clock()
#define Time \
printf("Time taken: %.3fs\n", (double)(clock() - tStart) / CLOCKS_PER_SEC)
ll minC(int ar[], int n, int k, ll dp[], int i) {
if (i >= n)
return 0;
if (i == n - 1)
return 0;
if (dp[i] != -1)
return dp[i];
int t = n;
ll mini = INT_MAX;
for (int j = i + 1; j <= i + k && j < n; j++) {
mini = min(abs(ar[i] - ar[j]) + minC(ar, n, k, dp, j), mini);
// cout<<mini<<" "<<i<<" "<< j<<endl;
}
dp[i] = mini;
return dp[i];
// return 0;
}
int main() {
fastio;
int t = 1;
while (t--) {
int n, k;
cin >> n >> k;
int ar[n];
ll dp[n + 1];
FOR(i, 0, n) cin >> ar[i];
memset(dp, -1, sizeof(dp));
cout << minC(ar, n, k, dp, 0);
}
return 0;
}
|
replace
| 48 | 50 | 48 | 50 |
TLE
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <cmath>
#include <iostream>
#include <limits>
#include <vector>
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> h(n);
for (auto &&hi : h)
std::cin >> hi;
std::vector<int> cost(n, std::numeric_limits<int>::max());
cost[0] = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < k; ++k) {
const auto ii = i - j - 1;
if (ii < 0)
break;
const auto c = std::abs(h[i] - h[ii]) + cost[ii];
if (cost[i] > c)
cost[i] = c;
}
}
std::cout << cost[n - 1] << std::endl;
}
|
#include <cmath>
#include <iostream>
#include <limits>
#include <vector>
int main() {
int n, k;
std::cin >> n >> k;
std::vector<int> h(n);
for (auto &&hi : h)
std::cin >> hi;
std::vector<int> cost(n, std::numeric_limits<int>::max());
cost[0] = 0;
for (int i = 1; i < n; ++i) {
for (int j = 0; j < k; ++j) {
const auto ii = i - j - 1;
if (ii < 0)
break;
const auto c = std::abs(h[i] - h[ii]) + cost[ii];
if (cost[i] > c)
cost[i] = c;
}
}
std::cout << cost[n - 1] << std::endl;
}
|
replace
| 16 | 17 | 16 | 17 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define CLR(a) memset((a), 0, sizeof(a))
#define mp make_pair
#define hell 1000000007
using namespace std;
// int g[105][105];
// vector < int > wt(100004);
// vector < int > a;
// vector < int > v(30500);
// int cst[102][100005];
// int ansr[100005];
// ll int sum,x=0,c=0,n,m,flag=0,a1,a2,flag1=0,flag2=0;
// vector < vector < int > > adj(5000, vector < int > (5000));
// int dp[1000006];
// int level[500];
void solve() {
int i, j = 0, r = 200, p, q, k = 0, ans = 0, d, h, flag = 0, n, m = 0, a, b,
id, c, h1, h2, a1, b1;
cin >> n >> k;
vector<int> v(n), dp(n, hell);
rep(i, 0, n) { cin >> v[i]; }
dp[0] = 0;
dp[1] = abs(v[1] - v[0]);
rep(i, 2, n) {
rep(j, 1, k + 1) { dp[i] = min(dp[i], dp[i - j] + abs(v[i - j] - v[i])); }
}
cout << dp[n - 1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
}
/*
void bfs( int i , int j)
{
vis[i][j]= true;
queue < int > que;
que.push(k);
while(!que.empty())
{
int x= que.front();
que.pop();
for ( int i=1;i<=n;i++)
{
if((!g[1][n] and g[x][i] ) or (g[1][n] and ! g[x][i]))
{
if(!vis[i])
{
que.push(i);
level[i]=level[x]+1;
vis[i]= true;
}
}
}
}
}
ll int por(ll int a,ll int b)
{
ll int k=1;
while(b>0)
{
if(b%2) k=k*a,k%=hell;
b/=2;
a*=a; a%=hell;
}
return k;
} */
|
#include <bits/stdc++.h>
#define ll long long
#define rep(i, a, b) for (i = a; i < b; i++)
#define pb push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define CLR(a) memset((a), 0, sizeof(a))
#define mp make_pair
#define hell 1000000007
using namespace std;
// int g[105][105];
// vector < int > wt(100004);
// vector < int > a;
// vector < int > v(30500);
// int cst[102][100005];
// int ansr[100005];
// ll int sum,x=0,c=0,n,m,flag=0,a1,a2,flag1=0,flag2=0;
// vector < vector < int > > adj(5000, vector < int > (5000));
// int dp[1000006];
// int level[500];
void solve() {
int i, j = 0, r = 200, p, q, k = 0, ans = 0, d, h, flag = 0, n, m = 0, a, b,
id, c, h1, h2, a1, b1;
cin >> n >> k;
vector<int> v(n), dp(n, hell);
rep(i, 0, n) { cin >> v[i]; }
dp[0] = 0;
dp[1] = abs(v[1] - v[0]);
rep(i, 2, n) {
rep(j, 1, k + 1) {
if (i - j >= 0)
dp[i] = min(dp[i], dp[i - j] + abs(v[i - j] - v[i]));
}
}
cout << dp[n - 1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--)
solve();
}
/*
void bfs( int i , int j)
{
vis[i][j]= true;
queue < int > que;
que.push(k);
while(!que.empty())
{
int x= que.front();
que.pop();
for ( int i=1;i<=n;i++)
{
if((!g[1][n] and g[x][i] ) or (g[1][n] and ! g[x][i]))
{
if(!vis[i])
{
que.push(i);
level[i]=level[x]+1;
vis[i]= true;
}
}
}
}
}
ll int por(ll int a,ll int b)
{
ll int k=1;
while(b>0)
{
if(b%2) k=k*a,k%=hell;
b/=2;
a*=a; a%=hell;
}
return k;
} */
|
replace
| 32 | 33 | 32 | 36 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll n, i, k, j;
cin >> n >> k;
ll a[n], dp[n];
for (i = 0; i < n; i++)
cin >> a[i];
dp[0] = 0;
// dp[1]=abs(a[1]-a[0]);
for (i = 1; i < k; i++)
// dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
{
ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
for (j = 2; j <= i; j++) {
ll x = dp[i - j] + abs(a[i] - a[i - j]);
if (x < m)
m = x;
}
dp[i] = m;
}
for (i = k; i < n; i++) {
ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
for (j = 2; j <= k; j++) {
ll x = dp[i - j] + abs(a[i] - a[i - j]);
if (x < m)
m = x;
}
dp[i] = m;
}
// dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
cout << dp[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll n, i, k, j;
cin >> n >> k;
ll a[n], dp[n];
for (i = 0; i < n; i++)
cin >> a[i];
dp[0] = 0;
// dp[1]=abs(a[1]-a[0]);
for (i = 1; i < k; i++) {
if (i >= n)
break;
ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
for (j = 2; j <= i; j++) {
ll x = dp[i - j] + abs(a[i] - a[i - j]);
if (x < m)
m = x;
}
dp[i] = m;
}
for (i = k; i < n; i++) {
ll m = dp[i - 1] + abs(a[i] - a[i - 1]);
for (j = 2; j <= k; j++) {
ll x = dp[i - j] + abs(a[i] - a[i - j]);
if (x < m)
m = x;
}
dp[i] = m;
}
// dp[i]=min(dp[i-1]+abs(a[i]-a[i-1]),dp[i-2]+abs(a[i]-a[i-2]));
cout << dp[n - 1] << endl;
return 0;
}
|
replace
| 11 | 14 | 11 | 14 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[100009], dp[100009], k;
ll f(int i) {
if (i > n)
return 1e9;
else if (i == n)
return 0;
if (dp[i] != -1)
return dp[i];
ll ans = f(i + 1) + abs(a[i] - a[i + 1]);
for (int j = 2; j <= k; j++) {
ans = min(ans, f(i + j) + abs(a[i] - a[i + j]));
}
return dp[i] = ans;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
cout << f(1) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, a[200009], dp[200009], k;
ll f(int i) {
if (i > n)
return 1e9;
else if (i == n)
return 0;
if (dp[i] != -1)
return dp[i];
ll ans = f(i + 1) + abs(a[i] - a[i + 1]);
for (int j = 2; j <= k; j++) {
ans = min(ans, f(i + j) + abs(a[i] - a[i + j]));
}
return dp[i] = ans;
}
int main() {
memset(dp, -1, sizeof dp);
cin >> n >> k;
for (int i = 1; i <= n; i++)
cin >> a[i];
cout << f(1) << endl;
}
|
replace
| 3 | 4 | 3 | 4 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> h(N, 0);
for (int i = 0; i < N; i++)
cin >> h[i];
vector<int> cost(N, 0);
cost[1] = abs(h[1] - h[0]);
for (int i = 2; i < N; i++) {
int res = INT_MAX;
for (int k = 1; k <= K; k++) {
res = min(res, cost[i - k] + abs(h[i] - h[i - k]));
}
cost[i] = res;
}
cout << cost[N - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> h(N, 0);
for (int i = 0; i < N; i++)
cin >> h[i];
vector<int> cost(N, 0);
cost[1] = abs(h[1] - h[0]);
for (int i = 2; i < N; i++) {
int res = INT_MAX;
for (int k = 1; k <= K and i - k >= 0; k++) {
res = min(res, cost[i - k] + abs(h[i] - h[i - k]));
}
cost[i] = res;
}
cout << cost[N - 1];
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, n) for (int i = a; i < n; i++)
#define pb(v) push_back(v)
int main() {
// code
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
int a[n];
f(i, 0, n) cin >> a[i];
int dp[n + 3];
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (int i = 2; i < k; i++) {
dp[i] = abs(a[i] - a[0]);
}
for (int i = k; i < n; i++) {
vector<int> v;
for (int j = i - k; j < i;
j++) { // cout<<dp[j]<<"d"<<dp[j]+abs(a[i]-a[j])<<"\n"<<endl;
v.pb(dp[j] + abs(a[i] - a[j]));
}
dp[i] = *min_element(v.begin(), v.end());
// cout<<dp[i]<<"sa";
}
cout << dp[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, n) for (int i = a; i < n; i++)
#define pb(v) push_back(v)
int main() {
// code
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
int a[n];
f(i, 0, n) cin >> a[i];
int dp[n + k + 1];
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (int i = 2; i < k; i++) {
dp[i] = abs(a[i] - a[0]);
}
for (int i = k; i < n; i++) {
vector<int> v;
for (int j = i - k; j < i;
j++) { // cout<<dp[j]<<"d"<<dp[j]+abs(a[i]-a[j])<<"\n"<<endl;
v.pb(dp[j] + abs(a[i] - a[j]));
}
dp[i] = *min_element(v.begin(), v.end());
// cout<<dp[i]<<"sa";
}
cout << dp[n - 1] << endl;
return 0;
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
vector<long long> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<long long> dp(n, INT_MAX);
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < n; i++) {
for (int j = i - 1; j <= k; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
}
cout << dp[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
vector<long long> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<long long> dp(n, INT_MAX);
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < n; i++) {
for (int j = 1; j <= k; j++) {
if (i < j)
break;
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
}
cout << dp[n - 1];
return 0;
}
|
replace
| 16 | 17 | 16 | 19 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &ht, int n, int k) {
vector<int> dp(n, 0);
dp[0] = 0;
dp[1] = abs(ht[1] - ht[0]);
int mn = INT_MAX;
for (int i = 2; i < n; i++) {
for (int j = i - 1; j >= i - k && j >= 0; j--) {
mn = min(mn, (dp[j] + abs(ht[i] - ht[j])));
}
dp[i] = mn;
mn = INT_MAX;
}
return dp[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
vector<int> ht;
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
ht.push_back(a);
}
cout << solve(ht, n, k) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &ht, int n, int k) {
vector<int> dp(n, 0);
dp[0] = 0;
dp[1] = abs(ht[1] - ht[0]);
int mn = INT_MAX;
for (int i = 2; i < n; i++) {
for (int j = i - 1; j >= i - k && j >= 0; j--) {
mn = min(mn, (dp[j] + abs(ht[i] - ht[j])));
}
dp[i] = mn;
mn = INT_MAX;
}
return dp[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
}
vector<int> ht;
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
ht.push_back(a);
}
cout << solve(ht, n, k) << endl;
}
|
replace
| 22 | 25 | 22 | 25 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &ht, int n, int k) {
vector<int> dp(n, 0);
dp[0] = 0;
dp[1] = abs(ht[1] - ht[0]);
int mn = INT_MAX;
for (int i = 2; i < n; i++) {
for (int j = i - 1; j >= i - k && j >= 0; j--) {
mn = min(mn, (dp[j] + abs(ht[i] - ht[j])));
}
dp[i] = mn;
mn = INT_MAX;
}
return dp[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
vector<int> ht;
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
ht.push_back(a);
}
cout << solve(ht, n, k) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> &ht, int n, int k) {
vector<int> dp(n, 0);
dp[0] = 0;
dp[1] = abs(ht[1] - ht[0]);
int mn = INT_MAX;
for (int i = 2; i < n; i++) {
for (int j = i - 1; j >= i - k && j >= 0; j--) {
mn = min(mn, (dp[j] + abs(ht[i] - ht[j])));
}
dp[i] = mn;
mn = INT_MAX;
}
return dp[n - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<int> ht;
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
ht.push_back(a);
}
cout << solve(ht, n, k) << endl;
}
|
delete
| 21 | 24 | 21 | 21 |
-6
|
terminate called after throwing an instance of 'std::length_error'
what(): cannot create std::vector larger than max_size()
|
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cost[100002], n, k;
ll dp[100002];
ll jump(ll pos) {
if (dp[pos] != -1)
return dp[pos];
if (pos == n)
return 0;
if (pos > n)
return INT_MAX;
ll ans = 0, prev = INT_MAX;
for (ll i = 1; i <= k; i++) {
ans = (abs(cost[pos] - cost[pos + i]) + jump(pos + i));
prev = min(prev, ans);
}
return prev;
}
int main() {
cin >> n >> k;
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; i++)
cin >> cost[i];
ll ans = jump(1);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll cost[100002], n, k;
ll dp[100002];
ll jump(ll pos) {
if (dp[pos] != -1)
return dp[pos];
if (pos == n)
return 0;
if (pos > n)
return INT_MAX;
ll ans = 0, prev = INT_MAX;
for (ll i = 1; i <= k; i++) {
ans = (abs(cost[pos] - cost[pos + i]) + jump(pos + i));
prev = min(prev, ans);
}
return dp[pos] = prev;
}
int main() {
cin >> n >> k;
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; i++)
cin >> cost[i];
ll ans = jump(1);
cout << ans << endl;
return 0;
}
|
replace
| 17 | 18 | 17 | 18 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <cstdio>
#include <iostream>
using namespace std;
int n, k, h[100001], f[100001];
int abs(int a) { return (a > 0) ? a : (-a); }
int way(int a) {
int mins = 2147483647, minp = 0;
for (int i = ((a - k > 0) ? (a - k) : 1); i < a; i++) {
if (f[i] + abs(h[a] - h[i]) < mins) {
mins = f[i] + abs(h[a] - h[i]);
minp = i;
}
}
return minp;
}
int main() {
freopen("FROG2.inp", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
f[1] = 0, f[2] = abs(h[2] - h[1]);
for (int i = 3; i <= n; i++) {
f[i] = f[way(i)] + abs(h[i] - h[way(i)]);
}
cout << f[n];
return 0;
}
|
#include <cstdio>
#include <iostream>
using namespace std;
int n, k, h[100001], f[100001];
int abs(int a) { return (a > 0) ? a : (-a); }
int way(int a) {
int mins = 2147483647, minp = 0;
for (int i = ((a - k > 0) ? (a - k) : 1); i < a; i++) {
if (f[i] + abs(h[a] - h[i]) < mins) {
mins = f[i] + abs(h[a] - h[i]);
minp = i;
}
}
return minp;
}
int main() {
// freopen("FROG2.inp", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> h[i];
}
f[1] = 0, f[2] = abs(h[2] - h[1]);
for (int i = 3; i <= n; i++) {
f[i] = f[way(i)] + abs(h[i] - h[way(i)]);
}
cout << f[n];
return 0;
}
|
replace
| 17 | 18 | 17 | 18 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60; // これはなに?
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
int N, K;
cin >> N >> K;
vector<int> h(N);
for (int i = 0; i < N; ++i)
cin >> h[i];
vector<long long> dp(N);
for (int i = 0; i < N; ++i)
dp[i] = 100000000010; // DPテーブル
dp[0] = 0;
for (int i = 0; i < N; ++i) {
for (int k = 1; k <= K; ++k) {
chmin(dp[i + k], dp[i] + abs(h[i] - h[i + k]));
}
}
cout << dp[N - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 60; // これはなに?
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
int N, K;
cin >> N >> K;
vector<int> h(N);
for (int i = 0; i < N; ++i)
cin >> h[i];
vector<long long> dp(100000);
for (int i = 0; i < 100000; ++i)
dp[i] = 100000000010; // DPテーブル
dp[0] = 0;
for (int i = 0; i < N; ++i) {
for (int k = 1; k <= K; ++k) {
chmin(dp[i + k], dp[i] + abs(h[i] - h[i + k]));
}
}
cout << dp[N - 1] << endl;
}
|
replace
| 19 | 21 | 19 | 21 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> dp(n, 1e9), arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
dp[0] = 0;
if (n > 1)
dp[1] = abs(arr[1] - arr[0]);
for (int i = 2; i < n; i++)
for (int j = 1; j <= k; j++)
dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
cout << dp[n - 1];
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> dp(n, 1e9), arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
dp[0] = 0;
for (int i = 0; i < n; i++)
for (int j = 1; j <= k && j <= i; j++)
dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
cout << dp[n - 1];
}
|
replace
| 9 | 13 | 9 | 11 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)
#define MOD 1e9 + 7
#define sp ' '
using ll = long long;
ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
ll r = 1;
while (e > 0) {
if (e & 1)
r = (r % m) * (b % m) % m;
b = (b % m) * (b % m) % m;
e >>= 1;
}
return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
template <typename T> void ppp(T n) { cout << n << endl; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int dp[100001];
fill_n(dp, 100001, 1e9);
int n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) { cin >> h[i]; }
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < n; ++i) {
for (int j = 1; j <= k; ++j) {
dp[i] = min(dp[i - j] + abs(h[i - j] - h[i]), dp[i]);
}
}
ppp(dp[n - 1]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// repetition
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define euc_dis(x, y) sqrt(x *x + y * y)
#define MOD 1e9 + 7
#define sp ' '
using ll = long long;
ll mod(ll a, ll b) { return (a % b + b) % b; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void Judge(bool b) { b ? Yes() : No(); }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void JUDGE(bool b) { b ? YES() : NO(); }
ll powMod(ll b, ll e, ll m) {
ll r = 1;
while (e > 0) {
if (e & 1)
r = (r % m) * (b % m) % m;
b = (b % m) * (b % m) % m;
e >>= 1;
}
return r;
}
double distance(ll x1, ll y1, ll x2, ll y2) {
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
template <typename T> void ppp(T n) { cout << n << endl; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int dp[100001];
fill_n(dp, 100001, 1e9);
int n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) { cin >> h[i]; }
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < n; ++i) {
for (int j = 1; j <= k; ++j) {
if (i - j < 0)
break;
dp[i] = min(dp[i - j] + abs(h[i - j] - h[i]), dp[i]);
}
}
ppp(dp[n - 1]);
return 0;
}
|
insert
| 56 | 56 | 56 | 58 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define fr first
#define sc second
#define memset(X, Y) memset(X, Y, sizeof X)
#define all(X) X.begin(), X.end()
#define sz size()
#define pb push_back
#define ve vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define Graph vector<vector<int>>
#define Fp cout << fixed << setprecision(9)
#define Fast \
ios::sync_with_stdio(0); \
cin.tie(0);
using namespace std;
const ll M = 1e3 + 30, N = 10400, MOD = 1e9 + 7, OO = 1e7;
ll m, n, k, x, y, dp[N], bla, c = 1, cnt, ans, sum, a[N], ok = 1;
int main() {
cin >> n >> k;
for (int i = k; i <= (n + k - 1); i++) {
cin >> a[i];
}
for (int i = 0; i <= (n + k - 1); i++) {
if (i == k)
dp[i] = 0;
else {
dp[i] = OO;
}
}
for (int i = k + 1; i <= (n + k - 1); i++) {
for (int j = 1; j <= k; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(a[i - j] - a[i]));
}
}
cout << dp[n + k - 1];
return 0;
}
/*
6 15= max
6 5
5 6
6 4
6 6
3 5
7 2
*/
|
#include <bits/stdc++.h>
#define fr first
#define sc second
#define memset(X, Y) memset(X, Y, sizeof X)
#define all(X) X.begin(), X.end()
#define sz size()
#define pb push_back
#define ve vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ll long long
#define Graph vector<vector<int>>
#define Fp cout << fixed << setprecision(9)
#define Fast \
ios::sync_with_stdio(0); \
cin.tie(0);
using namespace std;
const ll M = 1e3 + 30, N = 100400, MOD = 1e9 + 7, OO = 1e10;
ll m, n, k, x, y, dp[N], bla, c = 1, cnt, ans, sum, a[N], ok = 1;
int main() {
cin >> n >> k;
for (int i = k; i <= (n + k - 1); i++) {
cin >> a[i];
}
for (int i = 0; i <= (n + k - 1); i++) {
if (i == k)
dp[i] = 0;
else {
dp[i] = OO;
}
}
for (int i = k + 1; i <= (n + k - 1); i++) {
for (int j = 1; j <= k; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(a[i - j] - a[i]));
}
}
cout << dp[n + k - 1];
return 0;
}
/*
6 15= max
6 5
5 6
6 4
6 6
3 5
7 2
*/
|
replace
| 17 | 18 | 17 | 18 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, k;
cin >> n >> k;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> dp(n);
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
if (n == 2) {
cout << dp[1] << endl;
return 0;
}
for (int i = 2; i < n; i++) {
int tmp = dp[i - 1] + abs(h[i] - h[i - 1]);
for (int j = 2; j <= min(n, k); j++) {
tmp = min(tmp, dp[i - j] + abs(h[i] - h[i - j]));
}
dp[i] = tmp;
}
cout << dp.back() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, k;
cin >> n >> k;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> dp(n);
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
if (n == 2) {
cout << dp[1] << endl;
return 0;
}
for (int i = 2; i < n; i++) {
int tmp = dp[i - 1] + abs(h[i] - h[i - 1]);
for (int j = 2; j <= min(i, k); j++) {
tmp = min(tmp, dp[i - j] + abs(h[i] - h[i - j]));
}
dp[i] = tmp;
}
cout << dp.back() << endl;
return 0;
}
|
replace
| 20 | 21 | 20 | 21 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, m, k = 0, l = 0, x, y;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
vector<ll> a(n + 2 * k, 0), dp(n + 2 * k, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[i] = abs(a[i] - a[0]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++)
dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
dp[i + k] = dp[i] + abs(a[i] - a[i + k]);
}
cout << dp[n - 1];
}
|
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
ll n, m, k = 0, l = 0, x, y;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
vector<ll> a(n + 2 * k, 0), dp(n + 2 * k, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[i] = abs(a[i] - a[0]);
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < i + k; j++)
dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
dp[i + k] = dp[i] + abs(a[i] - a[i + k]);
}
cout << dp[n - 1];
}
|
replace
| 25 | 26 | 25 | 26 |
TLE
| |
p03161
|
C++
|
Time Limit Exceeded
|
// #define _GLIBCXX_DEBUG//TLEの原因になるので注意!!!!!!!!!!!
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long
// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある
ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
int h[100005];
int dp[100005];
int main() {
int n, k;
cin >> n >> k;
rep(i, n + 1) { dp[i] = INF; }
dp[1] = 0;
rep2(i, n) {
cin >> h[i];
for (int j = i - k; j < n; j++) {
if (0 < j && j < i) {
dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
}
}
}
cout << dp[n] << endl;
return 0;
}
|
// #define _GLIBCXX_DEBUG//TLEの原因になるので注意!!!!!!!!!!!
#include <bits/stdc++.h>
#include <cmath>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long
// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある
ll N, M, H, W, K, Q, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
int h[100005];
int dp[100005];
int main() {
int n, k;
cin >> n >> k;
rep(i, n + 1) { dp[i] = INF; }
dp[1] = 0;
rep2(i, n) {
cin >> h[i];
for (int j = i - k; j < n && j < i; j++) {
if (0 < j && j < i) {
dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
}
}
}
cout << dp[n] << endl;
return 0;
}
|
replace
| 50 | 51 | 50 | 51 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)
typedef long long ll;
#define int ll
#define INF 1e9 + 5
#undef int
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define int ll
int N;
cin >> N;
int K;
cin >> K; // K = 2;
int H[N];
list(i, N) cin >> H[i];
int dp[N];
list(i, N) dp[i] = INF;
dp[0] = 0;
list(i, N) {
for (int j = i + 1; j <= i + K; j++)
dp[j] = min(dp[i] + abs(H[i] - H[j]), dp[j]);
}
cout << dp[N - 1];
return 0;
}
|
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
#define rep(i, a, b) for (auto(i) = a; i < b; i++)
#define list(i, N) for (auto(i) = 0; i < N; i++)
typedef long long ll;
#define int ll
#define INF 1e9 + 5
#undef int
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define int ll
int N;
cin >> N;
int K;
cin >> K; // K = 2;
int H[N];
list(i, N) cin >> H[i];
int dp[N];
list(i, N) dp[i] = INF;
dp[0] = 0;
list(i, N) {
for (int j = i + 1; j <= i + K && j < N; j++)
dp[j] = min(dp[i] + abs(H[i] - H[j]), dp[j]);
}
cout << dp[N - 1];
return 0;
}
|
replace
| 30 | 31 | 30 | 31 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long
using namespace std;
int dpj[100001][101];
int dp[100001];
int jump(int a, int k, int h[], int n) {
if (a == n) {
return 0;
}
if (dp[a] != -1) {
return dp[a];
}
int min = INT32_MAX, i, x;
for (i = a + 1; i <= a + k && i <= n; i++) {
if (dpj[i][k] != -1) {
x = dpj[i][k];
} else {
x = jump(i, k, h, n);
dpj[i][k] = x;
}
x += abs(h[i] - h[a]);
if (min > x) {
min = x;
}
}
dp[a] = min;
return min;
}
void ini() {
for (int i = 0; i < 100001; i++) {
dp[i] = -1;
for (int j = 0; j < 101; j++) {
dpj[i][j] = -1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k, i;
cin >> n >> k;
int h[n + 1];
for (i = 1; i <= n; i++) {
cin >> h[i];
}
ini();
cout << jump(1, k, h, n) << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#include <unordered_map>
#define M 1000000007
#define T 998244353
#define PI 3.142
#define ll long long
using namespace std;
int dpj[100001][101];
int dp[100001];
int jump(int a, int k, int h[], int n) {
if (a == n) {
return 0;
}
if (dp[a] != -1) {
return dp[a];
}
int min = INT32_MAX, i, x;
for (i = a + 1; i <= a + k && i <= n; i++) {
if (dpj[i][k] != -1) {
x = dpj[i][k];
} else {
x = jump(i, k, h, n);
dpj[i][k] = x;
}
x += abs(h[i] - h[a]);
if (min > x) {
min = x;
}
}
dp[a] = min;
return min;
}
void ini() {
for (int i = 0; i < 100001; i++) {
dp[i] = -1;
for (int j = 0; j < 101; j++) {
dpj[i][j] = -1;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k, i;
cin >> n >> k;
int h[n + 1];
for (i = 1; i <= n; i++) {
cin >> h[i];
}
ini();
cout << jump(1, k, h, n) << "\n";
return 0;
}
|
delete
| 47 | 53 | 47 | 47 |
-11
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(int, char **) {
int n, k;
cin >> n >> k;
vector<int> h(n), a(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
a[0] = 0;
for (int i = 1; i < k; ++i) {
a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
for (int j = 2; j <= i; ++j) {
auto tmp = abs(h[i] - h[i - j]) + a[i - j];
if (a[i] > tmp) {
a[i] = tmp;
}
}
}
for (int i = k; i < n; ++i) {
a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
for (int j = 2; j <= k; ++j) {
auto tmp = abs(h[i] - h[i - j]) + a[i - j];
if (a[i] > tmp) {
a[i] = tmp;
}
}
}
cout << a[n - 1] << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int main(int, char **) {
int n, k;
cin >> n >> k;
vector<long> h(n), a(n);
for (int i = 0; i < n; ++i) {
cin >> h[i];
}
a[0] = 0;
for (int i = 1; i < k; ++i) {
a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
for (int j = 2; j <= i; ++j) {
auto tmp = abs(h[i] - h[i - j]) + a[i - j];
if (a[i] > tmp) {
a[i] = tmp;
}
}
}
for (int i = k; i < n; ++i) {
a[i] = abs(h[i] - h[i - 1]) + a[i - 1];
for (int j = 2; j <= k; ++j) {
auto tmp = abs(h[i] - h[i - j]) + a[i - j];
if (a[i] > tmp) {
a[i] = tmp;
}
}
}
cout << a[n - 1] << endl;
return 0;
}
|
replace
| 8 | 9 | 8 | 9 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, k;
cin >> n >> k;
long a[n], cost[n];
for (int i = 0; i < n; ++i)
cost[i] = INT_MAX;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 1; i <= k; ++i)
cost[i] = abs(a[i] - a[0]);
for (int i = 1; i < n; ++i)
for (int j = i + 1; j <= i + k and j < n; ++j)
cost[j] = min(cost[j], cost[i] + abs(a[j] - a[i]));
cout << cost[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, k;
cin >> n >> k;
long a[n], cost[n];
for (int i = 0; i < n; ++i)
cost[i] = INT_MAX;
for (int i = 0; i < n; ++i)
cin >> a[i];
cost[0] = 0;
for (int i = 0; i < n; ++i)
for (int j = i + 1; j <= i + k and j < n; ++j)
cost[j] = min(cost[j], cost[i] + abs(a[j] - a[i]));
cout << cost[n - 1];
return 0;
}
|
replace
| 12 | 15 | 12 | 14 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T) \
for_each(begin(A), end(A), [&](T i) { cout << i << " "; }); \
cout << "\n"
typedef long lint;
typedef pair<lint, lint> P;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
lint N;
cin >> N;
lint K;
cin >> K;
vector<lint> h(N);
REP(i, N) {
lint a;
cin >> a;
h[i] = a;
}
vector<lint> dp(N);
for (lint i = N - K - 1; i < N; i++) {
dp[i] = abs(h[i] - h[N - 1]);
}
for (lint i = N - K - 2; i >= 0; i--) {
lint res = 10000000000;
for (lint k = 1; k <= K; k++) {
res = min(abs(h[i] - h[i + k]) + dp[i + k], res);
}
dp[i] = res;
}
PRINT(dp[0]);
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T) \
for_each(begin(A), end(A), [&](T i) { cout << i << " "; }); \
cout << "\n"
typedef long lint;
typedef pair<lint, lint> P;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
lint N;
cin >> N;
lint K;
cin >> K;
vector<lint> h(N);
REP(i, N) {
lint a;
cin >> a;
h[i] = a;
}
vector<lint> dp(N);
for (int i = max(N - K - 1, (lint)0); i < N; i++) {
dp[i] = abs(h[i] - h[N - 1]);
}
for (lint i = N - K - 2; i >= 0; i--) {
lint res = 10000000000;
for (lint k = 1; k <= K; k++) {
res = min(abs(h[i] - h[i + k]) + dp[i + k], res);
}
dp[i] = res;
}
PRINT(dp[0]);
return 0;
}
|
replace
| 38 | 39 | 38 | 39 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007
int main() {
int N, K;
cin >> N >> K;
vector<int> H(N);
for (int &h : H)
cin >> h;
vector<int> dp(N, INF);
dp[0] = 0;
dp[1] = abs(H[1] - H[0]);
for (int i = 2; i < N; i++) {
for (int j = 1; j <= K; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(H[i] - H[i - j]));
}
}
cout << dp[N - 1] << endl;
return 0;
}
|
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define INF 1000000000
#define MOD 1000000007
int main() {
int N, K;
cin >> N >> K;
vector<int> H(N);
for (int &h : H)
cin >> h;
vector<int> dp(N, INF);
dp[0] = 0;
dp[1] = abs(H[1] - H[0]);
for (int i = 2; i < N; i++) {
for (int j = 1; j <= K && j <= i; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(H[i] - H[i - j]));
}
}
cout << dp[N - 1] << endl;
return 0;
}
|
replace
| 29 | 30 | 29 | 30 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define inf 1e9
using namespace std;
ll min(ll a, ll b) { return (a <= b) ? a : b; }
ll find(vector<ll> &h, ll n, ll k) {
vector<ll> dp(n + 1);
dp[1] = 0;
for (ll i = 2; i <= n; i++) {
dp[i] = inf;
for (ll j = i - 1; j >= 1 && (i - j) <= k; j--)
dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
}
return dp[n];
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif
fio
ll n,
k;
cin >> n >> k;
vector<ll> h(n + 1);
for (ll i = 1; i <= n; i++)
cin >> h[i];
cout << find(h, n, k) << "\n";
}
|
#include <bits/stdc++.h>
#define ll long long int
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define inf 1e9
using namespace std;
ll min(ll a, ll b) { return (a <= b) ? a : b; }
ll find(vector<ll> &h, ll n, ll k) {
vector<ll> dp(n + 1);
dp[1] = 0;
for (ll i = 2; i <= n; i++) {
dp[i] = inf;
for (ll j = i - 1; j >= 1 && (i - j) <= k; j--)
dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
}
return dp[n];
}
int main() {
fio
ll n,
k;
cin >> n >> k;
vector<ll> h(n + 1);
for (ll i = 1; i <= n; i++)
cin >> h[i];
cout << find(h, n, k) << "\n";
}
|
delete
| 27 | 32 | 27 | 27 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
int main() {
int n, k;
scanf("%d%d", &n, &k);
int h[n];
int dp[n];
REP(i, n) {
scanf("%d", &h[i]);
dp[i] = INF;
}
dp[0] = 0;
REP(i, n) {
for (int j = 1; j <= k; j++) {
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
}
}
cout << dp[n - 1] << endl;
}
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long int ll;
int main() {
int n, k;
scanf("%d%d", &n, &k);
int h[n];
int dp[n + k];
REP(i, n) {
scanf("%d", &h[i]);
dp[i] = INF;
}
dp[0] = 0;
REP(i, n) {
for (int j = 1; j <= k; j++) {
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
}
}
cout << dp[n - 1] << endl;
}
|
replace
| 12 | 13 | 12 | 13 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("frog_in.txt", "r", stdin);
#endif
int n, k;
cin >> n >> k;
k = min(n, k);
int arr[n], dp[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
if (i < k)
dp[i] = abs(arr[0] - arr[i]);
}
for (int i = k; i < n; ++i) {
int mn = INT_MAX;
for (int j = 0; j < k; ++j)
mn = min(mn, abs(arr[i] - arr[i - j - 1]) + dp[i - j - 1]);
dp[i] = mn;
}
cout << dp[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, k;
cin >> n >> k;
k = min(n, k);
int arr[n], dp[n];
for (int i = 0; i < n; ++i) {
cin >> arr[i];
if (i < k)
dp[i] = abs(arr[0] - arr[i]);
}
for (int i = k; i < n; ++i) {
int mn = INT_MAX;
for (int j = 0; j < k; ++j)
mn = min(mn, abs(arr[i] - arr[i - j - 1]) + dp[i - j - 1]);
dp[i] = mn;
}
cout << dp[n - 1];
return 0;
}
|
delete
| 8 | 12 | 8 | 8 |
0
| |
p03161
|
C++
|
Runtime Error
|
/*
LEARNING DP: FROG 2
+ Frog need to reach stone N
+ Can jump to Stone i + 1 or i + 2 when the Frog is on Stone i
+ Cost |hi - hj|
=> Imagine if the frog were at stone i + 1 or i + 2
What is the minium cost to reach on Stone i.
+ DIFFERENT FROM FROG 1:
+ i + 1, i + 2, i + k
=> Image if the frog were at stone i + 1 or i + 2 ... i + k
Or what if we've already known the minium cost at i - 1, i - 2 .. i
- k. we now can calculate the solution for stone i PRE DP: +FIRST STATE: i =
0 => cost = 0 TIME COMPLEXITY:
+ O(n * k)
*/
#include <bits/stdc++.h>
using namespace std;
#define DB(_x) cout << #_x << " is " << (_x) << "\n";
#define FOR(_a, _b) for (_a = 0; _a < _b; ++_a)
#define IOFILE
using LL = long long;
using LD = long double;
using VI = vector<int>;
using PI = pair<int, int>;
void Excalibur() {
int n, k;
while (cin >> n >> k) {
VI a(n);
for (auto &x : a)
cin >> x;
VI dp(n, 1e9);
int i, j;
dp[0] = 0;
FOR(i, n) {
FOR(j, k + 1) {
if (i - j >= 0)
dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
}
}
cout << dp[n - 1] << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef IOFILE
freopen("INPUT.in", "r", stdin);
freopen("OUTPUT.out", "w", stdout);
#endif
Excalibur();
return 0;
}
|
/*
LEARNING DP: FROG 2
+ Frog need to reach stone N
+ Can jump to Stone i + 1 or i + 2 when the Frog is on Stone i
+ Cost |hi - hj|
=> Imagine if the frog were at stone i + 1 or i + 2
What is the minium cost to reach on Stone i.
+ DIFFERENT FROM FROG 1:
+ i + 1, i + 2, i + k
=> Image if the frog were at stone i + 1 or i + 2 ... i + k
Or what if we've already known the minium cost at i - 1, i - 2 .. i
- k. we now can calculate the solution for stone i PRE DP: +FIRST STATE: i =
0 => cost = 0 TIME COMPLEXITY:
+ O(n * k)
*/
#include <bits/stdc++.h>
using namespace std;
#define DB(_x) cout << #_x << " is " << (_x) << "\n";
#define FOR(_a, _b) for (_a = 0; _a < _b; ++_a)
// #define IOFILE
using LL = long long;
using LD = long double;
using VI = vector<int>;
using PI = pair<int, int>;
void Excalibur() {
int n, k;
while (cin >> n >> k) {
VI a(n);
for (auto &x : a)
cin >> x;
VI dp(n, 1e9);
int i, j;
dp[0] = 0;
FOR(i, n) {
FOR(j, k + 1) {
if (i - j >= 0)
dp[i] = min(dp[i], dp[i - j] + abs(a[i] - a[i - j]));
}
}
cout << dp[n - 1] << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef IOFILE
freopen("INPUT.in", "r", stdin);
freopen("OUTPUT.out", "w", stdout);
#endif
Excalibur();
return 0;
}
|
replace
| 21 | 22 | 21 | 22 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <iostream>
#include <vector>
using namespace std;
long long int INF = 1e9 + 5;
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<long long int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
vector<long long int> dp(n + 1, INF);
dp[1] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 1; i - j >= 1; j++) {
if (j <= k)
dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
}
// cout<<dp[i]<<" ";
}
cout << dp[n];
}
|
#include <iostream>
#include <vector>
using namespace std;
long long int INF = 1e9 + 5;
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<long long int> arr(n + 1);
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
vector<long long int> dp(n + 1, INF);
dp[1] = 0;
for (int i = 2; i <= n; i++) {
for (int j = 1; i - j >= 1; j++) {
if (j <= k)
dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
else {
break;
}
}
// cout<<dp[i]<<" ";
}
cout << dp[n];
}
|
insert
| 19 | 19 | 19 | 22 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
// "It does not matter how slowly you go as long as you do not stop." -
// Confucius
#include <bits/stdc++.h>
using namespace std;
/*
author : Roshan_Mehta
motto : Time Management,Consistency,Patience!!
*/
#define int long long
#ifdef Local_Debug
#define debug(x) cout << #x << " " << x << " "
#define debug1(a) cout << #a << " " << a << endl
#define debug2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define debug3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl
#define debug4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl
void show(vector<int> a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
void show(vector<vector<int>> a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void printTime(clock_t begin) {
printf("%.3lf seconds\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
}
#else
#define debug(x)
#define debug1(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, a)
void show(vector<int> a) { return; }
void show(vector<vector<int>> a) { return; }
void printTime() { return; }
#endif
#define ll long long
#define pb push_back
#define fast() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define mp make_pair
#define f first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rsort(x) sort(x, greater<int>())
#define rall(x) rbegin(x), rend(x) // useful in sorting
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define gcd __gcd
#define deci(n) fixed << setprecision(n)
#define test() \
int test_case; \
cin >> test_case; \
while (test_case--)
#define loop(i, a, n) for (int i = a; i < n; i++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int, pi> tri;
typedef pair<pi, int> rtri;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int, int> umi;
typedef map<int, int> mi;
#define input(n, k) \
int n, k; \
cin >> n; \
cin >> k; \
vi arr(n, 0); \
loop(i, 0, n) { cin >> arr[i]; }
const int MOD = 1e9 + 7;
const int md = MOD;
int Power(int n, int x) {
int ans = 1;
while (x > 0) {
if (x & 1)
ans = (ans * n) % md;
n = (n * n) % md;
x = x >> 1;
}
return ans;
}
vl fact, inv;
void inverse(ll n) {
if (n >= inv.size()) {
ll size = inv.size();
size = size == 0 ? 1 : size;
inv.resize(n + 1);
inv[0] = 1;
for (ll i = size; i <= n; i++)
inv[i] = Power(fact[i], md - 2);
}
}
void factorial(ll n) {
if (n >= fact.size()) {
ll size = fact.size();
size = size == 0 ? 1 : size;
fact.resize(n + 1);
fact[0] = 1;
for (ll i = size; i <= n; i++)
fact[i] = (fact[i - 1] * i) % md;
}
}
ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }
vl SieveOfEratosthenes(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
vl ans;
for (int p = 2; p <= n; p++)
if (prime[p])
ans.pb(p);
return ans;
}
ll NcR(int n, int r) {
long long p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
} else
p = 1;
return p; // cout << p << endl;
}
//********************THE END OF TEMPLATES*******************//
void q1();
void q2();
void q3();
int32_t main() {
clock_t begin = clock();
fast();
// test()
// {
// }
q1();
// printf("%.3lf seconds,\n", (double) (clock() - begin) / CLOCKS_PER_SEC);
return 0;
}
void q1() {
int n, k;
cin >> n >> k;
vi arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vi dp(n);
dp[0] = 0;
for (int i = 1; i < k; i++) {
dp[i] = abs(arr[0] - arr[i]);
}
// dp[1]=abs(arr[0]-arr[1]);
for (int i = k; i < n; i++) {
// dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i-2]-arr[i]));
int ans = INT_MAX;
for (int j = i - k; j < i; j++) {
ans = min(ans, abs(arr[i] - arr[j]) + dp[j]);
}
dp[i] = ans;
}
cout << dp.back() << endl;
show(dp);
}
|
// "It does not matter how slowly you go as long as you do not stop." -
// Confucius
#include <bits/stdc++.h>
using namespace std;
/*
author : Roshan_Mehta
motto : Time Management,Consistency,Patience!!
*/
#define int long long
#ifdef Local_Debug
#define debug(x) cout << #x << " " << x << " "
#define debug1(a) cout << #a << " " << a << endl
#define debug2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define debug3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl
#define debug4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl
void show(vector<int> a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
}
void show(vector<vector<int>> a) {
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < a[i].size(); j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
}
void printTime(clock_t begin) {
printf("%.3lf seconds\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
}
#else
#define debug(x)
#define debug1(x)
#define debug2(x, y)
#define debug3(x, y, z)
#define debug4(x, y, z, a)
void show(vector<int> a) { return; }
void show(vector<vector<int>> a) { return; }
void printTime() { return; }
#endif
#define ll long long
#define pb push_back
#define fast() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define mp make_pair
#define f first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rsort(x) sort(x, greater<int>())
#define rall(x) rbegin(x), rend(x) // useful in sorting
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define gcd __gcd
#define deci(n) fixed << setprecision(n)
#define test() \
int test_case; \
cin >> test_case; \
while (test_case--)
#define loop(i, a, n) for (int i = a; i < n; i++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef pair<int, pi> tri;
typedef pair<pi, int> rtri;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int, int> umi;
typedef map<int, int> mi;
#define input(n, k) \
int n, k; \
cin >> n; \
cin >> k; \
vi arr(n, 0); \
loop(i, 0, n) { cin >> arr[i]; }
const int MOD = 1e9 + 7;
const int md = MOD;
int Power(int n, int x) {
int ans = 1;
while (x > 0) {
if (x & 1)
ans = (ans * n) % md;
n = (n * n) % md;
x = x >> 1;
}
return ans;
}
vl fact, inv;
void inverse(ll n) {
if (n >= inv.size()) {
ll size = inv.size();
size = size == 0 ? 1 : size;
inv.resize(n + 1);
inv[0] = 1;
for (ll i = size; i <= n; i++)
inv[i] = Power(fact[i], md - 2);
}
}
void factorial(ll n) {
if (n >= fact.size()) {
ll size = fact.size();
size = size == 0 ? 1 : size;
fact.resize(n + 1);
fact[0] = 1;
for (ll i = size; i <= n; i++)
fact[i] = (fact[i - 1] * i) % md;
}
}
ll ncr(ll n, ll r) { return (((fact[n] * inv[r]) % md) * inv[n - r]) % md; }
vl SieveOfEratosthenes(int n) {
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
vl ans;
for (int p = 2; p <= n; p++)
if (prime[p])
ans.pb(p);
return ans;
}
ll NcR(int n, int r) {
long long p = 1, k = 1;
if (n - r < r)
r = n - r;
if (r != 0) {
while (r) {
p *= n;
k *= r;
long long m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
} else
p = 1;
return p; // cout << p << endl;
}
//********************THE END OF TEMPLATES*******************//
void q1();
void q2();
void q3();
int32_t main() {
clock_t begin = clock();
fast();
// test()
// {
// }
q1();
// printf("%.3lf seconds,\n", (double) (clock() - begin) / CLOCKS_PER_SEC);
return 0;
}
void q1() {
int n, k;
cin >> n >> k;
vi arr(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
vi dp(n);
dp[0] = 0;
for (int i = 1; i < k and i < n; i++) {
dp[i] = abs(arr[0] - arr[i]);
}
// dp[1]=abs(arr[0]-arr[1]);
for (int i = k; i < n; i++) {
// dp[i]=min(dp[i-1]+abs(arr[i]-arr[i-1]),dp[i-2]+abs(arr[i-2]-arr[i]));
int ans = INT_MAX;
for (int j = i - k; j < i; j++) {
ans = min(ans, abs(arr[i] - arr[j]) + dp[j]);
}
dp[i] = ans;
}
cout << dp.back() << endl;
show(dp);
}
|
replace
| 202 | 203 | 202 | 203 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) cin >> h[i];
vector<ll> dp(n);
rep(i, n) dp[i] = INF;
dp[0] = 0;
rep(i, n) {
rep2(j, 1, k + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
}
cout << dp[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep2(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;
ll MOD = 1000000007;
int main() {
ll n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) cin >> h[i];
vector<ll> dp(n + k);
rep(i, n) dp[i] = INF;
dp[0] = 0;
rep(i, n) {
rep2(j, 1, k + 1) { chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j])); }
}
cout << dp[n - 1] << endl;
return 0;
}
|
replace
| 32 | 33 | 32 | 33 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b) for (ll i = a; i < b; i++)
int main() {
ll n, k5;
cin >> n >> k5;
ll a[n], inf = 1000000009;
f(i, 0, n) { cin >> a[i]; }
ll dp[n];
f(k1, 0, n) { dp[k1] = inf; }
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
f(j, 2, k5 + 1) {
f(k, 0, j) { dp[j] = min(dp[j], dp[k] + abs(a[j] - a[k])); }
}
f(j2, k5 + 1, n) {
f(k3, j2 - k5, j2) { dp[j2] = min(dp[j2], dp[k3] + abs(a[k3] - a[j2])); }
}
cout << dp[n - 1];
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, a, b) for (ll i = a; i < b; i++)
int main() {
ll n, k5;
cin >> n >> k5;
ll a[n], inf = 1000000009;
f(i, 0, n) { cin >> a[i]; }
ll dp[n];
f(k1, 0, n) { dp[k1] = inf; }
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
if (n > k5 + 1) {
f(j, 2, k5 + 1) {
f(k, 0, j) { dp[j] = min(dp[j], dp[k] + abs(a[j] - a[k])); }
}
}
f(j2, k5 + 1, n) {
f(k3, j2 - k5, j2) { dp[j2] = min(dp[j2], dp[k3] + abs(a[k3] - a[j2])); }
}
cout << dp[n - 1];
}
|
replace
| 13 | 15 | 13 | 17 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1LL << 60;
vector<ll> dp(100010, INF);
vector<ll> h(100010);
int K;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll rec(int i) {
if (dp[i] != INF)
return dp[i];
if (i == 0)
return 0;
ll res = INF;
for (int j = 1; j <= K; j++) {
if (i > j - 1)
chmin(res, rec(i - j) + abs(h[i - j] - h[i]));
}
return res;
}
int main() {
int N;
cin >> N;
cin >> K;
rep(i, N) cin >> h.at(i);
cout << rec(N - 1) << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1LL << 60;
vector<ll> dp(100010, INF);
vector<ll> h(100010);
int K;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
ll rec(int i) {
if (dp[i] != INF)
return dp[i];
if (i == 0)
return 0;
ll res = INF;
for (int j = 1; j <= K; j++) {
if (i > j - 1)
chmin(res, rec(i - j) + abs(h[i - j] - h[i]));
}
return res;
}
int main() {
int N;
cin >> N;
cin >> K;
rep(i, N) cin >> h.at(i);
dp[0] = 0;
for (int i = 1; i < N; i++) {
for (int j = 1; j <= K; j++) {
if (i > j - 1)
chmin(dp[i], dp[i - j] + abs(h[i - j] - h[i]));
}
}
cout << dp[N - 1] << endl;
}
|
replace
| 45 | 46 | 45 | 54 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
// SeeAlso: https://atcoder.jp/contests/dp/tasks/dp_b
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MAX 100000
#define NIL -1
#define MOD 1000000007
typedef int _loop_int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b) \
for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)
#define debug(x) cout << #x << ": " << x << endl
#define debig_vec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
#define ALL(a) (a).begin(), (a).end()
// 最大公約数
inline constexpr ll gcd(ll a, ll b) {
if (!a || !b)
return 0;
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
// 最小公倍数
inline constexpr ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
#define print2D(h, w, arr) \
rep(i, h) { \
rep(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
template <class T> void print(const T &x) {
cout << setprecision(12) << x << endl;
}
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
int n, k;
int h[MAX];
int main() {
cin >> n >> k;
memset(h, 0, sizeof(h));
rep(i, n) { cin >> h[i]; }
ll dp[n + 5];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
rep(i, n) {
FOR(j, i + 1, i + min(k, n) + 1) {
if (dp[j] >= 0) {
dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
} else {
dp[j] = dp[i] + abs(h[j] - h[i]);
}
}
}
print(dp[n - 1]);
return 0;
}
|
// SeeAlso: https://atcoder.jp/contests/dp/tasks/dp_b
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MAX 100000
#define NIL -1
#define MOD 1000000007
typedef int _loop_int;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b) \
for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)
#define debug(x) cout << #x << ": " << x << endl
#define debig_vec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
#define ALL(a) (a).begin(), (a).end()
// 最大公約数
inline constexpr ll gcd(ll a, ll b) {
if (!a || !b)
return 0;
while (b) {
ll c = b;
b = a % b;
a = c;
}
return a;
}
// 最小公倍数
inline constexpr ll lcm(ll a, ll b) {
if (!a || !b)
return 0;
return a * b / gcd(a, b);
}
#define print2D(h, w, arr) \
rep(i, h) { \
rep(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
template <class T> void print(const T &x) {
cout << setprecision(12) << x << endl;
}
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
int n, k;
int h[MAX];
int main() {
cin >> n >> k;
memset(h, 0, sizeof(h));
rep(i, n) { cin >> h[i]; }
ll dp[n + 5];
memset(dp, -1, sizeof(dp));
dp[0] = 0;
rep(i, n) {
FOR(j, i + 1, min(i + k + 1, n)) {
if (dp[j] >= 0) {
dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
} else {
dp[j] = dp[i] + abs(h[j] - h[i]);
}
}
}
print(dp[n - 1]);
return 0;
}
|
replace
| 73 | 74 | 73 | 74 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author ExplodingFreeze
*/
#include <fstream>
#include <iostream>
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
using pii = std::pair<int, int>;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); <-- PUT
// IN main()/solve()!
using namespace std;
int n, k, a[100005], ans[100105] = {0};
class BFrog2 {
public:
void solve(std::istream &in, std::ostream &out) {
in >> n >> k;
f(i, 0, n) {
in >> a[i];
ans[i] = (int)1e18;
}
ans[0] = 0;
f(i, 0, n) f(j, 1, min(i + k + 1, n - i + 1)) ans[i + j] =
min(ans[i + j], ans[i] + abs(a[i + j] - a[i]));
out << ans[n - 1] << "\n";
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
BFrog2 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author ExplodingFreeze
*/
#include <fstream>
#include <iostream>
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define int long long
#define f(i, a, b) for (int i = (a); i < (b); i++)
#define MOD 1000000007
using pii = std::pair<int, int>;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); <-- PUT
// IN main()/solve()!
using namespace std;
int n, k, a[100005], ans[100105] = {0};
class BFrog2 {
public:
void solve(std::istream &in, std::ostream &out) {
in >> n >> k;
f(i, 0, n) {
in >> a[i];
ans[i] = (int)1e18;
}
ans[0] = 0;
f(i, 0, n) f(j, 1, min(k + 1, n - i + 1)) ans[i + j] =
min(ans[i + j], ans[i] + abs(a[i + j] - a[i]));
out << ans[n - 1] << "\n";
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
BFrog2 solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
|
replace
| 30 | 31 | 30 | 31 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
// Vaidik Patel(India)
// DA-IICT
#include <bits/stdc++.h>
using namespace std;
//********************************************
#include <ext/pb_ds/assoc_container.hpp> // include the associative containers or group of templates such as set, multimap, map etc.
#include <ext/pb_ds/tree_policy.hpp> // include the tree_order_statistics_node update
using namespace __gnu_pbds; // necessary for the GNU based Policy based data
// structures.
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//********************************************
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int INF = (int)1e9;
ll INFINF = (ll)1e18 + 10;
const ld PI = 3.14159265358979323846;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define minheap priority_queue<ll, vector<ll>, greater<ll>>
#define maxheap priority_queue<ll, vector<ll>, less<ll>>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rFOR(i, a, b) for (ll i = a; i >= b; i--)
#define deb1(x) cout << #x << " : " << x << endl;
#define deb2(x, y) cout << #x << " : " << x << "\t" << #y << " : " << y << endl;
#define deb3(x, y, z) \
cout << #x << " : " << x << "\t" << #y << " : " << y << "\t" << #z << " : " \
<< z << endl;
#define deb4(x, y, z, w) \
cout << #x << " : " << x << "\t" << #y << " : " << y << "\t" << #z << " : " \
<< z << "\t" << #w << " : " << w << endl;
#define deb5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define deb6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define checkbit(n, b) ((n >> b) & 1)
#define mkp make_pair
#define ff first
#define ss second
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define TIME \
cerr << "Time Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
#define lc 2 * idx
#define rc 2 * idx + 1
ll powermodm(ll x, ll n, ll mod) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = (x * x) % mod;
n = n / 2;
}
return result;
}
ll GCD(ll, ll);
ll LCM(ll, ll);
ll power(ll, ll);
ll choose(ll, ll);
int ones(ll);
void extendedEuclid(ll, ll);
ll MMI(ll, ll);
void fastscan(int &);
bool checkithbit(ll, ll); // ith bit is zero or not
vector<ll> SegmentTree;
void Build_Segment_Tree(vector<ll> &a, ll idx, ll l, ll r);
ll sum(ll idx, ll sl, ll sr, ll l, ll r);
void update(ll idx, ll sl, ll sr, ll value, ll id);
vector<ll> prime;
void findprime(void);
ll phi(ll num);
vector<int> isprime;
void precomputeprime(int N);
vector<int> prephi;
void precomputephi(int N);
int read();
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
#define pb push_back
#define M 1000000007
//////////////////////////////////////////////////////////////////////////////
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k;
cin >> n >> k;
vll a(n);
vll dp(n, 1e18);
FOR(i, 0, n) { cin >> a[i]; }
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
FOR(i, 2, n) {
FOR(j, 1, k + 1) { dp[i] = min(dp[i], dp[i - j] + abs(a[i - j] - a[i])); }
}
cout << dp[n - 1] << "\n";
return 0;
}
//////////////////////////////////////////////////////////////////////////
// precomputephi()
void precomputephi(int N) {
prephi.resize(N);
FOR(i, 0, N) { prephi[i] = i; }
for (int i = 1; i < N; i++) {
if (isprime[i]) {
for (int j = i; j < N; j += i) {
prephi[j] = prephi[j] - (prephi[j] / i);
}
}
}
}
// precomputedprime()
void precomputeprime(int N) {
isprime.resize(N);
isprime.assign(N, 1);
isprime[0] = 0;
isprime[1] = 0;
for (int i = 2; i * i <= 500005; i++) {
if (isprime[i]) {
for (int j = i * i; j <= 500005; j += i) {
isprime[j] = 0;
}
}
}
}
//"""euler totient function""": counts the number of integers between 1 and n
//inclusive, which are coprime to n.
ll phi(ll num) // find using factorization in O(sqrt(N)).....
{
ll result = num;
for (ll i = 2; i * i <= num; i++) {
if (num % i == 0) {
while (num % i == 0) {
num /= i;
}
result -= (result / i);
}
}
if (num > 1) {
result -= (result / num);
}
return result;
}
void findprime(void) {
int N = 1e6 + 5;
vector<bool> isprime(N, 1); // up to 1e6+5 number
for (int i = 2; i * i <= N; i++) {
if (isprime[i]) {
for (int j = i * i; j <= N; j += i) {
if (isprime[j]) {
isprime[j] = 0;
}
}
}
}
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
prime.pb(i);
}
}
}
bool checkithbit(ll a, ll i) {
if (a & (1 << i)) {
return true;
} else {
return false;
}
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (max(a, b) / GCD(a, b)) * min(a, b); }
ll power(ll a, ll n) {
unsigned long long int result = 1, x = a;
while (n > 0) {
if (n % 2 == 1)
result = result * x;
x = x * x;
n = n / 2;
}
return result;
}
ll choose(ll n, ll k) {
if (k == 0)
return 1;
return (n * choose(n - 1, k - 1)) / k;
}
int ones(ll n) {
int c = 0;
while (n) {
n = n & (n - 1);
c++;
}
return c;
}
// store gcd of a,b in d
// store solution of a*x + b*y = d in x,y
// if gcd(a,b)=1 then x = a^(-1)(mod b) and y = b^(-1)(mod a)
ll d, x, y;
void extendedEuclid(ll A, ll B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendedEuclid(B, A % B);
int temp = x;
x = y;
y = temp - (A / B) * y;
}
}
// return modulo inverse of a wrt modulo p(should be prime)
// retunt -1 if modulo inverse does not exist
ll MMI(ll a, ll p) {
extendedEuclid(a, p);
if (d == 1 && p != 1)
return ((x % p) + p) % p;
else
return -1;
}
// Fastest way to read Input
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar())
number = number * 10 + c - 48;
if (negative)
number *= -1;
}
inline int read() {
int ret = 0, t = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-')
c = getchar();
if (c == '-')
t = -1, c = getchar();
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0', c = getchar();
return ret * t;
}
void Build_Segment_Tree(vector<ll> &a, ll idx, ll l,
ll r) // where l and r is query range.
{
if (l == r) {
SegmentTree[idx] = a[l];
} else {
ll mid = (l + r) / 2ll;
Build_Segment_Tree(a, (2ll * idx), l, mid);
Build_Segment_Tree(a, (2ll * idx) + 1, mid + 1, r);
SegmentTree[idx] = SegmentTree[2ll * idx] + SegmentTree[2ll * idx + 1];
}
}
ll sum(ll idx, ll sl, ll sr, ll l,
ll r) // here [sl...sr] segment range associate at idx index. where l and
// r is query range.
{
if (l > r) {
return 0;
}
if (sl == l && sr == r) {
return SegmentTree[idx];
}
ll mid = (sl + sr) / 2;
return sum(2ll * idx, sl, mid, l, min(r, mid)) +
sum(2ll * idx + 1, mid + 1, sr, max(mid + 1, l), r);
}
void update(ll idx, ll sl, ll sr, ll value, ll id) {
if (sl == sr && sl == id) {
SegmentTree[idx] = value;
} else {
ll mid = (sl + sr) / 2ll;
if (id <= mid) {
update(2ll * idx, sl, mid, value, id);
} else {
update(2ll * idx + 1, mid + 1, sr, value, id);
}
SegmentTree[idx] = SegmentTree[2ll * idx] + SegmentTree[2ll * idx + 1];
}
}
/*
note:
//a.resize(unique(a.begin(), a.end()) - a.begin()); use for create array unique
element...
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
shuffle(g1.begin(), g1.end(), rng);(g1 is array name) for choose random number
//int grand(int x) {
return uniform_int_distribution<int>(0, x - 1)(rng);
}
//use once(num) give no of 1 in its binary form
//use give (int)log2(num)+1 give no of digits in its binary form
//bitset<64>(num).to_string() convert decimal to num64 bit binary number
//stoull(str, nullptr, 2) convert str in binary form to decimal number
// "max" priority queue: priority_queue<ll, vector<ll>, less<ll> >pq; #select
max element as top()#
// "min" priority queue: priority_queue<ll, vector<ll>, greater< ll> >pq;
#select min element as top()#
//set<int> inbuit sort accending order (set<int,less<int>>)
//set<int,greater<int>> sort element in decreasing order
// int __builtin_popcount(unsigned int)
--->It returns the numbers of set bits in an integer (the number of ones in the
binary representation of the integer).
///for finding xnor from xor
x1=Xor
x2=Xnor
ll x1=a ^ b;
ll num=max(a,b);
ll xx=log2(num)+ 1; (length of num in binary form)
****ll t=(1 << xx) - 1; (set all xx length bits to 1)
ll x2=x1 ^ t; (get xnor via xor using t)
--->Dilworth's theorem:-
this theorem says that the minimum number of non-decreasing sequences we
need to cover the whole sequence equals the length of least decreasing
subsequence.
----->a + b = (a xor b) +( 2 * (a & b)); ///very imp
*/
|
// Vaidik Patel(India)
// DA-IICT
#include <bits/stdc++.h>
using namespace std;
//********************************************
#include <ext/pb_ds/assoc_container.hpp> // include the associative containers or group of templates such as set, multimap, map etc.
#include <ext/pb_ds/tree_policy.hpp> // include the tree_order_statistics_node update
using namespace __gnu_pbds; // necessary for the GNU based Policy based data
// structures.
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//********************************************
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int INF = (int)1e9;
ll INFINF = (ll)1e18 + 10;
const ld PI = 3.14159265358979323846;
#define precision(x, d) cout << fixed << setprecision(d) << x
#define minheap priority_queue<ll, vector<ll>, greater<ll>>
#define maxheap priority_queue<ll, vector<ll>, less<ll>>
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define rFOR(i, a, b) for (ll i = a; i >= b; i--)
#define deb1(x) cout << #x << " : " << x << endl;
#define deb2(x, y) cout << #x << " : " << x << "\t" << #y << " : " << y << endl;
#define deb3(x, y, z) \
cout << #x << " : " << x << "\t" << #y << " : " << y << "\t" << #z << " : " \
<< z << endl;
#define deb4(x, y, z, w) \
cout << #x << " : " << x << "\t" << #y << " : " << y << "\t" << #z << " : " \
<< z << "\t" << #w << " : " << w << endl;
#define deb5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define deb6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define checkbit(n, b) ((n >> b) & 1)
#define mkp make_pair
#define ff first
#define ss second
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define TIME \
cerr << "Time Elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
#define lc 2 * idx
#define rc 2 * idx + 1
ll powermodm(ll x, ll n, ll mod) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = (x * x) % mod;
n = n / 2;
}
return result;
}
ll GCD(ll, ll);
ll LCM(ll, ll);
ll power(ll, ll);
ll choose(ll, ll);
int ones(ll);
void extendedEuclid(ll, ll);
ll MMI(ll, ll);
void fastscan(int &);
bool checkithbit(ll, ll); // ith bit is zero or not
vector<ll> SegmentTree;
void Build_Segment_Tree(vector<ll> &a, ll idx, ll l, ll r);
ll sum(ll idx, ll sl, ll sr, ll l, ll r);
void update(ll idx, ll sl, ll sr, ll value, ll id);
vector<ll> prime;
void findprime(void);
ll phi(ll num);
vector<int> isprime;
void precomputeprime(int N);
vector<int> prephi;
void precomputephi(int N);
int read();
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<pii> vpii;
#define pb push_back
#define M 1000000007
//////////////////////////////////////////////////////////////////////////////
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n, k;
cin >> n >> k;
vll a(n);
vll dp(n, 1e18);
FOR(i, 0, n) { cin >> a[i]; }
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
FOR(i, 2, n) {
FOR(j, 1, k + 1) {
if (i - j >= 0)
dp[i] = min(dp[i], dp[i - j] + abs(a[i - j] - a[i]));
}
}
cout << dp[n - 1] << "\n";
return 0;
}
//////////////////////////////////////////////////////////////////////////
// precomputephi()
void precomputephi(int N) {
prephi.resize(N);
FOR(i, 0, N) { prephi[i] = i; }
for (int i = 1; i < N; i++) {
if (isprime[i]) {
for (int j = i; j < N; j += i) {
prephi[j] = prephi[j] - (prephi[j] / i);
}
}
}
}
// precomputedprime()
void precomputeprime(int N) {
isprime.resize(N);
isprime.assign(N, 1);
isprime[0] = 0;
isprime[1] = 0;
for (int i = 2; i * i <= 500005; i++) {
if (isprime[i]) {
for (int j = i * i; j <= 500005; j += i) {
isprime[j] = 0;
}
}
}
}
//"""euler totient function""": counts the number of integers between 1 and n
//inclusive, which are coprime to n.
ll phi(ll num) // find using factorization in O(sqrt(N)).....
{
ll result = num;
for (ll i = 2; i * i <= num; i++) {
if (num % i == 0) {
while (num % i == 0) {
num /= i;
}
result -= (result / i);
}
}
if (num > 1) {
result -= (result / num);
}
return result;
}
void findprime(void) {
int N = 1e6 + 5;
vector<bool> isprime(N, 1); // up to 1e6+5 number
for (int i = 2; i * i <= N; i++) {
if (isprime[i]) {
for (int j = i * i; j <= N; j += i) {
if (isprime[j]) {
isprime[j] = 0;
}
}
}
}
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
prime.pb(i);
}
}
}
bool checkithbit(ll a, ll i) {
if (a & (1 << i)) {
return true;
} else {
return false;
}
}
ll GCD(ll a, ll b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
ll LCM(ll a, ll b) { return (max(a, b) / GCD(a, b)) * min(a, b); }
ll power(ll a, ll n) {
unsigned long long int result = 1, x = a;
while (n > 0) {
if (n % 2 == 1)
result = result * x;
x = x * x;
n = n / 2;
}
return result;
}
ll choose(ll n, ll k) {
if (k == 0)
return 1;
return (n * choose(n - 1, k - 1)) / k;
}
int ones(ll n) {
int c = 0;
while (n) {
n = n & (n - 1);
c++;
}
return c;
}
// store gcd of a,b in d
// store solution of a*x + b*y = d in x,y
// if gcd(a,b)=1 then x = a^(-1)(mod b) and y = b^(-1)(mod a)
ll d, x, y;
void extendedEuclid(ll A, ll B) {
if (B == 0) {
d = A;
x = 1;
y = 0;
} else {
extendedEuclid(B, A % B);
int temp = x;
x = y;
y = temp - (A / B) * y;
}
}
// return modulo inverse of a wrt modulo p(should be prime)
// retunt -1 if modulo inverse does not exist
ll MMI(ll a, ll p) {
extendedEuclid(a, p);
if (d == 1 && p != 1)
return ((x % p) + p) % p;
else
return -1;
}
// Fastest way to read Input
void fastscan(int &number) {
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c == '-') {
negative = true;
c = getchar();
}
for (; (c > 47 && c < 58); c = getchar())
number = number * 10 + c - 48;
if (negative)
number *= -1;
}
inline int read() {
int ret = 0, t = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-')
c = getchar();
if (c == '-')
t = -1, c = getchar();
while (c >= '0' && c <= '9')
ret = ret * 10 + c - '0', c = getchar();
return ret * t;
}
void Build_Segment_Tree(vector<ll> &a, ll idx, ll l,
ll r) // where l and r is query range.
{
if (l == r) {
SegmentTree[idx] = a[l];
} else {
ll mid = (l + r) / 2ll;
Build_Segment_Tree(a, (2ll * idx), l, mid);
Build_Segment_Tree(a, (2ll * idx) + 1, mid + 1, r);
SegmentTree[idx] = SegmentTree[2ll * idx] + SegmentTree[2ll * idx + 1];
}
}
ll sum(ll idx, ll sl, ll sr, ll l,
ll r) // here [sl...sr] segment range associate at idx index. where l and
// r is query range.
{
if (l > r) {
return 0;
}
if (sl == l && sr == r) {
return SegmentTree[idx];
}
ll mid = (sl + sr) / 2;
return sum(2ll * idx, sl, mid, l, min(r, mid)) +
sum(2ll * idx + 1, mid + 1, sr, max(mid + 1, l), r);
}
void update(ll idx, ll sl, ll sr, ll value, ll id) {
if (sl == sr && sl == id) {
SegmentTree[idx] = value;
} else {
ll mid = (sl + sr) / 2ll;
if (id <= mid) {
update(2ll * idx, sl, mid, value, id);
} else {
update(2ll * idx + 1, mid + 1, sr, value, id);
}
SegmentTree[idx] = SegmentTree[2ll * idx] + SegmentTree[2ll * idx + 1];
}
}
/*
note:
//a.resize(unique(a.begin(), a.end()) - a.begin()); use for create array unique
element...
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
shuffle(g1.begin(), g1.end(), rng);(g1 is array name) for choose random number
//int grand(int x) {
return uniform_int_distribution<int>(0, x - 1)(rng);
}
//use once(num) give no of 1 in its binary form
//use give (int)log2(num)+1 give no of digits in its binary form
//bitset<64>(num).to_string() convert decimal to num64 bit binary number
//stoull(str, nullptr, 2) convert str in binary form to decimal number
// "max" priority queue: priority_queue<ll, vector<ll>, less<ll> >pq; #select
max element as top()#
// "min" priority queue: priority_queue<ll, vector<ll>, greater< ll> >pq;
#select min element as top()#
//set<int> inbuit sort accending order (set<int,less<int>>)
//set<int,greater<int>> sort element in decreasing order
// int __builtin_popcount(unsigned int)
--->It returns the numbers of set bits in an integer (the number of ones in the
binary representation of the integer).
///for finding xnor from xor
x1=Xor
x2=Xnor
ll x1=a ^ b;
ll num=max(a,b);
ll xx=log2(num)+ 1; (length of num in binary form)
****ll t=(1 << xx) - 1; (set all xx length bits to 1)
ll x2=x1 ^ t; (get xnor via xor using t)
--->Dilworth's theorem:-
this theorem says that the minimum number of non-decreasing sequences we
need to cover the whole sequence equals the length of least decreasing
subsequence.
----->a + b = (a xor b) +( 2 * (a & b)); ///very imp
*/
|
replace
| 111 | 112 | 111 | 115 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> v(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<ll> dp(n, INT_MAX);
dp[0] = 0;
for (int i = 1; i < k; i++) {
dp[i] = abs(v[i] - v[0]);
}
for (int i = k; i < n; i++) {
for (int j = 1; j <= k; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
}
}
cout << dp[n - 1] << endl;
}
|
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
ll n, k;
cin >> n >> k;
vector<ll> v(n, 0);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<ll> dp(n, INT_MAX);
dp[0] = 0;
k = min(k, n);
for (int i = 1; i < k; i++) {
dp[i] = abs(v[i] - v[0]);
}
for (int i = k; i < n; i++) {
for (int j = 1; j <= k; j++) {
dp[i] = min(dp[i], dp[i - j] + abs(v[i] - v[i - j]));
}
}
cout << dp[n - 1] << endl;
}
|
insert
| 13 | 13 | 13 | 14 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = n - 1; i > m; i--)
#define pb(n) push_back(n)
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl;
#define Vecpr vector<pair<lli, lli>>
#define mod 1000000007;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
lli A, B, N, M;
cin >> N >> M;
vector<int> DP(N, INF), Ct(N, 0);
rep(i, 0, N) cin >> Ct[i];
DP[0] = 0;
rep(i, 0, N) rep(j, 1, M + 1)
chmin(DP[i + j], DP[i] + abs(Ct[i] - Ct[i + j]));
Out(DP[N - 1]);
}
|
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = n - 1; i > m; i--)
#define pb(n) push_back(n)
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl;
#define Vecpr vector<pair<lli, lli>>
#define mod 1000000007;
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
lli A, B, N, M;
cin >> N >> M;
vector<int> DP(N, INF), Ct(N, 0);
rep(i, 0, N) cin >> Ct[i];
DP[0] = 0;
rep(i, 0, N) rep(j, 1, M + 1) {
if (i + j > N - 1)
continue;
chmin(DP[i + j], DP[i] + abs(Ct[i] - Ct[i + j]));
}
Out(DP[N - 1]);
}
|
replace
| 23 | 25 | 23 | 28 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(void) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> dp(10010, 1e9);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
chmin(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
}
}
cout << dp[n - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main(void) {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> dp(100010, 1e9);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
chmin(dp[i + j], dp[i] + abs(a[i] - a[i + j]));
}
}
cout << dp[n - 1] << endl;
}
|
replace
| 23 | 24 | 23 | 24 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
// edpc_b
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (int)(2e9)
#define MOD (1000000007)
#define print2D(h, w, arr) \
REP(i, h) { \
REP(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
#define print_line(vec, n) \
{ \
for (int i = 0; i < (n - 1); i++) \
cout << (vec)[i] << " "; \
cout << (vec)[(n)-1] << endl; \
}
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
struct PreMain {
PreMain() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} premain;
int N, K;
vector<int> H;
// vector<int> memo;
map<int, int> memo;
int dp_func(int i) {
if (i == N - 1)
return 0;
if (memo.count(i))
return memo[i];
// if (memo[i] != -1) return memo[i];
int tmp = INF;
REPN(k, 1, K + 1) {
if (i + k >= N)
break;
tmp = min(tmp, dp_func(i + k) + abs(H[i] - H[i + k]));
}
return memo[i] = tmp;
};
int main() {
#ifdef LOCAL
ifstream in("../arg.txt");
cin.rdbuf(in.rdbuf());
#endif
cin >> N >> K;
H.assign(N, 0);
REP(i, N) cin >> H[i];
// memo.assign(N, -1);
int ans = dp_func(0);
print(ans);
return 0;
}
|
// edpc_b
#include <bits/stdc++.h>
#ifdef LOCAL
#include "../cxx-prettyprint/prettyprint.hpp"
#endif
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define REP_REV(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define REPN_REV(i, m, n) for (int(i) = (int)(n)-1; (i) >= m; --(i))
#define ALL(x) x.begin(), x.end()
#define INF (int)(2e9)
#define MOD (1000000007)
#define print2D(h, w, arr) \
REP(i, h) { \
REP(j, w) cout << arr[i][j] << " "; \
cout << endl; \
}
#define print_line(vec, n) \
{ \
for (int i = 0; i < (n - 1); i++) \
cout << (vec)[i] << " "; \
cout << (vec)[(n)-1] << endl; \
}
template <class T> void print(const T &x) { cout << x << endl; }
template <class T, class... A> void print(const T &first, const A &...rest) {
cout << first << " ";
print(rest...);
}
struct PreMain {
PreMain() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} premain;
int N, K;
vector<int> H;
// vector<int> memo;
unordered_map<int, int> memo;
int dp_func(int i) {
if (i == N - 1)
return 0;
if (memo.count(i))
return memo[i];
// if (memo[i] != -1) return memo[i];
int tmp = INF;
REPN(k, 1, K + 1) {
if (i + k >= N)
break;
tmp = min(tmp, dp_func(i + k) + abs(H[i] - H[i + k]));
}
return memo[i] = tmp;
};
int main() {
#ifdef LOCAL
ifstream in("../arg.txt");
cin.rdbuf(in.rdbuf());
#endif
cin >> N >> K;
H.assign(N, 0);
REP(i, N) cin >> H[i];
// memo.assign(N, -1);
int ans = dp_func(0);
print(ans);
return 0;
}
|
replace
| 47 | 48 | 47 | 48 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define FastRead \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define ld long double
#define FOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define FI(i, n) for (int i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define f first
// #define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;
// I am questioning life and universe and
// everything else after looking at this
const ll MAXN = 1e5 + 5;
ll a[MAXN], dp[MAXN];
ll n, k;
ll rec(ll idx) {
if (idx > n)
return 1e10;
if (idx == n)
return 0;
ll &ans = dp[idx];
if (ans != -1)
return ans;
ans = 1e10;
FOR(i, 1, k) {
if (idx + i <= n)
ans = min(ans, rec(idx + i) + abs(a[idx + i] - a[idx]));
}
return ans;
}
void solve() {
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
memset(dp, -1, sizeof(dp));
ll ans = rec(1);
cout << ans << endl;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
FastRead;
ll t;
t = 1;
// cin>>t;
while (t--)
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define FastRead \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define ll long long int
#define ld long double
#define FOR(i, a, n) for (int i = (a); i <= (n); ++i)
#define RFOR(i, a, n) for (int i = (n); i >= (a); --i)
#define FI(i, n) for (int i = 0; i < (n); ++i)
#define ZERO(a) memset((a), 0, sizeof((a)))
#define f first
// #define s second
#define pb push_back
#define mk make_pair
#define all(g) g.begin(), g.end()
int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; }
int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; }
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including
// tree_order_statistics_node_updat using namespace __gnu_pbds; typedef tree<ll,
// null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
// ordered_set;
// I am questioning life and universe and
// everything else after looking at this
const ll MAXN = 1e5 + 5;
ll a[MAXN], dp[MAXN];
ll n, k;
ll rec(ll idx) {
if (idx > n)
return 1e10;
if (idx == n)
return 0;
ll &ans = dp[idx];
if (ans != -1)
return ans;
ans = 1e10;
FOR(i, 1, k) {
if (idx + i <= n)
ans = min(ans, rec(idx + i) + abs(a[idx + i] - a[idx]));
}
return ans;
}
void solve() {
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
memset(dp, -1, sizeof(dp));
ll ans = rec(1);
cout << ans << endl;
}
signed main() {
FastRead;
ll t;
t = 1;
// cin>>t;
while (t--)
solve();
}
|
delete
| 66 | 71 | 66 | 66 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int n, k;
cin >> n >> k;
int h[n + 1], dp[n + 1];
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 2; i <= n; i++)
dp[i] = INT_MAX;
dp[0] = dp[1] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
cout << endl << dp[n];
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int n, k;
cin >> n >> k;
int h[n + 1], dp[n + 1];
for (int i = 1; i <= n; i++)
cin >> h[i];
for (int i = 2; i <= n; i++)
dp[i] = INT_MAX;
dp[0] = dp[1] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
if (i + j <= n + 1)
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
cout << dp[n] << endl;
return 0;
}
|
replace
| 16 | 18 | 16 | 19 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int arr[n];
int i, j;
int dp[n];
for (i = 0; i < n; i++) {
dp[i] = 0;
cin >> arr[i];
}
for (i = 1; i < n; i++) {
dp[i] = INT_MAX;
for (j = i - 1; j >= 0; j--) {
int a1 = INT_MAX;
if (i - j <= k) {
a1 = abs(arr[i] - arr[j]) + dp[j];
}
dp[i] = min(dp[i], a1);
}
}
cout << dp[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int arr[n];
int i, j;
int dp[n];
for (i = 0; i < n; i++) {
dp[i] = 0;
cin >> arr[i];
}
for (i = 1; i < n; i++) {
dp[i] = INT_MAX;
for (j = i - 1; j >= i - k && j >= 0; j--) {
int a1 = abs(arr[i] - arr[j]) + dp[j];
dp[i] = min(dp[i], a1);
}
}
cout << dp[n - 1];
return 0;
}
|
replace
| 14 | 19 | 14 | 17 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr ll inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */
/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/* ----------------------end----------------------- */
/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
if (i == 0)
return 1;
return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n /= 10;
count++;
}
return count;
}
ll ketasum(ll n) { // 各桁の和
ll sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
int n, k;
cin >> n >> k;
vi h(n);
rep(i, n) cin >> h[i];
vi v(n, inf);
v[0] = 0;
rep(i, n) {
rep1(j, k) { chmin(v[i + j], abs(h[i + j] - h[i]) + v[i]); }
}
cout << v[n - 1] << endl;
return 0;
}
|
#pragma region header
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr ll inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */
/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/* ----------------------end----------------------- */
/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
if (i == 0)
return 1;
return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n /= 10;
count++;
}
return count;
}
ll ketasum(ll n) { // 各桁の和
ll sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
int n, k;
cin >> n >> k;
vi h(n);
rep(i, n) cin >> h[i];
vi v(n + 110, inf);
v[0] = 0;
rep(i, n) {
rep1(j, k) { chmin(v[i + j], abs(h[i + j] - h[i]) + v[i]); }
}
cout << v[n - 1] << endl;
return 0;
}
|
replace
| 85 | 86 | 85 | 86 |
-6
|
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
if (N == 1) {
cout << 0;
return 0;
}
int h[N];
for (int i = 0; i < N; i++) {
cin >> h[i];
}
int val[N];
for (int i = 0; i < N; i++) {
val[i] = INT_MAX;
}
val[0] = 0;
for (int i = 1; i < N; i++) {
for (int j = i - 1; j >= 0 && i - j <= K; j++) {
val[i] = min(val[i], abs(h[i] - h[j]) + val[j]);
}
}
cout << val[N - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
if (N == 1) {
cout << 0;
return 0;
}
int h[N];
for (int i = 0; i < N; i++) {
cin >> h[i];
}
int val[N];
for (int i = 0; i < N; i++) {
val[i] = INT_MAX;
}
val[0] = 0;
for (int i = 1; i < N; i++) {
for (int j = i - 1; j >= 0 && i - j <= K; j--) {
val[i] = min(val[i], abs(h[i] - h[j]) + val[j]);
}
}
cout << val[N - 1];
return 0;
}
|
replace
| 24 | 25 | 24 | 25 |
-11
| |
p03161
|
C++
|
Runtime Error
|
/*
I_Love_Trueno
Author- darklight13
Smoke_Weed_Everyday
*/
#define _CRT_SECURE_NO_DEPRECATE
#define V__0_0__V
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define SYNC \
std::ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define FRE \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define view(x) cout << (#x) << " is " << (x) << endl
#define view2(x, y) \
cout << (#x) << " " << (#y) << " are " << (x) << " " << (y) << endl
#define pb push_back
#define F first
#define S second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
#define all(v) (v).begin(), (v).end()
#define sz(a) (ll)((a).size())
#define sq(x) ((x) * (x))
#define pii pair<ll, ll>
#define endl '\n'
#define pow (ll) pow
int32_t main() {
// FRE
SYNC int n, k;
cin >> n >> k;
ll h[n + 1];
for (int i = 1; i <= n; i++)
cin >> h[i];
ll dp[n + 1];
for (int i = 1; i <= k; i++) {
dp[i] = abs(h[i] - h[1]);
}
for (int i = k + 1; i <= n; i++) {
dp[i] = LLINF;
for (int j = 1; j <= k; j++) {
if (j >= i)
break;
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
if (dp[i] == LLINF)
dp[i] = 0;
}
cout << dp[n];
return 0;
}
|
/*
I_Love_Trueno
Author- darklight13
Smoke_Weed_Everyday
*/
#define _CRT_SECURE_NO_DEPRECATE
#define V__0_0__V
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define SYNC \
std::ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define FRE \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define view(x) cout << (#x) << " is " << (x) << endl
#define view2(x, y) \
cout << (#x) << " " << (#y) << " are " << (x) << " " << (y) << endl
#define pb push_back
#define F first
#define S second
#define mp make_pair
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
#define all(v) (v).begin(), (v).end()
#define sz(a) (ll)((a).size())
#define sq(x) ((x) * (x))
#define pii pair<ll, ll>
#define endl '\n'
#define pow (ll) pow
int32_t main() {
// FRE
SYNC int n, k;
cin >> n >> k;
ll h[n + 1];
for (int i = 1; i <= n; i++)
cin >> h[i];
ll dp[n + 1];
if (k >= n) {
cout << abs(h[n] - h[1]);
exit(0);
}
for (int i = 1; i <= k; i++) {
dp[i] = abs(h[i] - h[1]);
}
for (int i = k + 1; i <= n; i++) {
dp[i] = LLINF;
for (int j = 1; j <= k; j++) {
if (j >= i)
break;
dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
}
if (dp[i] == LLINF)
dp[i] = 0;
}
cout << dp[n];
return 0;
}
|
insert
| 45 | 45 | 45 | 49 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> h(n);
for (int i = 0; i < n; i++)
cin >> h[i];
vector<int> cost(n, INT_MAX);
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 2; i < n; i++) {
for (int j = 1; j <= k; j++) {
cost[i] = min(cost[i], abs(h[i] - h[i - j]) + cost[i - j]);
}
}
cout << cost[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> h(n);
for (int i = 0; i < n; i++)
cin >> h[i];
vector<int> cost(n, INT_MAX);
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 2; i < n; i++) {
for (int j = 1; j <= k && i - j >= 0; j++) {
cost[i] = min(cost[i], abs(h[i] - h[i - j]) + cost[i - j]);
}
}
cout << cost[n - 1];
return 0;
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
// /$$$$ /$$ /$$$$ /$$$$$$ /$$
// | $$_/ | $$ |_ $$ /$$__ $$ | $$
// | $$ /$$$$$$$| $$$$$$$ /$$$$$$ | $$|__/ \ $$ /$$$$$$$ | $$ /$$
// | $$ /$$_____/| $$__ $$ |____ $$ | $$ /$$$$$$/| $$__ $$| $$ /$$/
// | $$ | $$$$$$ | $$ \ $$ /$$$$$$$ | $$ /$$____/ | $$ \ $$| $$$$$$/
// | $$ \____ $$| $$ | $$ /$$__ $$ | $$| $$ | $$ | $$| $$_ $$
// | $$$$ /$$$$$$$/| $$ | $$| $$$$$$$ /$$$$| $$$$$$$$| $$ | $$| $$ \ $$
// |____/|_______/ |__/ |__/ \_______/|____/|________/|__/ |__/|__/ \__/
//
//
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define po2(z) 1 << (ll)z
#define fr(i, x, n) for (ll i = (ll)(x); i < (ll)(n); ++i)
#define rf(i, x, n) for (ll i = (ll)(x); i >= (ll)(n); --i)
#define modinv(x) powah(x, mod - 2)
#define ncr(n, r) \
(((fact[n] * modinv(fact[n - r])) % mod * modinv(fact[r])) % mod)
#define factorial \
fact.pb(1); \
sidha(i, 1, MAX) fact.pb((fact[i - 1] * i) % mod)
#define tej \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define mod (ll)1000000007
#define MAX 100005
#define Vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define ff first
#define ss second
#define vpii vector<pii>
#define endl '\n'
//--------------------------------------------------//
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> h(n);
for (ll i = 0; i < n; i++) {
cin >> h[i];
}
ll dp[n];
for (ll i = 0; i < n; i++)
dp[i] = 1e10;
dp[0] = 0;
for (ll i = 0; i < n; i++) {
for (ll j = 1; j <= min(i + k, n - 1); j++) {
dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
}
}
cout << dp[n - 1];
}
//--------------------------------------------------//
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
|
// /$$$$ /$$ /$$$$ /$$$$$$ /$$
// | $$_/ | $$ |_ $$ /$$__ $$ | $$
// | $$ /$$$$$$$| $$$$$$$ /$$$$$$ | $$|__/ \ $$ /$$$$$$$ | $$ /$$
// | $$ /$$_____/| $$__ $$ |____ $$ | $$ /$$$$$$/| $$__ $$| $$ /$$/
// | $$ | $$$$$$ | $$ \ $$ /$$$$$$$ | $$ /$$____/ | $$ \ $$| $$$$$$/
// | $$ \____ $$| $$ | $$ /$$__ $$ | $$| $$ | $$ | $$| $$_ $$
// | $$$$ /$$$$$$$/| $$ | $$| $$$$$$$ /$$$$| $$$$$$$$| $$ | $$| $$ \ $$
// |____/|_______/ |__/ |__/ \_______/|____/|________/|__/ |__/|__/ \__/
//
//
//
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define po2(z) 1 << (ll)z
#define fr(i, x, n) for (ll i = (ll)(x); i < (ll)(n); ++i)
#define rf(i, x, n) for (ll i = (ll)(x); i >= (ll)(n); --i)
#define modinv(x) powah(x, mod - 2)
#define ncr(n, r) \
(((fact[n] * modinv(fact[n - r])) % mod * modinv(fact[r])) % mod)
#define factorial \
fact.pb(1); \
sidha(i, 1, MAX) fact.pb((fact[i - 1] * i) % mod)
#define tej \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define mod (ll)1000000007
#define MAX 100005
#define Vii vector<ll>
#define pb push_back
#define pii pair<ll, ll>
#define ff first
#define ss second
#define vpii vector<pii>
#define endl '\n'
//--------------------------------------------------//
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> h(n);
for (ll i = 0; i < n; i++) {
cin >> h[i];
}
ll dp[n];
for (ll i = 0; i < n; i++)
dp[i] = 1e10;
dp[0] = 0;
for (ll i = 0; i < n; i++) {
for (ll j = i + 1; j <= i + k; j++) {
if (j < n) {
dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]));
}
}
}
cout << dp[n - 1];
}
//--------------------------------------------------//
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}
|
replace
| 59 | 62 | 59 | 63 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
if (!r)
return 1;
else if (r % 2 == 0)
return powe(n * n, r / 2);
else
return n * powe(n * n, (r - 1) / 2);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
// cin>>t;
while (t--) {
ll n, k;
cin >> n >> k;
vector<ll> h(n);
vector<ll> dp(n, inf);
for (ll i = 0; i < n; i++)
cin >> h[i];
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (ll i = 2; i < n; i++) {
for (ll j = i - 1; j >= 0 && j >= i - k; j--) {
dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
}
}
cout << dp[n - 1] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 1000000000000000000
#define fr first
#define sc second
#define pb push_back
#define pf push_front
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, a, n) for (ll i = n - 1; i >= a; i--)
#define pll pair<ll, ll>
#define vll vector<ll>
#define umap unordered_map<ll, ll>
#define lpq priority_queue<ll, vector<ll>, greater<ll>>
ll powe(ll n, ll r) {
if (!r)
return 1;
else if (r % 2 == 0)
return powe(n * n, r / 2);
else
return n * powe(n * n, (r - 1) / 2);
}
int main() {
/*
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif*/
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
// cin>>t;
while (t--) {
ll n, k;
cin >> n >> k;
vector<ll> h(n);
vector<ll> dp(n, inf);
for (ll i = 0; i < n; i++)
cin >> h[i];
dp[0] = 0;
dp[1] = abs(h[1] - h[0]);
for (ll i = 2; i < n; i++) {
for (ll j = i - 1; j >= 0 && j >= i - k; j--) {
dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]));
}
}
cout << dp[n - 1] << endl;
}
return 0;
}
|
replace
| 26 | 31 | 26 | 31 |
-6
|
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test() \
ull t; \
cin >> t; \
while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define oa(a, n) \
for (ll i = 0; i < n; i++) \
cout << a[i] << " "; \
nl
#define cn(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i]
#define ov(a) \
for (ll i = 0; i < a.size(); i++) \
cout << a[i] << " "; \
nl
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
int main() {
fastio();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("outpu.txt", "w", stdout);
#endif
ll N, k;
cin >> N >> k;
ll ar[N];
cn(ar, N);
ll g[N];
g[0] = 0;
for (ll i = 1; i < N; i++) {
ll m = abs(ar[i] - ar[i - 1]) + g[i - 1];
for (int j = 2; j <= min(k, i); j++) {
m = min(m, abs(ar[i] - ar[i - j]) + g[i - j]);
}
g[i] = m;
}
cout << g[N - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define ll long long
#define MOD 1000000007
#define nl cout << endl
#define rt ll Q[100005] = {0}
#define mp make_pair
#define test() \
ull t; \
cin >> t; \
while (t--)
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define oa(a, n) \
for (ll i = 0; i < n; i++) \
cout << a[i] << " "; \
nl
#define cn(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i]
#define ov(a) \
for (ll i = 0; i < a.size(); i++) \
cout << a[i] << " "; \
nl
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
int main() {
fastio();
ll N, k;
cin >> N >> k;
ll ar[N];
cn(ar, N);
ll g[N];
g[0] = 0;
for (ll i = 1; i < N; i++) {
ll m = abs(ar[i] - ar[i - 1]) + g[i - 1];
for (int j = 2; j <= min(k, i); j++) {
m = min(m, abs(ar[i] - ar[i - j]) + g[i - j]);
}
g[i] = m;
}
cout << g[N - 1] << endl;
}
|
delete
| 34 | 38 | 34 | 34 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast;
ll n, k, j;
cin >> n >> k;
ll a[n], i;
rep(i, 0, n) { cin >> a[i]; }
ll dp[n];
memset(dp, 10001, sizeof(dp));
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (i = 2; i < min(n, k); i++) {
for (j = 1; j <= i; j++) {
dp[i] = min(dp[i - j] + abs(a[i] - a[i - j]), dp[i]);
}
}
for (i = k; i < n; i++) {
for (j = 1; j <= k; j++) {
dp[i] = min(dp[i - j] + abs(a[i] - a[i - j]), dp[i]);
}
}
cout << dp[n - 1] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define mod 1000000007
int main() {
fast;
ll n, k, j;
cin >> n >> k;
ll a[n], i;
rep(i, 0, n) { cin >> a[i]; }
ll dp[n];
memset(dp, 10001, sizeof(dp));
dp[0] = 0;
dp[1] = abs(a[1] - a[0]);
for (i = 2; i < min(n, k); i++) {
for (j = 1; j <= i; j++) {
dp[i] = min(dp[i - j] + abs(a[i] - a[i - j]), dp[i]);
}
}
for (i = k; i < n; i++) {
for (j = 1; j <= k; j++) {
dp[i] = min(dp[i - j] + abs(a[i] - a[i - j]), dp[i]);
}
}
cout << dp[n - 1] << "\n";
}
|
delete
| 11 | 15 | 11 | 11 |
-11
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int K;
cin >> K;
vector<int> h(N);
for (int i = 0; i < N; i++) {
cin >> h[i];
}
vector<int> dp(N);
dp[0] = h[0];
dp[1] = abs(h[0] - h[1]);
for (int i = 2; i <= K; i++) {
int this_min = INT_MAX;
for (int j = i - 1; j >= 1; j--) {
int curr_min = dp[j] + abs(h[j] - h[i]);
this_min = (curr_min < this_min ? curr_min : this_min);
}
dp[i] = min(abs(h[0] - h[i]), this_min);
}
for (int i = K + 1; i < N; i++) {
int dp_min = INT_MAX;
for (int j = i - 1; j >= max((i - 1 - (K - 1)), 1); j--) {
int dp_curr_min = dp[j] + abs(h[j] - h[i]);
dp_min = (dp_curr_min < dp_min ? dp_curr_min : dp_min);
}
dp[i] = dp_min;
}
cout << dp[N - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int K;
cin >> K;
vector<int> h(N);
for (int i = 0; i < N; i++) {
cin >> h[i];
}
vector<int> dp(N);
dp[0] = h[0];
dp[1] = abs(h[0] - h[1]);
for (int i = 2; i <= min(K, N - 1); i++) {
int this_min = INT_MAX;
for (int j = i - 1; j >= 1; j--) {
int curr_min = dp[j] + abs(h[j] - h[i]);
this_min = (curr_min < this_min ? curr_min : this_min);
}
dp[i] = min(abs(h[0] - h[i]), this_min);
}
for (int i = K + 1; i < N; i++) {
int dp_min = INT_MAX;
for (int j = i - 1; j >= max((i - 1 - (K - 1)), 1); j--) {
int dp_curr_min = dp[j] + abs(h[j] - h[i]);
dp_min = (dp_curr_min < dp_min ? dp_curr_min : dp_min);
}
dp[i] = dp_min;
}
cout << dp[N - 1] << endl;
}
|
replace
| 18 | 19 | 18 | 19 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;
/*---------------------------------------------------*/
int main() {
int N, K;
int h[100005];
cin >> N >> K;
rep(i, N) cin >> h[i];
ll cost[100005];
for (int i = 0; i < 100005; i++)
cost[i] = INT_MAX;
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 0; i < N; i++) {
for (int k = 1; k <= K; k++) {
cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
}
}
cout << cost[N - 1] << endl;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;
/*---------------------------------------------------*/
int main() {
int N, K;
int h[100005];
cin >> N >> K;
rep(i, N) cin >> h[i];
ll cost[100005];
for (int i = 0; i < 100005; i++)
cost[i] = INT_MAX;
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 0; i < N; i++) {
for (int k = 1; k <= min(N - i - 1, K); k++) {
cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
}
}
cout << cost[N - 1] << endl;
}
|
replace
| 62 | 63 | 62 | 63 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;
/*---------------------------------------------------*/
int main() {
int N, K;
int h[100005];
cin >> N >> K;
rep(i, N) cin >> h[i];
ll cost[100005];
for (int i = 0; i < 100005; i++)
cost[i] = INT_MAX;
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 0; i < N; i++) {
for (int k = 1; k <= min(N, K); k++) {
cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
}
}
cout << cost[N - 1] << endl;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (int i = (a); i < int(b); i++)
#define all(x) (x).begin(), x.end()
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef pair<int, int> pi;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int ddx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool debug = false;
/*---------------------------------------------------*/
int main() {
int N, K;
int h[100005];
cin >> N >> K;
rep(i, N) cin >> h[i];
ll cost[100005];
for (int i = 0; i < 100005; i++)
cost[i] = INT_MAX;
cost[0] = 0;
cost[1] = abs(h[0] - h[1]);
for (int i = 0; i < N; i++) {
for (int k = 1; k <= min(N - i - 1, K); k++) {
cost[i + k] = min(cost[i + k], cost[i] + abs(h[i] - h[i + k]));
}
}
cout << cost[N - 1] << endl;
}
|
replace
| 62 | 63 | 62 | 63 |
0
| |
p03161
|
C++
|
Runtime Error
|
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;
/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0; i < v.size() - 1; ++i)
os << v[i] << ", ";
os << v[v.size() - 1] << "]\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{";
for (auto it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
os << "{";
for (auto it : v) {
os << "(" << it.first << " : " << it.second << ")";
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << ", " << v.second << ")";
return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
fio;
int tcases = 1;
// cin>>testcases;
for (int tcase = 1; tcase <= tcases; ++tcase) {
int n, k;
cin >> n >> k;
vector<int> a(n, mod);
FOR(i, 0, n) cin >> a[i];
vector<int> dp(n, mod);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < min(i + k + 1, n); ++j) {
dp[j] = min(dp[j], abs(a[j] - a[i]) + dp[i]);
}
}
cout << dp[n - 1] << endl;
// cout<<"Case #"<<tcase<<": ";
}
return 0;
}
// Functions
int fpow(int x, int y, int p) {
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
|
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
#define int long long int
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;
/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0; i < v.size() - 1; ++i)
os << v[i] << ", ";
os << v[v.size() - 1] << "]\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{";
for (auto it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
os << "{";
for (auto it : v) {
os << "(" << it.first << " : " << it.second << ")";
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << ", " << v.second << ")";
return os;
}
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);
int32_t main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt" , "r" , stdin);freopen("output.txt" , "w" ,
// stdout);freopen("error.txt" , "w" , stderr);
#endif
fio;
int tcases = 1;
// cin>>testcases;
for (int tcase = 1; tcase <= tcases; ++tcase) {
int n, k;
cin >> n >> k;
vector<int> a(n, mod);
FOR(i, 0, n) cin >> a[i];
vector<int> dp(n, mod);
dp[0] = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < min(i + k + 1, n); ++j) {
dp[j] = min(dp[j], abs(a[j] - a[i]) + dp[i]);
}
}
cout << dp[n - 1] << endl;
// cout<<"Case #"<<tcase<<": ";
}
return 0;
}
// Functions
int fpow(int x, int y, int p) {
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
|
replace
| 62 | 65 | 62 | 64 |
-6
| |
p03161
|
C++
|
Runtime Error
|
#include <iostream>
// #include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int N, K;
vector<int> Distances;
vector<int> Heights;
cin >> N >> K;
Distances.reserve(N);
Heights.reserve(N);
int i, j;
int min = 2147483647;
int dist;
for (i = 0; i < N; i++) {
cin >> Heights[i];
if (i == 0) {
Distances[i] = 0;
} else {
for (j = i - K; j < i; j++) {
dist = Distances[j] + abs(Heights[i] - Heights[j]);
if (dist < min) {
min = dist;
}
}
Distances[i] = min;
min = 2147483647;
}
}
cout << Distances[N - 1];
// for(i=0;i<N;i++)
return 0;
}
|
#include <iostream>
// #include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int N, K;
vector<int> Distances;
vector<int> Heights;
cin >> N >> K;
Distances.reserve(N);
Heights.reserve(N);
int i, j;
int min = 2147483647;
int dist;
for (i = 0; i < N; i++) {
cin >> Heights[i];
if (i == 0) {
Distances[i] = 0;
} else if (i < K) {
for (j = 0; j < i; j++) {
dist = Distances[j] + abs(Heights[i] - Heights[j]);
if (dist < min) {
min = dist;
}
}
Distances[i] = min;
min = 2147483647;
} else {
for (j = i - K; j < i; j++) {
dist = Distances[j] + abs(Heights[i] - Heights[j]);
if (dist < min) {
min = dist;
}
}
Distances[i] = min;
min = 2147483647;
}
}
cout << Distances[N - 1];
// for(i=0;i<N;i++)
return 0;
}
|
insert
| 20 | 20 | 20 | 29 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main() {
lli n, k;
cin >> n >> k;
lli arr[n];
for (lli i = 0; i < n; i++)
cin >> arr[i];
vector<lli> dp(n, INT_MAX);
dp[0] = 0;
dp[1] = abs(arr[0] - arr[1]);
for (lli i = 2; i < n; i++) {
for (lli j = 1; j <= min(n, k); j++)
dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
}
cout << dp[n - 1];
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main() {
lli n, k;
cin >> n >> k;
lli arr[n];
for (lli i = 0; i < n; i++)
cin >> arr[i];
vector<lli> dp(n, INT_MAX);
dp[0] = 0;
dp[1] = abs(arr[0] - arr[1]);
for (lli i = 2; i < n; i++) {
for (lli j = 1; j <= min(i, k); j++)
dp[i] = min(dp[i - j] + abs(arr[i] - arr[i - j]), dp[i]);
}
cout << dp[n - 1];
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
const long long INF = 1ll << 60;
int n, k;
cin >> n >> k;
long long h[100010];
for (int i = 0; i < n; i++)
cin >> h[i];
long long dp[100010];
for (int i = 0; i < 100010; i++)
dp[i] = INF;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
}
cout << dp[n - 1] << endl;
}
|
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main() {
const long long INF = 1ll << 60;
int n, k;
cin >> n >> k;
long long h[100010];
for (int i = 0; i < n; i++)
cin >> h[i];
long long dp[100010];
for (int i = 0; i < 100010; i++)
dp[i] = INF;
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= k; j++) {
if (i + j < n)
dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
}
cout << dp[n - 1] << endl;
}
|
replace
| 22 | 23 | 22 | 24 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> dis(n);
for (int i = 0; i < n; i++) {
cin >> dis[i];
// cout<<dis[i]<<" ";
} // cout<<"\n";
vector<int> costs(n, INT_MAX);
costs[0] = 0;
for (int i = 1; i < n; i++) {
int count = k, j = i - 1;
while (count-- && j >= 0) {
int cost = costs[j] + abs(dis[i] - dis[j]);
// cout<<cost<<"\n";
if (cost < costs[i])
costs[i] = cost;
j--;
}
}
cout << costs[n - 1];
return 1;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<int> dis(n);
for (int i = 0; i < n; i++) {
cin >> dis[i];
// cout<<dis[i]<<" ";
} // cout<<"\n";
vector<int> costs(n, INT_MAX);
costs[0] = 0;
for (int i = 1; i < n; i++) {
int count = k, j = i - 1;
while (count-- && j >= 0) {
int cost = costs[j] + abs(dis[i] - dis[j]);
// cout<<cost<<"\n";
if (cost < costs[i])
costs[i] = cost;
j--;
}
}
cout << costs[n - 1];
return 0;
}
|
replace
| 24 | 25 | 24 | 25 |
1
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int min_cost(int *arr, int n, int k) {
int *dp = new int[n + 1];
dp[0] = 0;
dp[1] = 0;
dp[2] = abs(arr[1] - arr[2]);
for (int i = 3; i <= n; ++i) {
int temp = INT_MAX;
for (int j = 1; j <= k; ++j) {
temp = min(temp, abs(arr[i] - arr[i - j]) + dp[i - j]);
}
dp[i] = temp;
}
int ans = dp[n];
delete[] dp;
return ans;
}
int main() {
int n, k;
cin >> n >> k;
int arr[n + 1];
arr[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
}
cout << min_cost(arr, n, k);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int min_cost(int *arr, int n, int k) {
int *dp = new int[n + 1];
dp[0] = 0;
dp[1] = 0;
dp[2] = abs(arr[1] - arr[2]);
for (int i = 3; i <= n; ++i) {
int temp = INT_MAX;
for (int j = 1; j <= k && (i - j) > 0; ++j) {
temp = min(temp, abs(arr[i] - arr[i - j]) + dp[i - j]);
}
dp[i] = temp;
}
int ans = dp[n];
delete[] dp;
return ans;
}
int main() {
int n, k;
cin >> n >> k;
int arr[n + 1];
arr[0] = 0;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
}
cout << min_cost(arr, n, k);
return 0;
}
|
replace
| 13 | 14 | 13 | 14 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define f0r(a, b) for (long long a = 0; a < (b); ++a)
#define f1r(a, b, c) for (long long a = (b); a < (c); ++a)
#define f0rd(a, b) for (long long a = (b); a >= 0; --a)
#define f1rd(a, b, c) for (long long a = (b); a >= (c); --a)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define pb push_back
#define send \
{ ios_base::sync_with_stdio(false); }
#define help \
{ \
cin.tie(NULL); \
cout.tie(NULL); \
}
#define fix(prec) \
{ cout << setprecision(prec) << fixed; }
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define getunique(v) \
{ \
sort(all(v)); \
v.erase(unique(all(v)), v.end()); \
}
#define readgraph(list, edges) \
for (int i = 0; i < edges; i++) { \
int a, b; \
cin >> a >> b; \
a--; \
b--; \
list[a].pb(b); \
list[b].pb(a); \
}
#define ai(a, n) \
for (int ele = 0; ele < n; ele++) \
cin >> a[ele];
#define ain(a, lb, rb) \
for (int ele = lb; ele <= rb; ele++) \
cin >> a[ele];
#define ao(a, n) \
{ \
for (int ele = 0; ele < (n); ele++) { \
if (ele) \
cout << " "; \
cout << a[ele]; \
} \
cout << '\n'; \
}
#define aout(a, lb, rb) \
{ \
for (int ele = (lb); ele <= (rb); ele++) { \
if (ele > (lb)) \
cout << " "; \
cout << a[ele]; \
} \
cout << '\n'; \
}
#define vsz(x) ((long long)x.size())
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.f << ", " << p.s << ")";
}
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i)
cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(steady_clock::now().time_since_epoch().count());
// mt19937 rng(61378913);
/* usage - just do rng() */
void usaco(string filename) {
// #pragma message("be careful, freopen may be wrong")
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const lld pi = 3.14159265358979323846;
// const ll mod = 1000000007;
const ll mod = 998244353;
// ll mod;
ll n, m, k, q, l, r, x, y, z;
ll a[1000005];
ll b[1000005];
ll c[1000005];
string s, t;
ll ans = 0;
void solve(int tc) {
cin >> n >> k;
ai(a, n);
ll dp[n];
f0r(i, n) dp[i] = 1e15;
dp[0] = 0;
f0r(i, n) {
f1r(j, i + 1, i + k + 1) { dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j])); }
}
cout << dp[n - 1] << endl;
}
int main() {
#ifdef galen_colin_local
auto begin = std::chrono::high_resolution_clock::now();
#endif
send help
// usaco("cowland");
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++)
solve(t);
#ifdef galen_colin_local
auto end = std::chrono::high_resolution_clock::now();
cout << setprecision(4) << fixed;
// cout << "Execution time: " <<
// std::chrono::duration_cast<std::chrono::duration<double>>(end -
// begin).count() << " seconds" << endl;
#endif
}
|
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace std::chrono;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
// #pragma optimization_level 3
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define f0r(a, b) for (long long a = 0; a < (b); ++a)
#define f1r(a, b, c) for (long long a = (b); a < (c); ++a)
#define f0rd(a, b) for (long long a = (b); a >= 0; --a)
#define f1rd(a, b, c) for (long long a = (b); a >= (c); --a)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define pb push_back
#define send \
{ ios_base::sync_with_stdio(false); }
#define help \
{ \
cin.tie(NULL); \
cout.tie(NULL); \
}
#define fix(prec) \
{ cout << setprecision(prec) << fixed; }
#define mp make_pair
#define f first
#define s second
#define all(v) v.begin(), v.end()
#define getunique(v) \
{ \
sort(all(v)); \
v.erase(unique(all(v)), v.end()); \
}
#define readgraph(list, edges) \
for (int i = 0; i < edges; i++) { \
int a, b; \
cin >> a >> b; \
a--; \
b--; \
list[a].pb(b); \
list[b].pb(a); \
}
#define ai(a, n) \
for (int ele = 0; ele < n; ele++) \
cin >> a[ele];
#define ain(a, lb, rb) \
for (int ele = lb; ele <= rb; ele++) \
cin >> a[ele];
#define ao(a, n) \
{ \
for (int ele = 0; ele < (n); ele++) { \
if (ele) \
cout << " "; \
cout << a[ele]; \
} \
cout << '\n'; \
}
#define aout(a, lb, rb) \
{ \
for (int ele = (lb); ele <= (rb); ele++) { \
if (ele > (lb)) \
cout << " "; \
cout << a[ele]; \
} \
cout << '\n'; \
}
#define vsz(x) ((long long)x.size())
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
return cout << "(" << p.f << ", " << p.s << ")";
}
template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) {
cout << "[";
for (int i = 0; i < v.size(); i++) {
if (i)
cout << ", ";
cout << v[i];
}
return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
cin >> p.first;
return cin >> p.second;
}
mt19937 rng(steady_clock::now().time_since_epoch().count());
// mt19937 rng(61378913);
/* usage - just do rng() */
void usaco(string filename) {
// #pragma message("be careful, freopen may be wrong")
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
const lld pi = 3.14159265358979323846;
// const ll mod = 1000000007;
const ll mod = 998244353;
// ll mod;
ll n, m, k, q, l, r, x, y, z;
ll a[1000005];
ll b[1000005];
ll c[1000005];
string s, t;
ll ans = 0;
void solve(int tc) {
cin >> n >> k;
ai(a, n);
ll dp[n];
f0r(i, n) dp[i] = 1e15;
dp[0] = 0;
f0r(i, n) {
f1r(j, i + 1, i + k + 1) if (j < n) {
dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));
}
}
cout << dp[n - 1] << endl;
}
int main() {
#ifdef galen_colin_local
auto begin = std::chrono::high_resolution_clock::now();
#endif
send help
// usaco("cowland");
int tc = 1;
// cin >> tc;
for (int t = 0; t < tc; t++)
solve(t);
#ifdef galen_colin_local
auto end = std::chrono::high_resolution_clock::now();
cout << setprecision(4) << fixed;
// cout << "Execution time: " <<
// std::chrono::duration_cast<std::chrono::duration<double>>(end -
// begin).count() << " seconds" << endl;
#endif
}
|
replace
| 132 | 133 | 132 | 135 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
int N, K, dp[1000] = {};
cin >> N >> K;
vector<int> h(N);
for (int i = 0; i < N; i++)
cin >> h[i];
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < N; i++) {
dp[i] = 1000000000;
for (int j = max(i - K, 0); j < i; j++) {
dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i]));
}
}
cout << dp[N - 1];
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long int ll;
int main() {
int N, K, dp[10000000] = {};
cin >> N >> K;
vector<int> h(N);
for (int i = 0; i < N; i++)
cin >> h[i];
dp[1] = abs(h[1] - h[0]);
for (int i = 2; i < N; i++) {
dp[i] = 1000000000;
for (int j = max(i - K, 0); j < i; j++) {
dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i]));
}
}
cout << dp[N - 1];
}
|
replace
| 15 | 16 | 15 | 16 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int minimumcost(int n, int k, vector<int> &v) {
vector<int> dp(n, 0);
dp[0] = 0;
for (int i = 1; i <= k; i++) {
dp[i] = abs(v[i] - v[0]);
}
for (int i = k + 1; i < n; i++) {
dp[i] = dp[i - k] + abs(v[i] - v[i - k]);
for (int j = i - 1; j >= i - k; j--) {
dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j]));
}
}
return dp[n - 1];
}
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
cout << minimumcost(n, k, v);
}
|
#include <bits/stdc++.h>
using namespace std;
int minimumcost(int n, int k, vector<int> &v) {
if (k >= n) {
return v[n - 1] - v[0];
}
vector<int> dp(n, 0);
dp[0] = 0;
for (int i = 1; i <= k; i++) {
dp[i] = abs(v[i] - v[0]);
}
for (int i = k + 1; i < n; i++) {
dp[i] = dp[i - k] + abs(v[i] - v[i - k]);
for (int j = i - 1; j >= i - k; j--) {
dp[i] = min(dp[i], dp[j] + abs(v[i] - v[j]));
}
}
return dp[n - 1];
}
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
cout << minimumcost(n, k, v);
}
|
insert
| 4 | 4 | 4 | 7 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
// Begin Header {{{
using namespace std;
#ifndef DEBUG
#define dump(...)
#endif
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, n) for (intmax_t i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, b, e) \
for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) \
for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) \
Type __VA_ARGS__; \
input(__VA_ARGS__)
constexpr size_t operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };
constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;
namespace moke {
template <class T> using MaxHeap = priority_queue<T, vector<T>, less<T>>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
inline void input() {}
template <class Head, class... Tail>
inline void input(Head &&head, Tail &&...tail) {
cin >> head;
input(forward<Tail>(tail)...);
}
template <class T> inline void input(vector<T> &vec) {
for (auto &e : vec) {
cin >> e;
}
}
template <class T> inline void input(vector<vector<T>> &mat) {
for (auto &vec : mat) {
input(vec);
}
}
inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
static constexpr const char *delim[] = {"", " "};
cout << head << delim[sizeof...(tail)];
print(forward<Tail>(tail)...);
}
template <class T>
inline ostream &operator<<(ostream &strm, const vector<T> &vec) {
static constexpr const char *delim[] = {" ", ""};
for (const auto &e : vec) {
strm << e << delim[&e == &vec.back()];
}
return strm;
}
template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
return vector<decltype(make_v<T>(initValue, args...))>(
sz, make_v<T>(initValue, args...));
}
template <class A, class B> inline bool chmax(A &a, const B &b) noexcept {
return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) noexcept {
return b < a && (a = b, true);
}
template <class A, class B>
inline common_type_t<A, B> min(const A &a, const B &b) noexcept {
return a < b ? a : b;
}
template <class A, class B, class... Args>
inline common_type_t<A, B, Args...> min(const A &a, const B &b,
const Args &...args) noexcept {
return moke::min(a < b ? a : b, args...);
}
template <class A, class B>
inline common_type_t<A, B> max(const A &a, const B &b) noexcept {
return a > b ? a : b;
}
template <class A, class B, class... Args>
inline common_type_t<A, B, Args...> max(const A &a, const B &b,
const Args &...args) noexcept {
return moke::max(a > b ? a : b, args...);
}
template <class A, class B>
inline common_type_t<A, B> diff(const A &a, const B &b) noexcept {
return a < b ? b - a : a - b;
}
} // namespace moke
// }}} End Header
namespace moke {
size_t N, K;
vector<intmax_t> h;
vector<intmax_t> dp;
intmax_t rec(size_t idx) {
if (idx == 0)
return 0;
auto &ret = dp[idx];
if (~ret)
return ret;
ret = LINF;
reps(i, 1, K) {
if (idx - i >= 0) {
chmin(ret, rec(idx - i) + diff(h[idx], h[idx - i]));
}
}
return ret;
}
int main_() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input(N, K);
h.resize(N);
input(h);
dp.resize(N, -1);
print(rec(N - 1));
return 0;
}
} // namespace moke
signed main() { // {{{
moke::main_();
return 0;
} // }}}
|
#include "bits/stdc++.h"
#pragma GCC optimize("Ofast")
// Begin Header {{{
using namespace std;
#ifndef DEBUG
#define dump(...)
#endif
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rep(i, n) for (intmax_t i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, b, e) \
for (intmax_t i = (b), i##_limit = (e); i <= i##_limit; ++i)
#define repr(i, b, e) \
for (intmax_t i = (b), i##_limit = (e); i >= i##_limit; --i)
#define var(Type, ...) \
Type __VA_ARGS__; \
input(__VA_ARGS__)
constexpr size_t operator""_zu(unsigned long long value) { return value; };
constexpr intmax_t operator""_jd(unsigned long long value) { return value; };
constexpr uintmax_t operator""_ju(unsigned long long value) { return value; };
constexpr int INF = 0x3f3f3f3f;
constexpr intmax_t LINF = 0x3f3f3f3f3f3f3f3f_jd;
namespace moke {
template <class T> using MaxHeap = priority_queue<T, vector<T>, less<T>>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
inline void input() {}
template <class Head, class... Tail>
inline void input(Head &&head, Tail &&...tail) {
cin >> head;
input(forward<Tail>(tail)...);
}
template <class T> inline void input(vector<T> &vec) {
for (auto &e : vec) {
cin >> e;
}
}
template <class T> inline void input(vector<vector<T>> &mat) {
for (auto &vec : mat) {
input(vec);
}
}
inline void print() { cout << "\n"; }
template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
static constexpr const char *delim[] = {"", " "};
cout << head << delim[sizeof...(tail)];
print(forward<Tail>(tail)...);
}
template <class T>
inline ostream &operator<<(ostream &strm, const vector<T> &vec) {
static constexpr const char *delim[] = {" ", ""};
for (const auto &e : vec) {
strm << e << delim[&e == &vec.back()];
}
return strm;
}
template <class T> inline vector<T> make_v(const T &initValue, size_t sz) {
return vector<T>(sz, initValue);
}
template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
return vector<decltype(make_v<T>(initValue, args...))>(
sz, make_v<T>(initValue, args...));
}
template <class A, class B> inline bool chmax(A &a, const B &b) noexcept {
return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) noexcept {
return b < a && (a = b, true);
}
template <class A, class B>
inline common_type_t<A, B> min(const A &a, const B &b) noexcept {
return a < b ? a : b;
}
template <class A, class B, class... Args>
inline common_type_t<A, B, Args...> min(const A &a, const B &b,
const Args &...args) noexcept {
return moke::min(a < b ? a : b, args...);
}
template <class A, class B>
inline common_type_t<A, B> max(const A &a, const B &b) noexcept {
return a > b ? a : b;
}
template <class A, class B, class... Args>
inline common_type_t<A, B, Args...> max(const A &a, const B &b,
const Args &...args) noexcept {
return moke::max(a > b ? a : b, args...);
}
template <class A, class B>
inline common_type_t<A, B> diff(const A &a, const B &b) noexcept {
return a < b ? b - a : a - b;
}
} // namespace moke
// }}} End Header
namespace moke {
size_t N, K;
vector<intmax_t> h;
vector<intmax_t> dp;
intmax_t rec(int idx) {
if (idx == 0)
return 0;
auto &ret = dp[idx];
if (~ret)
return ret;
ret = LINF;
reps(i, 1, K) {
if (idx - i >= 0) {
chmin(ret, rec(idx - i) + diff(h[idx], h[idx - i]));
}
}
return ret;
}
int main_() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
input(N, K);
h.resize(N);
input(h);
dp.resize(N, -1);
print(rec(N - 1));
return 0;
}
} // namespace moke
signed main() { // {{{
moke::main_();
return 0;
} // }}}
|
replace
| 125 | 126 | 125 | 126 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, K;
cin >> n >> K;
vector<ll> v(n);
vector<ll> dp(n + 1, -1);
for (ll i = 0; i < n; i++) {
cin >> v[i];
}
dp[0] = 0;
for (ll i = 1; i < n; i++) {
ll add = LLONG_MAX;
for (ll k = 1; k <= K; k++) {
add = min(add, dp[i - k] + abs(v[i] - v[i - k]));
}
dp[i] = add;
}
cout << dp[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, K;
cin >> n >> K;
vector<ll> v(n);
vector<ll> dp(n + 1, -1);
for (ll i = 0; i < n; i++) {
cin >> v[i];
}
dp[0] = 0;
for (ll i = 1; i < n; i++) {
ll add = LLONG_MAX;
for (ll k = 1; k <= K; k++) {
if (i - k >= 0)
add = min(add, dp[i - k] + abs(v[i] - v[i - k]));
}
dp[i] = add;
}
cout << dp[n - 1] << endl;
return 0;
}
|
replace
| 16 | 17 | 16 | 18 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
vector<int> ht;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ht.push_back(x);
}
long long cost[100010] = {0};
for (int i = 2; i <= n; i++) {
cost[i] = pow(2, 30);
}
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= m; k++) {
cost[i + k] = min(cost[i + k], cost[i] + abs(ht[i + k - 1] - ht[i - 1]));
}
}
cout << cost[n] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m;
vector<int> ht;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
ht.push_back(x);
}
long long cost[100010] = {0};
for (int i = 2; i <= n; i++) {
cost[i] = pow(2, 30);
}
for (int i = 1; i <= n; i++) {
for (int k = 1; k <= m; k++) {
if (i + k - 1 < n)
cost[i + k] =
min(cost[i + k], cost[i] + abs(ht[i + k - 1] - ht[i - 1]));
}
}
cout << cost[n] << endl;
return 0;
}
|
replace
| 21 | 22 | 21 | 24 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int absdiff(int a, int b) { return (abs(a - b)); }
int main() {
int n, k;
cin >> n >> k;
vector<int> height;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
height.push_back(temp);
}
vector<int> cost(n, 0);
cost[1] = abs(height[1] - height[0]);
for (int i = 2; i < n; i++) {
int min_val = 1e9;
for (int j = 1; j <= k; j++) {
int temp = cost[i - j] + absdiff(height[i - j], height[i]);
min_val = min(min_val, temp);
}
cost[i] = min_val;
}
cout << cost[n - 1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int absdiff(int a, int b) { return (abs(a - b)); }
int main() {
int n, k;
cin >> n >> k;
vector<int> height;
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
height.push_back(temp);
}
vector<int> cost(n, 0);
cost[1] = abs(height[1] - height[0]);
for (int i = 2; i < n; i++) {
int min_val = 1e9;
for (int j = 1; j <= k and i - j >= 0; j++) {
int temp = cost[i - j] + absdiff(height[i - j], height[i]);
min_val = min(min_val, temp);
}
cost[i] = min_val;
}
cout << cost[n - 1];
return 0;
}
|
replace
| 19 | 20 | 19 | 20 |
0
| |
p03161
|
C++
|
Runtime Error
|
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace(...) \
cerr << "Line:" << __LINE__ << " "; \
__f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
int n, k;
int h[N];
int cache[N];
int dp(int idx) {
if (idx >= n)
return 0;
int &ans = cache[idx];
if (ans != -1)
return ans;
ans = inf;
for (int i = 1; i <= k; i++) {
ans = min(ans, dp(idx + i) + abs(h[idx + i] - h[idx]));
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
rep(i, 1, n + 1) cin >> h[i];
mst(cache, -1);
int res = dp(1);
cout << res;
return 0;
}
|
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace(...) \
cerr << "Line:" << __LINE__ << " "; \
__f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
int n, k;
int h[N];
int cache[N];
int dp(int idx) {
if (idx >= n)
return 0;
int &ans = cache[idx];
if (ans != -1)
return ans;
ans = inf;
for (int i = 1; i <= k; i++) {
if (i + idx <= n)
ans = min(ans, dp(idx + i) + abs(h[idx + i] - h[idx]));
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
rep(i, 1, n + 1) cin >> h[i];
mst(cache, -1);
int res = dp(1);
cout << res;
return 0;
}
|
replace
| 85 | 86 | 85 | 87 |
0
| |
p03161
|
C++
|
Runtime Error
|
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
ll n, k;
ll h[N];
ll cache[N];
ll dp(ll idx) {
if (idx == n)
return 0;
if (idx > n)
return inf;
ll &ans = cache[idx];
if (ans != -1)
return ans;
ans = inf;
for (ll i = idx + 1; i <= idx + k; i++) {
ans = min(ans, abs(h[i] - h[idx]) + dp(i));
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
rep(i, 1, n + 1) cin >> h[i];
mst(cache, -1);
ll res = dp(1);
cout << res;
return 0;
}
/* The judge is never wrong! Your code is buggy
Look for:
* * Read the problem carefully.
* * Don't forget to sort(), mod, ll!!!!
* * Initial value = +/- infinity instead of zero!!!
* * an easier and alternate approach
* * read the problem statement carefully
* * if concept is correct and still WA, try with a different implementation
* * special cases (n=1?)
* * if you have no idea just guess the appropriate well-known algorithm instead
of doing nothing :/
*/
|
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// 🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤🖤 //
// --------------------<optimizations>--------------------
#pragma GCC optimize("O3")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)\
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)\
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pii pair<ll, ll>
#define vpii vector<pair<ll, ll>>
#define F first
#define S second
#define ld long double
#define built __builtin_popcountll
#define mst(a, i) memset(a, i, sizeof(a))
#define all(x) x.begin(), x.end()
#define itit(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define reprr(i, a, b) for (ll i = a; i >= b; i--)
#define pi 3.14159265358979323846264338327950288419716939937510582097494459230
ll max3(ll x, ll y, ll z) { return max(max(x, y), z); }
ll min3(ll x, ll y, ll z) { return min(min(x, y), z); }
const ll N = 1e5 + 10, M = 2e5 + 10, M2 = 1e6 + 10, mod = 1e9 + 7,
inf = 1e17 + 10;
const int INF = 1e9 + 7;
void add(int &a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
int X[] = {0, 1, 0, -1};
int Y[] = {-1, 0, 1, 0};
ll power(ll x, ll n) {
ll result = 1;
while (n > 0) {
if (n % 2 == 1)
result = (result * x) % mod;
x = ((x % mod) * (x % mod)) % mod;
n = n / 2;
}
return result;
}
ll n, k;
ll h[N];
ll cache[N];
ll dp(ll idx) {
if (idx == n)
return 0;
if (idx > n)
return inf;
ll &ans = cache[idx];
if (ans != -1)
return ans;
ans = inf;
for (ll i = idx + 1; i <= min(idx + k, n); i++) {
ans = min(ans, abs(h[i] - h[idx]) + dp(i));
}
return ans;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
rep(i, 1, n + 1) cin >> h[i];
mst(cache, -1);
ll res = dp(1);
cout << res;
return 0;
}
/* The judge is never wrong! Your code is buggy
Look for:
* * Read the problem carefully.
* * Don't forget to sort(), mod, ll!!!!
* * Initial value = +/- infinity instead of zero!!!
* * an easier and alternate approach
* * read the problem statement carefully
* * if concept is correct and still WA, try with a different implementation
* * special cases (n=1?)
* * if you have no idea just guess the appropriate well-known algorithm instead
of doing nothing :/
*/
|
replace
| 104 | 105 | 104 | 105 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf 4e17
#define nllinf -4e18
#define vll vector<ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define repup(a, b, i) for (i = a; i <= b; i++)
#define repdown(a, b, i) for (i = b; i >= a; i--)
#define repupx(a, b, i, x) for (i = a; i <= b; i += x)
#define repit(v, it) for (it = v.begin(); it != v.end(); it++)
#define itall(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define err(n) cout << n << " ";
#define t1 cout << ",";
ll MOD = 998244353;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll i, j;
ll n, k;
cin >> n >> k;
ll a[n + 5];
repup(0, n - 1, i) cin >> a[i];
ll ans[n + 5];
ans[0] = 0;
repup(1, k - 1, i) {
ans[i] = abs(a[i] - a[0]);
repup(1, i - 1, j) { ans[i] = min(ans[i], ans[j] + abs(a[i] - a[j])); }
}
repup(k, n - 1, i) {
ans[i] = (ll)llinf;
repdown(1, k, j) {
ans[i] = min(ans[i], ans[i - j] + abs(a[i] - a[i - j]));
}
}
cout << ans[n - 1];
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define llinf 4e17
#define nllinf -4e18
#define vll vector<ll>
#define sll set<ll>
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define repup(a, b, i) for (i = a; i <= b; i++)
#define repdown(a, b, i) for (i = b; i >= a; i--)
#define repupx(a, b, i, x) for (i = a; i <= b; i += x)
#define repit(v, it) for (it = v.begin(); it != v.end(); it++)
#define itall(v) v.begin(), v.end()
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define err(n) cout << n << " ";
#define t1 cout << ",";
ll MOD = 998244353;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll i, j;
ll n, k;
cin >> n >> k;
ll a[n + 5];
repup(0, n - 1, i) cin >> a[i];
ll ans[n + 5];
ans[0] = 0;
repup(1, min(n - 1, k - 1), i) {
ans[i] = abs(a[i] - a[0]);
repup(1, i - 1, j) { ans[i] = min(ans[i], ans[j] + abs(a[i] - a[j])); }
}
repup(k, n - 1, i) {
ans[i] = (ll)llinf;
repdown(1, k, j) {
ans[i] = min(ans[i], ans[i - j] + abs(a[i] - a[i - j]));
}
}
cout << ans[n - 1];
}
|
replace
| 43 | 44 | 43 | 44 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll INF = 101010101010;
int main() {
int n, k;
cin >> n >> k;
vector<ll> h(n);
vector<ll> dp(n);
rep(i, n) cin >> h[i];
rep(i, n) dp[i] = INF;
dp[0] = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (j < i - k)
continue;
dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
}
/*
最高2こ飛ばしの場合
dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
if(i>=2)dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
*/
}
cout << dp[n - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll INF = 101010101010;
int main() {
int n, k;
cin >> n >> k;
vector<ll> h(n);
vector<ll> dp(n);
rep(i, n) cin >> h[i];
rep(i, n) dp[i] = INF;
dp[0] = 0;
for (int i = 1; i < n; i++) {
for (int j = i - k; j < i; j++) {
if (j < 0)
continue;
dp[i] = min(dp[i], abs(h[i] - h[j]) + dp[j]);
}
/*
最高2こ飛ばしの場合
dp[i]=dp[i-1]+abs(h[i]-h[i-1]);
if(i>=2)dp[i]=min(dp[i],dp[i-2]+abs(h[i]-h[i-2]));
*/
}
cout << dp[n - 1] << endl;
}
|
replace
| 17 | 19 | 17 | 19 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int ans(vector<int> &v, int n, int i, int k) {
if (i == 0)
return 0;
if (dp[i] != -1)
return dp[i];
int mn = INT_MAX;
for (int j = i - 1; j >= i - k; j--) {
int p = ans(v, n, j, k) + abs(v[j] - v[i]);
mn = min(p, mn);
}
return dp[i] = mn;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
for (int i = 0; i < 1000000; i++)
dp[i] = -1;
int a = ans(v, n, n - 1, k);
cout << a;
}
|
#include <bits/stdc++.h>
using namespace std;
int dp[1000000];
int ans(vector<int> &v, int n, int i, int k) {
if (i == 0)
return 0;
if (dp[i] != -1)
return dp[i];
int mn = INT_MAX;
for (int j = i - 1; j >= max(0, i - k); j--) {
int p = ans(v, n, j, k) + abs(v[j] - v[i]);
mn = min(p, mn);
}
return dp[i] = mn;
}
int main() {
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
for (int i = 0; i < 1000000; i++)
dp[i] = -1;
int a = ans(v, n, n - 1, k);
cout << a;
}
|
replace
| 9 | 10 | 9 | 10 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int h[N];
int dp[N];
for (int i = 0; i < N; i++) {
cin >> h[i];
dp[i] = INT_MAX;
}
dp[0] = 0;
for (int i = 0; i < N; i++) {
for (int k = 1; k <= K; k++) {
if (dp[i + k] > dp[i] + abs(h[i] - h[i + k])) {
dp[i + k] = dp[i] + abs(h[i] - h[i + k]);
}
}
}
cout << dp[N - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int h[N + K];
int dp[N + K];
for (int i = 0; i < N; i++) {
cin >> h[i];
dp[i] = INT_MAX;
}
dp[0] = 0;
for (int i = 0; i < N; i++) {
for (int k = 1; k <= K; k++) {
if (dp[i + k] > dp[i] + abs(h[i] - h[i + k])) {
dp[i + k] = dp[i] + abs(h[i] - h[i + k]);
}
}
}
cout << dp[N - 1] << endl;
}
|
replace
| 7 | 9 | 7 | 9 |
0
| |
p03161
|
C++
|
Runtime Error
|
/*
* Problem URL: https://atcoder.jp/contests/dp/tasks/dp_b
*/
#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
using namespace std;
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(w) (int)w.size()
#define FASTER \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
typedef vector<int> vi;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
int main() {
int n, k;
cin >> n >> k;
vi arr(n + 1);
FOR(i, 1, n + 1) cin >> arr[i];
// cout<<getAns(1);
int dp[n + 1];
FOR(i, 0, n + 1) dp[i] = INT_MAX;
FOR(i, 1, k + 1) dp[i] = arr[i];
// FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
FOR(i, 1, n + 1) {
FOR(j, 1, k + 1) {
if (i - j > 0)
dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
}
}
// FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
cout << dp[n] << endl;
return 0;
}
|
/*
* Problem URL: https://atcoder.jp/contests/dp/tasks/dp_b
*/
#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
using namespace std;
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define sz(w) (int)w.size()
#define FASTER \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
typedef vector<int> vi;
typedef long long int lli;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
int main() {
int n, k;
cin >> n >> k;
vi arr(n + 1);
FOR(i, 1, n + 1) cin >> arr[i];
// cout<<getAns(1);
int dp[n + 1];
FOR(i, 1, n + 1) dp[i] = INT_MAX;
dp[0] = dp[1] = 0;
FOR(i, 1, n + 1) {
FOR(j, 1, k + 1) {
if (i - j > 0)
dp[i] = min(dp[i], dp[i - j] + abs(arr[i] - arr[i - j]));
}
}
// FOR(i, 1, n+1)cout<<dp[i]<<" ";cout<<endl;
cout << dp[n] << endl;
return 0;
}
|
replace
| 35 | 38 | 35 | 37 |
0
| |
p03161
|
C++
|
Runtime Error
|
// Resting_Prince
#include <bits/stdc++.h>
#define mod 1000000007
#define inf 1e18
#define PI 3.141592653589
#define fi first
#define se second
#define sv(v) \
sort(all(v)); \
reverse(all(v))
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define en '\n'
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double ld;
int main() {
ll n, h[100005], i, j, k, l, dp[100005];
cin >> n >> k;
rep(i, 0, n - 1) cin >> h[i + 1], dp[i + 1] = inf;
dp[0] = 0;
dp[1] = 0;
rep(i, 1, n) {
rep(j, 1, k) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
cout << dp[n];
return 0;
}
|
// Resting_Prince
#include <bits/stdc++.h>
#define mod 1000000007
#define inf 1e18
#define PI 3.141592653589
#define fi first
#define se second
#define sv(v) \
sort(all(v)); \
reverse(all(v))
#define all(v) v.begin(), v.end()
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define mk make_pair
#define en '\n'
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double ld;
int main() {
ll n, h[200005], i, j, k, l, dp[200005];
cin >> n >> k;
rep(i, 0, n - 1) cin >> h[i + 1], dp[i + 1] = inf;
dp[0] = 0;
dp[1] = 0;
rep(i, 1, n) {
rep(j, 1, k) dp[i + j] = min(dp[i + j], dp[i] + abs(h[i + j] - h[i]));
}
cout << dp[n];
return 0;
}
|
replace
| 26 | 27 | 26 | 27 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define pb push_back
ll dp[100001];
int solve(int i, int *h, int n, int k) {
if (i == n) {
dp[n] = 0;
return dp[n];
}
ll ans = 1000000009;
for (int j = 1; j <= k and i + j <= n; ++j) {
if (dp[i + j] == -1) {
dp[i + j] = solve(i + j, h, n, k);
}
ans = min(ans, abs(h[i] - h[i + j]) + dp[i + j]);
}
dp[i] = ans;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, k;
cin >> n >> k;
int h[n];
for (int i = 1; i < n + 1; ++i)
cin >> h[i];
for (int i = 0; i < 100001; ++i)
dp[i] = -1;
cout << solve(1, h, n, k);
return 0;
}
|
#include <algorithm>
#include <climits>
#include <cstring>
#include <fstream>
#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
using namespace std;
#define ll long long
#define w(x) \
int x; \
cin >> x; \
while (x--)
#define pb push_back
ll dp[100001];
int solve(int i, int *h, int n, int k) {
if (i == n) {
dp[n] = 0;
return dp[n];
}
ll ans = 1000000009;
for (int j = 1; j <= k and i + j <= n; ++j) {
if (dp[i + j] == -1) {
dp[i + j] = solve(i + j, h, n, k);
}
ans = min(ans, abs(h[i] - h[i + j]) + dp[i + j]);
}
dp[i] = ans;
return ans;
}
int main() {
int n, k;
cin >> n >> k;
int h[n];
for (int i = 1; i < n + 1; ++i)
cin >> h[i];
for (int i = 0; i < 100001; ++i)
dp[i] = -1;
cout << solve(1, h, n, k);
return 0;
}
|
delete
| 39 | 47 | 39 | 39 |
-11
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> dp(n, INF);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= i + k; j++) {
if (j <= n) {
dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
}
}
}
cout << dp[n - 1];
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
int main() {
int n;
cin >> n;
int k;
cin >> k;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
vector<int> dp(n, INF);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= i + k; j++) {
if (j < n) {
dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
}
}
}
cout << dp[n - 1];
}
|
replace
| 16 | 17 | 16 | 17 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define Jrep(j, n) for (int j = 0; j < (n); j++)
#define Jreps(j, n) for (int j = 1; j <= (n); j++)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
vector<int> dp(100010, INF);
cin >> n >> k;
int h[100010];
reps(i, n) { cin >> h[i]; }
dp[1] = 0;
dp[2] = abs(h[1] - h[2]);
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= k; j++) {
dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j], dp[i]);
}
}
cout << dp[n] << '\n';
return 0;
}
|
#include <algorithm>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define Jrep(j, n) for (int j = 0; j < (n); j++)
#define Jreps(j, n) for (int j = 1; j <= (n); j++)
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k;
vector<int> dp(100010, INF);
cin >> n >> k;
int h[100010];
reps(i, n) { cin >> h[i]; }
dp[1] = 0;
dp[2] = abs(h[1] - h[2]);
for (int i = 3; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (i - j == 0)
break;
dp[i] = min(abs(h[i] - h[i - j]) + dp[i - j], dp[i]);
}
}
cout << dp[n] << '\n';
return 0;
}
|
insert
| 33 | 33 | 33 | 35 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
#define ll long long int
using namespace std;
int main() {
int n, k;
int h[10005];
int dp[10005] = {};
cin >> n >> k;
REP(i, n) { cin >> h[i]; }
REP(i, n) {
int result = 1110001110;
FOR(j, 1, k + 1) {
if (i <= k) {
result = abs(h[i] - h[0]);
break;
}
result = min(result, dp[i - j] + abs(h[i] - h[i - j]));
}
dp[i] = result;
}
cout << dp[n - 1] << endl;
}
|
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
#define ll long long int
using namespace std;
int main() {
int n, k;
int h[100000];
int dp[100000] = {};
cin >> n >> k;
REP(i, n) { cin >> h[i]; }
REP(i, n) {
int result = 1110001110;
FOR(j, 1, k + 1) {
if (i <= k) {
result = abs(h[i] - h[0]);
break;
}
result = min(result, dp[i - j] + abs(h[i] - h[i - j]));
}
dp[i] = result;
}
cout << dp[n - 1] << endl;
}
|
replace
| 10 | 12 | 10 | 12 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define pint pair<int, int>
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
flag = 0;
string S, T, O;
cin >> N >> M;
Vec(P, lli, N, 0);
Vec(DP, lli, 10000, INF);
DP[0] = 0;
rep(i, 0, N) cin >> P[i];
rep(i, 0, N) {
rep(j, 1, M + 1) {
if (i + j > N - 1)
continue;
chmin(DP[i + j], DP[i] + abs(P[i + j] - P[i]));
}
}
Out(DP[N - 1]);
}
|
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 99999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(20) << S << endl
#define Vecpr(K, L1, L2, N) vector<pair<L1, L2>> K(N)
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, R) vector<vector<L>> K(N, vector<L>(M, R))
#define pint pair<int, int>
#define Lower(v, X) lower_bound(v.begin(), v.end(), X) - v.begin();
#define mod 1000000007
#define MAX 5100000
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int main() {
lli A, B, C, L, R, N, M, K, X = 0, Y = 0, W, H = INF, sum = 0, num = 0,
flag = 0;
string S, T, O;
cin >> N >> M;
Vec(P, lli, N, 0);
Vec(DP, lli, 10000000, INF);
DP[0] = 0;
rep(i, 0, N) cin >> P[i];
rep(i, 0, N) {
rep(j, 1, M + 1) {
if (i + j > N - 1)
continue;
chmin(DP[i + j], DP[i] + abs(P[i + j] - P[i]));
}
}
Out(DP[N - 1]);
}
|
replace
| 31 | 32 | 31 | 32 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
const ll INF = 10e9;
int solve(int n, int k, vector<int> h) {
vector<int> dp(n, INF);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i + k; j++) {
if (j < n) {
dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
}
}
}
return dp[n - 1];
}
int main() {
int n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) cin >> h[i];
cout << solve(n, k, h) << endl;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
const ll INF = 10e9;
int solve(int n, int k, vector<int> h) {
vector<int> dp(n, INF);
dp[0] = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j <= i + k; j++) {
if (j < n) {
dp[j] = min(dp[j], dp[i] + abs(h[i] - h[j]));
}
}
}
return dp[n - 1];
}
int main() {
int n, k;
cin >> n >> k;
vector<int> h(n);
rep(i, n) cin >> h[i];
cout << solve(n, k, h) << endl;
}
|
replace
| 10 | 11 | 10 | 11 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out) \
stdi(in); \
stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
// typedef int ittt;
// #define int ll
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
const ll inf = 1e18;
ll hit[MAX];
ll dp[MAX];
int n, k;
ll solve(int i) {
if (i <= 1) {
return 0;
}
if (dp[i] != -1)
return dp[i];
ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);
for (int j = 2; j <= k; j++) {
if (i - j > 0) {
ans = min(ans, solve(i - j) + abs(hit[i] - hit[i - j]));
}
}
return dp[i] = ans;
}
int main() {
#ifndef ONLINE_JUDGE
stdio("in", "out");
#endif
fast;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> hit[i];
dp[i] = -1;
}
ll ans = solve(n);
cout << ans << '\n';
return 0;
}
|
#include <algorithm>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define stdi(in) freopen(in, "r", stdin)
#define stdo(out) freopen(out, "w", stdout)
#define stdio(in, out) \
stdi(in); \
stdo(out)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define MAX 1000007
#define MOD 1000000007
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
// typedef int ittt;
// #define int ll
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
const ll inf = 1e18;
ll hit[MAX];
ll dp[MAX];
int n, k;
ll solve(int i) {
if (i <= 1) {
return 0;
}
if (dp[i] != -1)
return dp[i];
ll ans = solve(i - 1) + abs(hit[i] - hit[i - 1]);
for (int j = 2; j <= k; j++) {
if (i - j > 0) {
ans = min(ans, solve(i - j) + abs(hit[i] - hit[i - j]));
}
}
return dp[i] = ans;
}
int main() {
/*#ifndef ONLINE_JUDGE
stdio("in", "out");
#endif*/
fast;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> hit[i];
dp[i] = -1;
}
ll ans = solve(n);
cout << ans << '\n';
return 0;
}
|
replace
| 55 | 58 | 55 | 58 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
long long dp[10000];
int main(void) {
int N, K;
cin >> N >> K;
long long h[N + 1];
for (int i = 1; i <= N; i++)
cin >> h[i];
// 現在地から+1 +2 ..+Kまで移動することが出来る
// dp[x]はxまでいくのにかかる最小のコスト
// 初期コストのセット
for (int i = 1; i <= K + 1; i++) {
dp[i] = abs(h[1] - h[i]);
}
// K+1~を計算して行くンゴ
for (int i = K + 1; i <= N; i++) {
long long temp1 = dp[i - 1] + abs(h[i - 1] - h[i]);
long long temp2;
for (int j = 1; j <= K; j++) {
temp2 = dp[i - j] + abs(h[i - j] - h[i]);
if (temp1 > temp2)
temp1 = temp2;
}
dp[i] = temp1;
}
cout << dp[N];
}
|
#include <bits/stdc++.h>
using namespace std;
long long dp[100001];
int main(void) {
int N, K;
cin >> N >> K;
long long h[N + 1];
for (int i = 1; i <= N; i++)
cin >> h[i];
// 現在地から+1 +2 ..+Kまで移動することが出来る
// dp[x]はxまでいくのにかかる最小のコスト
// 初期コストのセット
for (int i = 1; i <= K + 1; i++) {
dp[i] = abs(h[1] - h[i]);
}
// K+1~を計算して行くンゴ
for (int i = K + 1; i <= N; i++) {
long long temp1 = dp[i - 1] + abs(h[i - 1] - h[i]);
long long temp2;
for (int j = 1; j <= K; j++) {
temp2 = dp[i - j] + abs(h[i - j] - h[i]);
if (temp1 > temp2)
temp1 = temp2;
}
dp[i] = temp1;
}
cout << dp[N];
}
|
replace
| 2 | 3 | 2 | 3 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using VI = vector<ll>;
using VD = vector<ld>;
using VVI = vector<VI>;
using VC = vector<char>;
using VVC = vector<VC>;
using VS = vector<string>;
using PLL = pair<ll, ll>;
using PLD = pair<ld, ld>;
using VPLL = vector<PLL>;
#define print(x) std::cout << x << "\n"
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define SZ(x) ((ll)(x).size())
#define MAX(x) *max_element((x).begin(), (x).end())
#define MIN(x) *min_element((x).begin(), (x).end())
#define SORTR(x) sort((x).rbegin(), (x).rend())
#define SORT(x) sort((x).begin(), (x).end())
#define SUM(x) accumulate((x).begin(), (x).end(), 0)
#define FILL(x, a) fill(x.begin(), x.end(), a)
#define EACH(i, x) \
for (typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
const ll INF = 1e18;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class BidirectionalIterator>
bool next_partial_permutation(BidirectionalIterator first,
BidirectionalIterator middle,
BidirectionalIterator last) {
reverse(middle, last);
return next_permutation(first, last);
}
ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll GCD(VI v) {
ll a = v[0];
for (ll i = 1; i < SZ(v); i++) {
a = gcd(a, v[i]);
}
return a;
}
ll LCM(VI v) {
ll a = v[0];
for (ll i = 1; i < SZ(v); i++) {
a = lcm(a, v[i]);
}
return a;
}
VI Bit2Vector(const ll bit, ll n) {
VI s;
rep(i, n) if (bit & (1 << i)) s.push_back(i);
return s;
}
void Main() {
ll n, m, l;
ll res = 0;
ll k;
cin >> n >> k;
VI h(n), b(n);
rep(i, n) cin >> h[i];
// VVI dp(n*2,VI(INF,0));
VI dp(n * 2, INF);
// rep(i,n) dp[i]=0;
dp[0] = 0;
rep(i, n) {
rep(w, k + 1) {
ll cost = abs(h[i + w] - h[i]);
dp[i + w] = min(dp[i + w], dp[i] + cost);
}
}
res = dp[n - 1];
cout << res << "\n";
return;
}
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
using VI = vector<ll>;
using VD = vector<ld>;
using VVI = vector<VI>;
using VC = vector<char>;
using VVC = vector<VC>;
using VS = vector<string>;
using PLL = pair<ll, ll>;
using PLD = pair<ld, ld>;
using VPLL = vector<PLL>;
#define print(x) std::cout << x << "\n"
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define SZ(x) ((ll)(x).size())
#define MAX(x) *max_element((x).begin(), (x).end())
#define MIN(x) *min_element((x).begin(), (x).end())
#define SORTR(x) sort((x).rbegin(), (x).rend())
#define SORT(x) sort((x).begin(), (x).end())
#define SUM(x) accumulate((x).begin(), (x).end(), 0)
#define FILL(x, a) fill(x.begin(), x.end(), a)
#define EACH(i, x) \
for (typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
const ll INF = 1e18;
const ld EPS = 1e-10;
const int MOD = int(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class BidirectionalIterator>
bool next_partial_permutation(BidirectionalIterator first,
BidirectionalIterator middle,
BidirectionalIterator last) {
reverse(middle, last);
return next_permutation(first, last);
}
ll gcd(ll x, ll y) { return (x % y) ? gcd(y, x % y) : y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll GCD(VI v) {
ll a = v[0];
for (ll i = 1; i < SZ(v); i++) {
a = gcd(a, v[i]);
}
return a;
}
ll LCM(VI v) {
ll a = v[0];
for (ll i = 1; i < SZ(v); i++) {
a = lcm(a, v[i]);
}
return a;
}
VI Bit2Vector(const ll bit, ll n) {
VI s;
rep(i, n) if (bit & (1 << i)) s.push_back(i);
return s;
}
void Main() {
ll n, m, l;
ll res = 0;
ll k;
cin >> n >> k;
VI h(n), b(n);
rep(i, n) cin >> h[i];
// VVI dp(n*2,VI(INF,0));
VI dp(1000010, INF);
// rep(i,n) dp[i]=0;
dp[0] = 0;
rep(i, n) {
rep(w, k + 1) {
ll cost = abs(h[i + w] - h[i]);
dp[i + w] = min(dp[i + w], dp[i] + cost);
}
}
res = dp[n - 1];
cout << res << "\n";
return;
}
int main() {
std::cin.tie(0);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
Main();
}
|
replace
| 84 | 85 | 84 | 85 |
0
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long INF = 10000000000;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main() {
int N, K;
cin >> N >> K;
int h[100000 + 10];
for (int i = 0; i < N; i++)
cin >> h[i];
int dp[100000 + 10];
for (int i = 0; i < 100000 + 10; i++)
dp[i] = INF; // 初期化の際のiの範囲は配列の要素数!
dp[0] = 0; // 初期タッ君
for (int i = 0; i < N; i++) {
for (int j = 1; j <= K; j++) {
chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
}
}
cout << dp[N - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const long long INF = 10000000000;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main() {
int N, K;
cin >> N >> K;
int h[100000 + 10];
for (int i = 0; i < N; i++)
cin >> h[i];
int dp[100000 + 10];
for (int i = 0; i < 100000 + 10; i++)
dp[i] = INF; // 初期化の際のiの範囲は配列の要素数!
dp[0] = 0; // 初期タッ君
for (int i = 0; i < N; i++) {
for (int j = 1; j <= K; j++) {
chmin(dp[i + j], dp[i] + abs(h[i] - h[i + j]));
if (i + j >= N)
break;
}
}
cout << dp[N - 1] << endl;
return 0;
}
|
insert
| 32 | 32 | 32 | 34 |
0
| |
p03161
|
C++
|
Time Limit Exceeded
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 1000005
#define endl "\n"
using namespace std;
vector<ll> v;
ll dp[100003];
ll a, n, k;
ll rec(int i) {
if (i == n - 1)
return 0;
ll ans = LLONG_MAX;
for (int j = 1; j <= k && i + j < n; j++) {
ans = min(ans, abs(v[i] - v[i + j]) + rec(i + j));
}
return dp[i] = ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (ll i = 0; i < n; i++) {
cin >> a;
v.pb(a);
}
memset(dp, -1, sizeof(dp));
ll ans = rec(0);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define mp make_pair
#define ff first
#define ss second
#define MAXN 1000005
#define endl "\n"
using namespace std;
vector<ll> v;
ll dp[100003];
ll a, n, k;
ll rec(int i) {
if (i == n - 1)
return 0;
if (dp[i] != -1)
return dp[i];
ll ans = LLONG_MAX;
for (int j = 1; j <= k && i + j < n; j++) {
ans = min(ans, abs(v[i] - v[i + j]) + rec(i + j));
}
return dp[i] = ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (ll i = 0; i < n; i++) {
cin >> a;
v.pb(a);
}
memset(dp, -1, sizeof(dp));
ll ans = rec(0);
cout << ans << endl;
return 0;
}
|
insert
| 16 | 16 | 16 | 18 |
TLE
| |
p03161
|
C++
|
Runtime Error
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, t;
cin >> n >> k;
vector<int> input(n, 0);
vector<int> score(n, 0);
for (int i = 0; i < n; i++) {
cin >> t;
input[i] = t;
}
for (int i = 1; i < k; i++) {
t = INT_MAX;
for (int j = 1; j <= i; j++) {
t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
}
score[i] = t;
}
for (int i = k; i < n; i++) {
t = INT_MAX;
for (int j = 1; j <= k; j++) {
t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
}
score[i] = t;
}
cout << score[n - 1] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, t;
cin >> n >> k;
vector<int> input(n, 0);
vector<int> score(n, 0);
for (int i = 0; i < n; i++) {
cin >> t;
input[i] = t;
}
for (int i = 1; i < k && i < n; i++) {
t = INT_MAX;
for (int j = 1; j <= i; j++) {
t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
}
score[i] = t;
}
for (int i = k; i < n; i++) {
t = INT_MAX;
for (int j = 1; j <= k; j++) {
t = min(t, abs(input[i] - input[i - j]) + score[i - j]);
}
score[i] = t;
}
cout << score[n - 1] << endl;
return 0;
}
|
replace
| 16 | 17 | 16 | 17 |
0
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.