Skip to content

Commit 6dc2f83

Browse files
committed
atcoder dp contest
1 parent 04fc5bd commit 6dc2f83

File tree

61 files changed

+508
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+508
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
#include <bits/stdc++.h>
7+
#include <ext/pb_ds/assoc_container.hpp>
8+
#include <ext/pb_ds/tree_policy.hpp>
9+
using namespace std;
10+
using namespace __gnu_pbds;
11+
typedef long long ll ;
12+
typedef unsigned long long ull;
13+
typedef vector<ll> vl;
14+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
15+
/* Abbrevations */
16+
#define ff first
17+
#define ss second
18+
#define mp make_pair
19+
#define line cout<<endl;
20+
#define pb push_back
21+
#define Endl "\n"
22+
// loops
23+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
24+
// Some print
25+
#define no cout<<"No"<<endl;
26+
#define yes cout<<"Yes"<<endl;
27+
// sort
28+
#define all(V) (V).begin(),(V).end()
29+
#define srt(V) sort(all(V))
30+
#define srtGreat(V) sort(all(V),greater<ll>())
31+
// some extra
32+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
33+
#define precision(x) cout<<fixed<<setprecision(x);
34+
#define sz(V) ll(V.size())
35+
// template
36+
template <typename T>
37+
T mymax(T x,T y)
38+
{
39+
return (x>y)?x:y;
40+
}
41+
// function
42+
ll power(ll x,ll y,ll mod)
43+
{
44+
ll res=1;
45+
// x=x%mod;
46+
while(y>0)
47+
{
48+
if(y%2==1)
49+
{
50+
res*=x;
51+
// res=res%mod;
52+
}
53+
y/=2; x*=x; // x=x%mod;
54+
}
55+
return res;
56+
}
57+
ll str_to_num(string s)
58+
{
59+
return stoi(s);
60+
}
61+
62+
string num_to_str(ll num)
63+
{
64+
return to_string(num);
65+
}
66+
// datatype definination
67+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
68+
class Point
69+
{
70+
public:
71+
ll x;
72+
ll y;
73+
ll z;
74+
ll getsum()
75+
{
76+
return x+y+z;
77+
}
78+
};
79+
/* ascii value
80+
A=65,Z=90,a=97,z=122
81+
*/
82+
/* --------------------MAIN PROGRAM----------------------------*/
83+
// to run ctrl+b
84+
const ll INF=LONG_MAX;
85+
const ll mod1=1e9+7;
86+
const ll mod2=998244353;
87+
88+
89+
// Techniques
90+
// divide into cases, brute force, pattern finding
91+
// sort, greedy, binary search, two pointer
92+
// transform into graph
93+
94+
ll solve()
95+
{
96+
ll n;
97+
cin>>n;
98+
vl v(n);
99+
forin(v,n);
100+
vl dp(n,0);
101+
dp[0]=0;
102+
dp[1]=abs(v[1]-v[0]);
103+
for(ll i=2;i<n;i++){
104+
dp[i]=min({abs(v[i]-v[i-1])+dp[i-1],abs(v[i]-v[i-2])+dp[i-2]});
105+
}
106+
cout<<dp[n-1]<<endl;
107+
return 0;
108+
}
109+
110+
int main()
111+
{
112+
speed;
113+
/* #ifndef ONLINE_JUDGE
114+
freopen("input.txt","r",stdin);
115+
freopen("output.txt","w",stdout);
116+
#endif */
117+
ll TestCase=1;
118+
// cin>>TestCase;
119+
while(TestCase--)
120+
{
121+
solve();
122+
}
123+
}
124+
/* -----------------END OF PROGRAM --------------------*/
125+
/*
126+
* stuff you should look before submission
127+
* constraint and time limit
128+
* int overflow
129+
* special test case (n=0||n=1||n=2)
130+
* don't get stuck on one approach if you get wrong answer
131+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
#include <bits/stdc++.h>
7+
#include <ext/pb_ds/assoc_container.hpp>
8+
#include <ext/pb_ds/tree_policy.hpp>
9+
using namespace std;
10+
using namespace __gnu_pbds;
11+
typedef long long ll ;
12+
typedef unsigned long long ull;
13+
typedef vector<ll> vl;
14+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
15+
/* Abbrevations */
16+
#define ff first
17+
#define ss second
18+
#define mp make_pair
19+
#define line cout<<endl;
20+
#define pb push_back
21+
#define Endl "\n"
22+
// loops
23+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
24+
// Some print
25+
#define no cout<<"No"<<endl;
26+
#define yes cout<<"Yes"<<endl;
27+
// sort
28+
#define all(V) (V).begin(),(V).end()
29+
#define srt(V) sort(all(V))
30+
#define srtGreat(V) sort(all(V),greater<ll>())
31+
// some extra
32+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
33+
#define precision(x) cout<<fixed<<setprecision(x);
34+
#define sz(V) ll(V.size())
35+
// template
36+
template <typename T>
37+
T mymax(T x,T y)
38+
{
39+
return (x>y)?x:y;
40+
}
41+
// function
42+
ll power(ll x,ll y,ll mod)
43+
{
44+
ll res=1;
45+
// x=x%mod;
46+
while(y>0)
47+
{
48+
if(y%2==1)
49+
{
50+
res*=x;
51+
// res=res%mod;
52+
}
53+
y/=2; x*=x; // x=x%mod;
54+
}
55+
return res;
56+
}
57+
ll str_to_num(string s)
58+
{
59+
return stoi(s);
60+
}
61+
62+
string num_to_str(ll num)
63+
{
64+
return to_string(num);
65+
}
66+
// datatype definination
67+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
68+
class Point
69+
{
70+
public:
71+
ll x;
72+
ll y;
73+
ll z;
74+
ll getsum()
75+
{
76+
return x+y+z;
77+
}
78+
};
79+
/* ascii value
80+
A=65,Z=90,a=97,z=122
81+
*/
82+
/* --------------------MAIN PROGRAM----------------------------*/
83+
// to run ctrl+b
84+
const ll INF=LONG_MAX;
85+
const ll mod1=1e9+7;
86+
const ll mod2=998244353;
87+
88+
89+
// Techniques
90+
// divide into cases, brute force, pattern finding
91+
// sort, greedy, binary search, two pointer
92+
// transform into graph
93+
94+
ll solve()
95+
{
96+
ll n,k;
97+
cin>>n>>k;
98+
vl v(n);
99+
forin(v,n);
100+
vl dp(n,LONG_MAX);
101+
dp[0]=0;
102+
dp[1]=abs(v[0]-v[1]);
103+
for(ll i=2;i<n;i++){
104+
for(ll j=1;j<=k;j++){
105+
if((i-j)>=0){
106+
dp[i]=min({dp[i],dp[i-j]+abs(v[i-j]-v[i])});
107+
}
108+
}
109+
}
110+
cout<<dp[n-1]<<endl;
111+
return 0;
112+
}
113+
114+
int main()
115+
{
116+
speed;
117+
/* #ifndef ONLINE_JUDGE
118+
freopen("input.txt","r",stdin);
119+
freopen("output.txt","w",stdout);
120+
#endif */
121+
ll TestCase=1;
122+
// cin>>TestCase;
123+
while(TestCase--)
124+
{
125+
solve();
126+
}
127+
}
128+
/* -----------------END OF PROGRAM --------------------*/
129+
/*
130+
* stuff you should look before submission
131+
* constraint and time limit
132+
* int overflow
133+
* special test case (n=0||n=1||n=2)
134+
* don't get stuck on one approach if you get wrong answer
135+
*/
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Atcoder/practice/a.out

-30 KB
Binary file not shown.

0 commit comments

Comments
 (0)