A Garden
Luba thinks about watering her garden. The garden can be represented as a segment of length k. Luba has got n buckets, the i-th bucket allows her to water some continuous subsegment of garden of length exactly ai each hour. Luba can't water any parts of the garden that were already watered, also she can't water the ground outside the garden.
Luba has to choose one of the buckets in order to water the garden as fast as possible (as mentioned above, each hour she will water some continuous subsegment of length ai if she chooses the i-th bucket). Help her to determine the minimum number of hours she has to spend watering the garden. It is guaranteed that Luba can always choose a bucket so it is possible water the garden.
See the examples for better understanding.
Input
The first line of input contains two integer numbers n and k (1 ≤ n, k ≤ 100) — the number of buckets and the length of the garden, respectively.
The second line of input contains n integer numbers ai (1 ≤ ai ≤ 100) — the length of the segment that can be watered by the i-th bucket in one hour.
It is guaranteed that there is at least one bucket such that it is possible to water the garden in integer number of hours using only this bucket.
Output
Print one integer number — the minimum number of hours required to water the garden.
Examples
input
3 6
2 3 5
output
2
input
6 7
1 2 3 4 5 6
output
7
Note
In the first test the best option is to choose the bucket that allows to water the segment of length 3. We can't choose the bucket that allows to water the segment of length 5 because then we can't water the whole garden.
In the second test we can choose only the bucket that allows us to water the segment of length 1.
题目类型:简单枚举+贪心
算法分析:从最大容量的桶开始枚举,直到找到能够整除garden长度的容积
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
/************************************************** filename :A.cpp author :maksyuki created time :2018/1/13 20:25:00 last modified :2018/1/13 21:16:57 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int aa[maxn]; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, k; while(cin >> n >> k) { for(int i = 1; i <= n; i++) cin >> aa[i]; sort(aa + 1, aa + 1 + n); int ans = -1; for(int i = n; i >= 1; i--) { if(k % aa[i] == 0) { ans = k / aa[i]; break; } } cout << ans << endl; } return 0; } |
B Browser
Luba is surfing the Internet. She currently has n opened tabs in her browser, indexed from 1 to n from left to right. The mouse cursor is currently located at the pos-th tab. Luba needs to use the tabs with indices from l to r (inclusive) for her studies, and she wants to close all the tabs that don't belong to this segment as fast as possible.
Each second Luba can either try moving the cursor to the left or to the right (if the cursor is currently at the tab i, then she can move it to the tab max(i - 1, a) or to the tab min(i + 1, b)) or try closing all the tabs to the left or to the right of the cursor (if the cursor is currently at the tab i, she can close all the tabs with indices from segment [a, i - 1] or from segment [i + 1, b]). In the aforementioned expressions a and b denote the minimum and maximum index of an unclosed tab, respectively. For example, if there were 7 tabs initially and tabs 1, 2 and 7 are closed, then a = 3, b = 6.
What is the minimum number of seconds Luba has to spend in order to leave only the tabs with initial indices from l to r inclusive opened?
Input
The only line of input contains four integer numbers n, pos, l, r (1 ≤ n ≤ 100, 1 ≤ pos ≤ n, 1 ≤ l ≤ r ≤ n) — the number of the tabs, the cursor position and the segment which Luba needs to leave opened.
Output
Print one integer equal to the minimum number of seconds required to close all the tabs outside the segment [l, r].
Examples
input
6 3 2 4
output
5
input
6 3 1 3
output
1
input
5 2 1 5
output
0
Note
In the first test Luba can do the following operations: shift the mouse cursor to the tab 2, close all the tabs to the left of it, shift the mouse cursor to the tab 3, then to the tab 4, and then close all the tabs to the right of it.
In the second test she only needs to close all the tabs to the right of the current position of the cursor.
In the third test Luba doesn't need to do anything.
题目类型:简单构造+贪心
算法分析:如果pos不在[l,r]之间的话,最优的操作是每次执行左移或右移使得pos到达l或者是r位置,因为del操作可以一次性删除,在还没到达l或者r之前使用del操作不是最优的。然后问题转换成pos在[l,r]之间的情况,此时若l和1,r和n之间还有tab则分类讨论,没有tab则不需要向给定方向移动+del,注意若两边都有tab,则需先向距离边界l,r小的移动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
/************************************************** filename :B.cpp author :maksyuki created time :2018/1/13 21:16:58 last modified :2018/1/16 10:53:41 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int main() { #ifdef LOCAL CFF; //CFO; #endif int n, p, l, r; while(cin >> n >> p >> l >> r) { int ans = 0; if(p < l) { ans += l - p; p = l; } if(p > r) { ans += p - r; p = r; } int va = n - r, vb = l - 1; int vva = r - p, vvb = p - l; if(va && vb) { ans += (vva + vvb + min(vva, vvb)); ans += 2; } else if(va) ans += vva, ans++; else if(vb) ans += vvb, ans++; cout << ans << endl; } return 0; } |
C Permute Digits
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b (1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.
Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.
Examples
input
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940
题目类型:数位构造+贪心
算法分析:若a位数小于b位数则a直接从高位开始贪心地放最大的数字。若a和b的位数相同,则需判断a每次从高位放置尽可能大的数字时是否能够构造出a。可以使用dfs递减搜索每一位并维护一个tag,若tag为false则表示a的前面高位中已经有小于对应b位的情况,此时放置数字时可以不用考虑b对应位的大小了,否则需要考虑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
/************************************************** filename :C.cpp author :maksyuki created time :2018/1/13 21:45:54 last modified :2018/1/14 19:48:51 file location :C:\Users\abcd\Desktop\codeforces ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; string sa, sb; int lena, lenb, cnt[16]; LL maxval; bool is_find; void dfs(int cur, LL v, bool is_first) { if(is_find) return; if(cur == lenb) { maxval = max(maxval, v); is_find = true; return ; } int vv = sb[cur] - '0'; if(is_first) { for(int i = 9; i >= 0; i--) { if(cnt[i] && vv >= i) { cnt[i]--; if(vv > i) dfs(cur + 1, v * 10LL + (LL) i, false); else dfs(cur + 1, v * 10LL + (LL) i, true); cnt[i]++; } } } else { for(int i = 9; i >= 0; i--) { if(cnt[i]) { cnt[i]--; dfs(cur + 1, v * 10LL + (LL) i, false); cnt[i]++; } } } } int main() { #ifdef LOCAL CFF; //CFO; #endif while(cin >> sa >> sb) { lena = sa.size(), lenb = sb.size(); for(int i = 0; i < 16; i++) cnt[i] = 0; for(int i = 0; i < lena; i++) cnt[sa[i]-'0']++; if(lena < lenb) { for(int i = 9; i >= 0; i--) { while(cnt[i]) { cnt[i]--; printf("%d", i); } } puts(""); } else { maxval = -INF; is_find = false; dfs(0, 0LL, true); printf("%I64d\n", maxval); } } return 0; } |
- « 上一篇:uva1592