Hướng dẫn giải + code mẫu

Nhớ quê ra đứng đỉnh đèo, bỗng đâu thấy một chú mèo .. gâu gâu!
Dừng chân đứng lại trên cầu, bỗng đâu thấy một con trâu vàng vàng..
int root(int n) { | |
if (n >= 10) { | |
int s = 0; | |
while (n > 0) { | |
s += n % 10; | |
n /= 10; | |
} | |
return root(s); | |
} | |
return n; | |
} |
/** | |
* MDL - https://codechef.com/problems/MDL | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<algorithm> | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T, N; cin >> T; | |
while (T--) { | |
cin >> N; | |
vector<int> a, b; | |
for (int i = 1; i <= N; ++i) {int u; cin >> u; a.push_back(u); b.push_back(u);} | |
sort(a.begin(), a.end()); | |
int f = 0, l = 0; | |
for (int i = 0; i <= N; ++i) | |
if (b[i] == a[0]) {f = a[0]; l = a[a.size() - 1]; break;} | |
else if (b[i] == a[a.size() - 1]) {f = a[a.size() - 1]; l = a[0]; break;} | |
cout << f << ' ' << l << endl; | |
} | |
return 0; | |
} |
/** | |
* HYPNOS - Happy Numbers I | |
* https://www.spoj.com/problems/HPYNOS/ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<algorithm> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); | |
vector<int> e; | |
int n; cin >> n; | |
int s = 0, c = 0; | |
while (true) { | |
while (n > 0) { | |
s += (n % 10) * (n % 10); | |
n /= 10; | |
} | |
c++; | |
if (s == 1) { | |
cout << c << endl; | |
break; | |
} else { | |
// Binary search | |
sort(e.begin(), e.end()); | |
if (binary_search(e.begin(), e.end(), s)) { | |
cout << -1 << endl; | |
break; | |
} | |
e.push_back(s); | |
n = s; | |
s = 0; | |
} | |
} | |
return 0; | |
} |
/** | |
* ADASTAIR - https://www.codechef.com/problems/ADASTAIR | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T; cin >> T; | |
int N, K, S; | |
while (T--) { | |
cin >> N >> K; | |
S = 0; | |
vector<int> a; a.push_back(0); | |
for (int i = 1; i <= N; ++i) { | |
int u; cin >> u; a.push_back(u); | |
} | |
for (int i = 1; i <= N; ++i) | |
if (a[i] - a[i - 1] > K) { | |
S += (a[i] - a[i - 1]) / K; | |
if ((a[i] - a[i - 1]) % K == 0) S--; | |
} | |
cout << S << endl; | |
} | |
return 0; | |
} |
/** | |
* ZOZ - https://codechef.com/problems/ZOZ | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int t; cin >> t; | |
while(t--) { | |
int n, k, s = 0, c = 0; cin >> n >> k; | |
vector<int>a; | |
for(int i = 1; i <= n; ++i) {int u; cin >> u; a.push_back(u); s += u;} | |
for (int i = 0; i < n; ++i) | |
if (a[i] + k > s - a[i]) c++; | |
cout << c << endl; | |
} | |
return 0; | |
} |
/* | |
* longestArrays.cpp - Dynamic Programming approach. | |
* Code by @trhgquan - https://github.com/trhgquan | |
*/ | |
int longestArrays(vector<int> a) { | |
vector<int> b(a.size(), 1); | |
int res = 1; | |
for (unsigned i = 1; i < a.size(); ++i){ | |
if (a[i] >= a[i - 1]) b[i] = b[i - 1] + 1; | |
res = max(b[i], res); | |
} | |
return res; | |
} |
// | |
// CMPRSS - https://codechef.com/problems/CMPRSS | |
// Code by @trhgquan - https://github.com/trhgquan | |
// | |
#include<iostream> | |
#include<vector> | |
using namespace std; | |
int main() { | |
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | |
int T, N; cin >> T; | |
while (T--) { | |
cin >> N; | |
vector<int>a, b; | |
for (int i = 1; i <= N; ++i) {int u; cin >> u; a.push_back(u);b.push_back(1);} | |
// | |
// The idea is using DP (Dynamic Programming), | |
// since we need to count every adjacent subsequence. | |
// An element is flagged "1" if it is an edge, or, a single item. | |
// | |
for (int i = 1; i < N; ++i) { | |
if (a[i] - a[i - 1] == 1) b[i] = b[i - 1] + 1; | |
if (a[i] - a[i - 1] > 1 && b[i] - b[i - 1] < 0) b[i - 1] = b[i]; | |
if (i == N - 1 && b[i] != 1) b[i] = 1; | |
} | |
// | |
// Print result | |
// | |
cout << a[0]; | |
int c = 1; | |
for (int i = 1; i < N; ++i){ | |
if (b[i] != 1) ++c; | |
if (b[i] == 1) { | |
if (c >= 2) { | |
cout << "..."; | |
c = 1; | |
} else cout << ","; | |
cout << a[i]; | |
} | |
} | |
cout << endl; | |
} | |
return 0; | |
} |