Hướng dẫn giải của Bedao Mini Contest 26 - Bản nhạc thế kỉ
Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.
Nhận xét: Chỉ cần cố định ~a_1~ thì ta sẽ có thông tin của toàn bộ mảng ~a~. Vì vậy chỉ cần thử mọi giá trị của ~a_1~ và tính thời gian cho từng trường hợp.
Độ phức tạp: ~O(n \times max(a[i]))~
/* Author: Cadocx Codeforces: https://codeforces.com/profile/Kadoc VNOJ: oj.vnoi.info/user/Cadoc */ #include <bits/stdc++.h> using namespace std; // input/output #define fastIO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define el cout << '\n' #define debug(x) cout << #x << " = " << x << '\n' #define execute cerr << "Time elapsed: " << (1.0 * clock() / CLOCKS_PER_SEC) << "s" //data type #define ll long long #define ull unsigned long long #define pii pair<int, int> #define pll pair<ll, ll> #define piv pair<int, vector<int>> #define vi vector<int> #define vl vector<ll> #define vc vector<char> template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return 1; }; return 0; } template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return 1; }; return 0; } //STL #define sz(x) (int)(x).size() #define FOR(i,l,r) for(auto i = l; i <= r; i++) #define FORD(i,r,l) for(auto i = r; i >= l; i--) #define forin(i,a) for(auto i : a) #define pb push_back #define eb emplace_back #define pf push_front #define all(x) (x).begin(), (x).end() #define fi first #define se second //bitmask #define bitcnt(n) __builtin_popcount(n) #define mask(i) (1 << (i)) #define bit(n, i) (((n) >> (i)) & 1) #define set_on(n, i) ((n) | mask(i)) #define set_off(n, i) ((n) & ~mask(i)) //constant #define N 5005 #define MOD 1000000007 #define INF 0x3f3f3f3f #define LINF 0x3f3f3f3f3f3f3f3f #define base 31 #define Kadoc 0 int n, k; int a[N]; void solve(){ cin >> n >> k; FOR(i, 1, n) cin >> a[i]; ll Ans = LINF; FOR(x, 1, 6){ ll Res = 0; int cur = x; FOR(i, 1, n){ Res += abs(a[i] - cur); cur += k; } Ans = min(Ans, Res); } cout << Ans; } int main(){ #define NAME "TASK" if(fopen(NAME".inp", "r")){ freopen(NAME".inp", "r", stdin); freopen(NAME".out", "w", stdout); } fastIO; if(Kadoc){ int tc; cin >> tc; while(tc--){ solve(); } } else solve(); }
Bình luận