Codeforces Round #318-(D. Bear and Blocks)

这道题我上来就是想到的是暴力,每次都把表面的那一层减掉,直到所有的高度为0为止。但是T了。

题意:

现在有n个方格,然后每个方格都有一个高度,然后每次都可以把那些非完整块(就是它的四个方向没有被完全包围)给连在一起消去。问你最后把所有的方块消去需要几次。

思路:

我们只需要从左边,右边分别进行一次消去,然后最后进行一次判断就好了。最多的次数不可能超过最大的那个的高度。

首先初始化为h1[0]=0,h2[n+1]=0, 我们设两个数组h1代表的是从左边开始消去每次的最大高度,h2则是右边的。

h1[i]=min(h[i],h1[i-1]+1);

h2[i]=min(h[i],h2[i+1]+1);

这两个方程应该想一下就能明白的。我们每次当然应该满足高度较小的那个。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<vector>
using namespace std;
#define inf 99999999
#define maxn 100010
int h[maxn];
int h1[maxn],h2[maxn];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&h[i]);
	int ans=0;
	h1[0]=0;
	for(int i=1;i<=n;i++){
		h1[i]=min(h[i],h1[i-1]+1);
	}
	h2[n+1]=0;
	for(int i=n;i>=1;i--){
		h2[i]=min(h[i],h2[i+1]+1);
	}
	for(int i=1;i<=n;i++){
		ans=max(ans,min(h1[i],h2[i]));
	}
	printf("%d\n",ans);
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 21:18:47

Codeforces Round #318-(D. Bear and Blocks)的相关文章

Codeforces Round #318(Div. 2)(A,B,C,D)

A题: 题目地址:Bear and Elections 题意:最少变换多少次可以使得第一个数字大于后面所有数字. 思路:把后面n-1个数排序,让第一个和最后一个数比较,然后增减.知道第一个数大于最后一个数为止 #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <sstream>

Codeforces Round #318(Div 1) 573A, 573B,573C

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 这场的前两题完全是手速题...A题写了7分钟,交的时候已经500+了,好在B题出的速度勉强凑活吧,and C题也没有FST A. Bear and Poker Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n pla

Codeforces Round #318 (Div. 2) D Bear and Blocks

不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k == min(hi-j+j,hi+j+j)时hi会变成0,因为min具有传递性,那么可以左右分开来考虑,hi-j+j化一下就是hi-j-(i-j)+i,其中后面i和j无关,所以只要找出前面最小的hi-i 对于右边的类似处理,最后再扫一遍,得出最大的一个k就是答案. #include<bits/std

在青岛穷游打的cf codeforces Round #318 (Div. 2) A.Bear and Elections

这场cf是暑假集训后在青岛旅游打的一场,好累..... 题意:给出一个序列,要使a[1]大于a[2]~a[n],a[1]每次可以增加一,这个一从a[2]到a[[n]里面任意一个数减一扣除,求最少的步数 思路:要不断地修改值,并从中找出最大的,可以想到优先队列,还要保存下该数的编号,要知道在队首时是a[1].还有处里一种特殊情况,详见代码 总结:这道题并不难,我用了40多分钟,主要前面太急了,一开始并没有想到是优先队列,是一些其他的想法,只要合适一个样例就下手去做,导致有很明显的bug我竟然敲完代

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)

Limak is a grizzly bear who desires power and adoration. He wants to win in upcoming elections and rule over the Bearland. There are n candidates, including Limak. We know how many citizens are going to vote for each candidate. Now i-th candidate wou

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)

Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Each player can

Codeforces Round #318 (Div. 2) B Bear and Three Musketeers

不要想多了直接暴.对于u枚举a和b,判断一个是否连边,更新答案. #include<bits/stdc++.h> using namespace std; int n,m; const int maxn = 4001; #define PB push_back vector<int> G[maxn]; bool g[maxn][maxn]; int deg[maxn]; const int INF = 0x3f3f3f3f; int main() { //freopen("

Codeforces Round #318 (Div. 2) A Bear and Elections

优先队列模拟一下就好. #include<bits/stdc++.h> using namespace std; priority_queue<int>q; int main() { int n; scanf("%d",&n); int t; scanf("%d",&t); for(int i = 2; i <= n; i++){ int v; scanf("%d",&v); q.push(v

Codeforces Round #318 (Div. 2) C Bear and Poker

很简单,求一下所有数的2和3的幂是任意调整的,把2和3的因子除掉以后必须相等. #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll a[maxn]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(i

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)——A二分——Bear and Elections

/*贪心WA一发..二分枚举加的数赛后发现直接暴力枚举1到1000也是可以的*//************************************************ * Author :Powatr * Created Time :2015-8-30 0:58:45 * File Name :A.cpp ************************************************/ #include <cstdio> #include <algorith