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

46 篇文章 7 订阅
1 篇文章 0 订阅

本文同步发布于:https://goodcoder666.github.io/post/cf1307/

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 i i-th pile contains a i a_i ai 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 i i and j j j ( 1 ≤ i , j ≤ n 1\le i,j\le n 1i,jn) such that ∣ i − j ∣ = 1 |i−j|=1 ij=1 and a i > 0 a_i>0 ai>0 and apply a i = a i − 1 a_i=a_i−1 ai=ai1, a j = a j + 1 a_j = a_j + 1 aj=aj+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 1 1 (i.e. to maximize a 1 a_1 a1), and she only has d d d days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile 1 1 1 if she acts optimally!

输入

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

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

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1,a_2,\dots,a_n a1,a2,,an ( 0 ≤ a i ≤ 100 ) (0\le a_i\le 100) (0ai100) — 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 1 1 after d d 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 3 3 haybales in pile 1 1 1:

  • On day one, move a haybale from pile 3 3 3 to pile 2 2 2
  • On day two, move a haybale from pile 3 3 3 to pile 2 2 2
  • On day three, move a haybale from pile 3 3 3 to pile 1 1 1
  • On day four, move a haybale from pile 2 2 2 to pile 1 1 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 2 2 to pile 1 1 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 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) (0,0) to ( x , 0 ) (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: a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an. What is the minimum number of hops Rabbit needs to get from ( 0 , 0 ) (0,0) (0,0) to ( x , 0 ) (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 ( x i , y i ) (x_i,y_i) (xi,yi) and ( x j , y j ) (x_j,y_j) (xj,yj) is ( x i − x j ) 2 + ( y i − y j ) 2 \sqrt{(x_i−x_j)^2+(y_i−y_j)^2} (xixj)2+(yiyj)2 .

For example, if Rabbit has favorite numbers 1 and 3 he could hop from ( 0 , 0 ) (0,0) (0,0) to ( 4 , 0 ) (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) (4,0) in 2 2 2 hops (e.g. ( 0 , 0 ) → ( 2 , 5 ) → ( 4 , 0 ) (0,0)\to(2, \sqrt 5)\to(4,0) (0,0)(2,5 )(4,0)).

在这里插入图片描述
Here is a graphic for the first example. Both hops have distance 3 3 3, one of Rabbit’s favorite numbers.

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

The first line of each test case contains two integers n n n and x x x ( 1 ≤ n ≤ 1 0 5 1\le n\le 10^5 1n105, 1 ≤ x ≤ 1 0 9 1\le x\le 10^9 1x109) — the number of favorite numbers and the distance Rabbit wants to travel, respectively.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a i ≤ 1 0 9 1\le a_i \le 10^9 1ai109) — Rabbit’s favorite numbers. It is guaranteed that the favorite numbers are distinct.

It is guaranteed that the sum of n n n over all the test cases will not exceed 1 0 5 10^5 105.

输出

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) (2,5 ), then to ( 4 , 0 ) (4,0) (4,0) for a total of two hops. Each hop has a distance of 3 3 3, which is one of his favorite numbers.

In the second test case of the sample, one way for Rabbit to hop 3 3 3 times is: ( 0 , 0 ) → ( 4 , 0 ) → ( 8 , 0 ) → ( 12 , 0 ) (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) (0,0) to ( 5 , 0 ) (5,0) (5,0).

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

输入

The first line contains a string S S S of lowercase Latin letters ( 1 ≤ ∣ S ∣ ≤ 105 1\le |S|\le 105 1S105) — 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) (1),(2),(3)
  • b occurs at ( 4 ) , ( 5 ) (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) (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) (1,2),(1,3),(2,3)
  • bb occurs at ( 4 , 5 ) (4,5) (4,5)
  • aab occurs at ( 1 , 3 , 5 ) , ( 2 , 3 , 4 ) (1,3,5), (2,3,4) (1,3,5),(2,3,4)
  • aaa occurs at ( 1 , 2 , 3 ) (1,2,3) (1,2,3)
  • abb occurs at ( 3 , 4 , 5 ) (3,4,5) (3,4,5)
  • aaab occurs at ( 1 , 2 , 3 , 4 ) (1,2,3,4) (1,2,3,4)
  • aabb occurs at ( 2 , 3 , 4 , 5 ) (2,3,4,5) (2,3,4,5)
  • aaabb occurs at ( 1 , 2 , 3 , 4 , 5 ) (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;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值