Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define fi first
- #define se second
- #define pb push_back
- #define pf push_front
- #define popb pop_back
- #define popf pop_front
- #define ins insert
- #define pq priority_queue
- #define minele min_element
- #define maxele max_element
- #define lb lower_bound //first pos >= val
- #define ub upper_bound // first pos > val
- #define cnt_bit __builtin_popcount
- #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
- //#pragma GCC optimize("Ofast")
- //#pragma GCC target("avx,avx2,fma")
- using namespace std;
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- typedef long long ll;
- typedef pair<ll, ll> pll;
- typedef pair<int, int> pii;
- int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
- int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
- int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
- const ll oo = 1e18;
- const ll maxN = 1e6;
- /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
- void maximize(int &a, int b) {
- a = max(a, b);
- }
- void minimize(int &a, int b) {
- a = min(a, b);
- }
- int p[50], a[5], b[5];
- int dp[1010][1010];
- int f[22][(1 << 16)];
- void solve() {
- int v, n, m;
- cin >> v >> n >> m;
- for(int i = 1; i <= n; i++) cin >> p[i];
- if(v <= 1000) {
- for(int i = 0; i <= v; i++) {
- for(int j = 0; j <= v; j++) {
- dp[i][j] = -1;
- }
- }
- dp[v][0] = 0;
- queue<pii> q;
- q.push(make_pair(v, 0));
- while(!q.empty()) {
- a[0] = q.front().fi, a[1] = q.front().se; q.pop();
- a[2] = v - a[0] - a[1];
- for(int i = 0; i <= 2; i++) {
- for(int j = 0; j <= 2; j++) {
- if(i == j) continue;
- //from ai to aj
- for(int x = 0; x <= n; x++) {
- if(a[i] <= p[x]) break;
- int add = a[i] - p[x];
- for(int y = 0; y < 3; y++) b[y] = a[y];
- b[i] = a[i] - add; b[j] = a[j] + add;
- if(dp[b[0]][b[1]] == -1) {
- dp[b[0]][b[1]] = dp[a[0]][a[1]] + 1;
- q.push(make_pair(b[0], b[1]));
- }
- }
- for(int x = 0; x <= n; x++) {
- if(a[j] >= p[x]) continue;
- int add = p[x] - a[j];
- if(add > a[i]) break;
- for(int y = 0; y < 3; y++) b[y] = a[y];
- b[i] = a[i] - add; b[j] = a[j] + add;
- if(dp[b[0]][b[1]] == -1) {
- dp[b[0]][b[1]] = dp[a[0]][a[1]] + 1;
- q.push(make_pair(b[0], b[1]));
- }
- }
- }
- }
- }
- int ans = 1e9;
- for(int i = 0; i <= v; i++) {
- for(int j = 0; j <= v; j++) {
- if(dp[i][j] == -1) continue;
- if(i == m || j == m || (v - i - j) == m) ans = min(ans, dp[i][j]);
- }
- }
- if(ans == 1e9) ans = -1;
- cout << ans;
- } else {
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= v; j++) f[i][j] = -1;
- }
- f[0][v] = 0;
- queue<pii> q;
- q.push(make_pair(0, v));
- while(!q.empty()) {
- int g = f[q.front().fi][q.front().se];
- a[0] = p[q.front().fi], a[1] = q.front().se; q.pop();
- a[2] = v - a[0] - a[1];
- for(int i = 0; i < 3; i++) {
- for(int j = 0; j < 3; j++) {
- if(i == j) continue;
- //from ai to aj
- for(int x = 0; x <= n; x++) {
- if(a[i] <= p[x]) continue;
- int add = a[i] - p[x];
- for(int y = 0; y < 3; y++) b[y] = a[y];
- b[i] = p[x]; b[j] = a[j] + add;
- if(f[x][b[j]] == -1) {
- f[x][b[j]] = g + 1;
- q.push(make_pair(x, b[j]));
- }
- }
- for(int x = 0; x <= n; x++) {
- if(a[j] >= p[x]) continue;
- int add = p[x] - a[j];
- if(add > a[i]) break;
- for(int y = 0; y < 3; y++) b[y] = a[y];
- b[i] = a[i] - add; b[j] = p[x];
- if(f[x][b[i]] == -1) {
- f[x][b[i]] = g + 1;
- q.push(make_pair(x, b[i]));
- }
- }
- }
- }
- }
- int ans = 1e9;
- for(int i = 0; i <= n; i++) {
- for(int j = 0; j <= v; j++) {
- if(f[i][j] == -1) continue;
- if(p[i] == m || j == m || (v - p[i] - j == m)) ans = min(ans, f[i][j]);
- }
- }
- if(ans == 1e9) ans = -1;
- cout << 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);
- #else
- //online
- #endif
- int tc = 1, ddd = 0;
- // cin >> tc;
- while(tc--) {
- //ddd++;
- //cout << "Case #" << ddd << ": ";
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement