The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of n haybale piles on the farm. The -th pile contains 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 and () such that and and apply , . 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 (i.e. to maximize ), and she only has days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile if she acts optimally!
The input consists of multiple test cases. The first line contains an integer () — the number of test cases. Next lines contain a description of test cases — two lines per test case.
The first line of each test case contains integers and () — the number of haybale piles and the number of days, respectively.
The second line of each test case contains integers — the number of haybales in each pile.
For each test case, output one integer: the maximum number of haybales that may be in pile after 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 haybales in pile :
In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile to pile 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 1
d -= num * i;
first += num;
}
}
printf("%d\n", first);
}
return 0;
}
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 to 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: . What is the minimum number of hops Rabbit needs to get from to ? 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 and is .
For example, if Rabbit has favorite numbers 1 and 3 he could hop from to in two hops as shown below. Note that there also exists other valid ways to hop to in hops (e.g. ).
Here is a graphic for the first example. Both hops have distance , one of Rabbit's favorite numbers.
In other words, each time Rabbit chooses some number and hops with distance equal to 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 () — the number of test cases. Next lines contain test cases — two lines per test case.
The first line of each test case contains two integers and (, ) — the number of favorite numbers and the distance Rabbit wants to travel, respectively.
The second line of each test case contains integers () — Rabbit's favorite numbers. It is guaranteed that the favorite numbers are distinct.
It is guaranteed that the sum of over all the test cases will not exceed .
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 , then to for a total of two hops. Each hop has a distance of , which is one of his favorite numbers.
In the second test case of the sample, one way for Rabbit to hop times is: .
In the third test case of the sample, Rabbit can hop from to .
In the fourth test case of the sample, Rabbit can hop: .
纯数学!!!
#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;
}
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 of lowercase Latin letters. She considers a string as hidden in string if exists as a subsequence of whose indices form an arithmetic progression. For example, the string aab
is hidden in string aaabb because it occurs at indices , , and , which form an arithmetic progression with a common difference of . Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of 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 times, b
is hidden times, ab
is hidden times, aa
is hidden times, bb
is hidden time, aab
is hidden times, aaa
is hidden time, abb
is hidden time, aaab
is hidden time, aabb
is hidden time, and aaabb
is hidden time. The number of occurrences of the secret message is .
The first line contains a string of lowercase Latin letters () — the text that Bessie intercepted.
Output a single integer — the number of occurrences of the secret message.
aaabb
6
usaco
1
lol
2
In the first example, these are all the hidden strings and their indice sets:
a
occurs at b
occurs at ab
occurs at aa
occurs at bb
occurs at aab
occurs at aaa
occurs at abb
occurs at aaab
occurs at aabb
occurs at aaabb
occurs at 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;
}