cf--A. Cow and Haybales+简单推理

Description

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 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 and j (1≤i,j≤n) such that |i−j|=1 and ai>0 and apply ai=ai−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 (i.e. to maximize a1), 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!

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤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≤n,d≤100) — the number of haybale piles and the number of days, respectively.

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤100) — the number of haybales in each pile.

Output

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.

传送门:http://codeforces.com/contest/1307/problem/A

题意:

一只母牛想让第一堆(也就是这里的第0堆)的干草数目尽可能地多,它可以进行如下操作:
1.只能移动相邻堆,每次只能移动一捆,且移走后地那堆必须是大于等于零地
2.题目=设定,每天只能移动一捆,也可以什么都不做。

思路:

离第一堆越远则需要地天数越多,且满足第i堆移动需要i天。所以从后依次向后遍历即可。

AC代码:

#include<bits/stdc++.h>
using namespace std;
int arr[105];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,d;
		scanf("%d%d",&n,&d);
		for(int i=0;i<n;i++)	scanf("%d",&arr[i]);
		for(int i=1;i<n;i++)
		{
			if(d-arr[i]*i>0)	
				arr[0]+=arr[i],d-=arr[i]*i;//还可进行下一次移动 
			else
			{
				arr[0]+=d/i;//只能进行最后一次移动 
				break;
			}
		}	
		printf("%d\n",arr[0]);
	}
	return 0;
} 
发布了39 篇原创文章 · 获赞 1 · 访问量 568

猜你喜欢

转载自blog.csdn.net/qq_45249273/article/details/104418849