A. Cow and Haybales
题面
The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of n haybale piles on the farm. The \(i\)-th pile contains \(a_i\) haybales.
However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices \(i\) and \(j\) (\(1\le i,j\le n\)) such that \(|i−j|=1\) and \(a_i>0\) and apply \(a_i=a_i−1\), \(a_j = a_j + 1\). She may also decide to not do anything on some days because she is lazy.
Bessie wants to maximize the number of haybales in pile \(1\) (i.e. to maximize \(a_1\)), and she only has \(d\) days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile \(1\) if she acts optimally!
输入
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 100\)) — the number of test cases. Next \(2t\) lines contain a description of test cases — two lines per test case.
The first line of each test case contains integers \(n\) and \(d\) (\(1\le n,d\le 100\)) — the number of haybale piles and the number of days, respectively.
The second line of each test case contains \(n\) integers \(a_1,a_2,\dots,a_n\) \((0\le a_i\le 100)\) — the number of haybales in each pile.
输出
For each test case, output one integer: the maximum number of haybales that may be in pile \(1\) after \(d\) days if Bessie acts optimally.
样例
输入
3
4 5
1 0 3 2
2 2
100 1
1 8
0
输出
3
101
0
注释
In the first test case of the sample, this is one possible way Bessie can end up with \(3\) haybales in pile \(1\):
- On day one, move a haybale from pile \(3\) to pile \(2\)
- On day two, move a haybale from pile \(3\) to pile \(2\)
- On day three, move a haybale from pile \(3\) to pile \(1\)
- On day four, move a haybale from pile \(2\) to pile \(1\)
- On day five, do nothing
In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile \(2\) to pile \(1\) on the second day.
解题思路
贪心算法:先把第二堆的移到第一堆,再把第三堆的移到第一堆,……
题目中的样例解释有点误导(😦)
代码
#include <cstdio>
#define maxn 105
using namespace std;inline int min(int a, int b)
{return a < b? a: b;
}int main(int argc, char** argv)
{int T;scanf("%d", &T);while(T--){int n, d, first;scanf("%d%d%d", &n, &d, &first);for(int i=1; i<n; i++){int x;scanf("%d", &x);if(d >= i){int num = min(d / i, x); // The max number of haybales bessie can take to pile 1d -= num * i;first += num;}}printf("%d\n", first);}return 0;
}
B. Cow and Friend
题面
Bessie has way too many friends because she is everyone's favorite cow! Her new friend Rabbit is trying to hop over so they can play!
More specifically, he wants to get from \((0,0)\) to \((x,0)\) by making multiple hops. He is only willing to hop from one point to another point on the 2D plane if the Euclidean distance between the endpoints of a hop is one of its n favorite numbers: \(a_1,a_2,…,a_n\). What is the minimum number of hops Rabbit needs to get from \((0,0)\) to \((x,0)\)? Rabbit may land on points with non-integer coordinates. It can be proved that Rabbit can always reach his destination.
Recall that the Euclidean distance between points \((x_i,y_i)\) and \((x_j,y_j)\) is \(\sqrt{(x_i−x_j)^2+(y_i−y_j)^2}\).
For example, if Rabbit has favorite numbers 1 and 3 he could hop from \((0,0)\) to \((4,0)\) in two hops as shown below. Note that there also exists other valid ways to hop to \((4,0)\) in \(2\) hops (e.g. \((0,0)\to(2, \sqrt 5)\to(4,0)\)).
Here is a graphic for the first example. Both hops have distance \(3\), one of Rabbit's favorite numbers.
In other words, each time Rabbit chooses some number \(a_i\) and hops with distance equal to \(a_i\) in any direction he wants. The same number can be used multiple times.
输入
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 1000\)) — the number of test cases. Next \(2t\) lines contain test cases — two lines per test case.
The first line of each test case contains two integers \(n\) and \(x\) (\(1\le n\le 10^5\), \(1\le x\le 10^9\)) — the number of favorite numbers and the distance Rabbit wants to travel, respectively.
The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1\le a_i \le 10^9\)) — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct.
It is guaranteed that the sum of \(n\) over all the test cases will not exceed \(10^5\).
输出
For each test case, print a single integer — the minimum number of hops needed.
样例
输入
4
2 4
1 3
3 12
3 4 5
1 5
5
2 10
15 4
输出
2
3
1
2
注释
The first test case of the sample is shown in the picture above. Rabbit can hop to \((2, \sqrt 5)\), then to \((4,0)\) for a total of two hops. Each hop has a distance of \(3\), which is one of his favorite numbers.
In the second test case of the sample, one way for Rabbit to hop \(3\) times is: \((0,0) → (4,0) → (8,0) → (12,0)\).
In the third test case of the sample, Rabbit can hop from \((0,0)\) to \((5,0)\).
In the fourth test case of the sample, Rabbit can hop: \((0,0) → (5,10\sqrt2) → (10,0)\).
解题思路
纯数学!!!
代码
#include <cstdio>
#define maxn 100005
using namespace std;int fav[maxn], n, d;int calc(int x)
{int tm = d / x, leftdist = d % x;if(leftdist == 0) return tm;if(tm == 0){for(int i=0; i<n; i++)if(fav[i] == leftdist)return 1;return 2;}return tm + 1;
}int main(int argc, char** argv)
{int T;scanf("%d", &T);while(T--){scanf("%d%d", &n, &d);int maxf = -1;for(int i=0; i<n; i++){scanf("%d", fav + i);if(fav[i] > maxf) maxf = fav[i];}printf("%d\n", calc(maxf));}return 0;
}
C. Cow and Message
题面
Bessie the cow has just intercepted a text that Farmer John sent to Burger Queen! However, Bessie is sure that there is a secret message hidden inside.
The text is a string \(S\) of lowercase Latin letters. She considers a string \(t\) as hidden in string \(S\) if \(t\) exists as a subsequence of \(S\) whose indices form an arithmetic progression. For example, the string aab
is hidden in string aaabb because it occurs at indices \(1\), \(3\), and \(5\), which form an arithmetic progression with a common difference of \(2\). Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of \(S\) are distinct if the sets of indices are different. Help her find the number of occurrences of the secret message!
For example, in the string aaabb
, a
is hidden \(3\) times, b
is hidden \(2\) times, ab
is hidden \(6\) times, aa
is hidden \(3\) times, bb
is hidden \(1\) time, aab
is hidden \(2\) times, aaa
is hidden \(1\) time, abb
is hidden \(1\) time, aaab
is hidden \(1\) time, aabb
is hidden \(1\) time, and aaabb
is hidden \(1\) time. The number of occurrences of the secret message is \(6\).
输入
The first line contains a string \(S\) of lowercase Latin letters (\(1\le |S|\le 105\)) — the text that Bessie intercepted.
输出
Output a single integer — the number of occurrences of the secret message.
样例
输入 #1
aaabb
输出 #1
6
输入 #2
usaco
输出 #2
1
输入 #3
lol
输出 #3
2
注释
In the first example, these are all the hidden strings and their indice sets:
a
occurs at \((1), (2), (3)\)b
occurs at \((4), (5)\)ab
occurs at \((1,4), (1,5), (2,4), (2,5), (3,4), (3,5)\)aa
occurs at \((1,2), (1,3), (2,3)\)bb
occurs at \((4,5)\)aab
occurs at \((1,3,5), (2,3,4)\)aaa
occurs at \((1,2,3)\)abb
occurs at \((3,4,5)\)aaab
occurs at \((1,2,3,4)\)aabb
occurs at \((2,3,4,5)\)aaabb
occurs at \((1,2,3,4,5)\)
Note that all the sets of indices are arithmetic progressions.
In the second example, no hidden string occurs more than once.
In the third example, the hidden string is the letter l
.
解题思路
动态规划
代码
#include <cstdio>
#define maxn 100005
#define let 26
using namespace std;typedef long long LL;char s[maxn], c;
LL cnt1[let][maxn], cnt2[let][let];int main(int argc, char** argv)
{cnt1[(s[0] = getchar()) - 'a'][0] ++;int slen = 1;while((c = getchar()) != '\n'){s[slen] = c;for(int i=0; i<let; i++)cnt1[i][slen] = cnt1[i][slen - 1];cnt1[c - 'a'][slen++] ++;}int last = slen - 1;for(int i=0; i<let; i++) if(cnt1[i][last]){cnt2[i][i] = cnt1[i][last] * (cnt1[i][last] - 1) >> 1;for(int j=0; j<slen; j++)if(s[j] != i + 'a')cnt2[i][s[j] - 'a'] += cnt1[i][last] - cnt1[i][j];}LL maxcnt = -1;for(int i=0; i<let; i++){if(cnt1[i][last] && cnt1[i][last] > maxcnt) maxcnt = cnt1[i][last];for(int j=0; j<let; j++)if(cnt2[i][j] && cnt2[i][j] > maxcnt)maxcnt = cnt2[i][j];}printf("%lld\n", maxcnt);return 0;
}