GoodCoder666的个人博客

CodeForces Round #621 ABC (1307A+1307B+1307C) 题解

2020-02-22 · 13 min read
C++ 算法竞赛

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 ii-th pile contains aia_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 ii and jj (1i,jn1\le i,j\le n) such that ij=1|i−j|=1 and ai>0a_i>0 and apply ai=ai1a_i=a_i−1, aj=aj+1a_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 11 (i.e. to maximize a1a_1), and she only has dd days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 11 if she acts optimally!

输入

The input consists of multiple test cases. The first line contains an integer tt (1t1001\le t\le 100) — the number of test cases. Next 2t2t lines contain a description of test cases — two lines per test case.

The first line of each test case contains integers nn and dd (1n,d1001\le n,d\le 100) — the number of haybale piles and the number of days, respectively.

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\dots,a_n (0ai100)(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 11 after dd 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 33 haybales in pile 11:

  • On day one, move a haybale from pile 33 to pile 22
  • On day two, move a haybale from pile 33 to pile 22
  • On day three, move a haybale from pile 33 to pile 11
  • On day four, move a haybale from pile 22 to pile 11
  • 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 22 to pile 11 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;
}

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)(0,0) to (x,0)(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: a1,a2,,ana_1,a_2,…,a_n. What is the minimum number of hops Rabbit needs to get from (0,0)(0,0) to (x,0)(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 (xi,yi)(x_i,y_i) and (xj,yj)(x_j,y_j) is (xixj)2+(yiyj)2\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)(0,0) to (4,0)(4,0) in two hops as shown below. Note that there also exists other valid ways to hop to (4,0)(4,0) in 22 hops (e.g. (0,0)(2,5)(4,0)(0,0)\to(2, \sqrt 5)\to(4,0)).

Here is a graphic for the first example. Both hops have distance 33, one of Rabbit's favorite numbers.

In other words, each time Rabbit chooses some number aia_i and hops with distance equal to aia_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 tt (1t10001\le t\le 1000) — the number of test cases. Next 2t2t lines contain test cases — two lines per test case.

The first line of each test case contains two integers nn and xx (1n1051\le n\le 10^5, 1x1091\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 nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1091\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 nn over all the test cases will not exceed 10510^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,5)(2, \sqrt 5), then to (4,0)(4,0) for a total of two hops. Each hop has a distance of 33, which is one of his favorite numbers.

In the second test case of the sample, one way for Rabbit to hop 33 times is: (0,0)(4,0)(8,0)(12,0)(0,0) → (4,0) → (8,0) → (12,0).

In the third test case of the sample, Rabbit can hop from (0,0)(0,0) to (5,0)(5,0).

In the fourth test case of the sample, Rabbit can hop: (0,0)(5,102)(10,0)(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 SS of lowercase Latin letters. She considers a string tt as hidden in string SS if tt exists as a subsequence of SS whose indices form an arithmetic progression. For example, the string aab is hidden in string aaabb because it occurs at indices 11, 33, and 55, which form an arithmetic progression with a common difference of 22. Bessie thinks that any hidden string that occurs the most times is the secret message. Two occurrences of a subsequence of SS 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 33 times, b is hidden 22 times, ab is hidden 66 times, aa is hidden 33 times, bb is hidden 11 time, aab is hidden 22 times, aaa is hidden 11 time, abb is hidden 11 time, aaab is hidden 11 time, aabb is hidden 11 time, and aaabb is hidden 11 time. The number of occurrences of the secret message is 66.

输入

The first line contains a string SS of lowercase Latin letters (1S1051\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)(1), (2), (3)
  • b occurs at (4),(5)(4), (5)
  • ab occurs at (1,4),(1,5),(2,4),(2,5),(3,4),(3,5)(1,4), (1,5), (2,4), (2,5), (3,4), (3,5)
  • aa occurs at (1,2),(1,3),(2,3)(1,2), (1,3), (2,3)
  • bb occurs at (4,5)(4,5)
  • aab occurs at (1,3,5),(2,3,4)(1,3,5), (2,3,4)
  • aaa occurs at (1,2,3)(1,2,3)
  • abb occurs at (3,4,5)(3,4,5)
  • aaab occurs at (1,2,3,4)(1,2,3,4)
  • aabb occurs at (2,3,4,5)(2,3,4,5)
  • aaabb occurs at (1,2,3,4,5)(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;
}