Leveling Ground

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4925

Description

It is important to first level the ground before you build anything on top of it (e.g., new house, shed,

swimming pool, driveway, etc.), especially when there are hills on the land, otherwise whatever you

built will not be stable. In case you don’t understand, “leveling the ground” means making the ground

flat and even (having the same height). In this problem, you are given a land description and the

length of land — M — that you want to level; your task is to determine the minimum amount of land

you should dispose in order to have a level land of length M. Note that in this problem you are only

allowed to dispose land, not filling it.

The total length of the given land will be N, and the land will be encoded with the following format:

(1) / means ascending slope (disposing an ascending slope cost 0.5),

(2) \ means descending slope (disposing a descending slope cost 0.5),

(3) means flat (disposing a flat land cost 0),

(4) . means full land (disposing a full land cost 1).

Note that the input will only describe the land’s surface, thus (4) will not appear in any input. Also

note that (1) and (2) are not level.

For example, consider the following input.

Input : //_//_\_////\/

The input corresponds to the following land (which length is 31).

__ /_ __

/.._/...\ /..\ _

Land : /..........\ ___ /...._
/.

............_/.../...........

...............................

Index : 1234567890123456789012345678901

Supposed we want to level a land of length M = 7, and for some reasons, we choose the land we

want to level to be at index [11, 17]. Recall that you are only allowed to dispose land, thus if you want

to level the land at [11, 17], you should level it such that the height is equal to the height of land at

index 14 (because it is the lowest point). In the following figure, ‘’ (stars) mark the land which should

be disposed.

__ /_ __ __ /_ __

/.._/...
/..\ _ /.._/...| /..\ _

/.........** **_ /...._
/. /.........| _ /...._/.

..........*******./........... ..........|
____|./...........

............................... ...............................

Index : 1234567890123456789012345678901 1234567890123456789012345678901

If you observe, there are 12 stars in the left figure, they are:

• 1 ascending slope (at index: 15),

• 3 descending slopes (at indexes: 11, 12, and 13),

• 3 flat lands (at indexes: 14, 16, and 17), and

• 5 full lands (2 at index 11, 1 at index 12, 1 at index 16, and another 1 at index 17).

Therefore, the cost of leveling [11, 17] is: 1 * 0.5 + 3 * 0.5 + 3 * 0 + 5 * 1 = 7.

In this example, [11, 17] is not the best choice, you can do better.

Input

The first line of input contains T (T ≤ 50) denoting the number of cases. Each case begins with two

integers N and M (1 ≤ M ≤ N ≤ 1, 000, 000) denoting the total length of the land and the length of the

land which should be leveled respectively. The following line contains a string of length N describing

the land’s surface. The string will only contain character ‘/’, ‘\’, or ‘ ’, as described in the problem

statement.

Output

For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the minimum

amount of land which should be disposed to achieve a level land which length is M for that particular

case. Output this number with exactly one digit after the decimal point.

Explanation for 1st sample case:

This is the same case as the example in the problem statement. The minimum amount of land

which you should dispose is 3.5. You can achieve this by leveling lands at [25, 31].

__ /_ __ __ /_ __

/.._/...\ /..\ _ /.._/...\ /..* *

Land : /..........\ ___ /....___/. /..........\ ___ /...*******

............_/.../........... ............_/.../...........

............................... ...............................

Index : 1234567890123456789012345678901 1234567890123456789012345678901

You will dispose: 1 ascending slope (at index 30), 2 descending slopes (at index 15 and 16), 4 flat

lands (at index 27, 28, 29, and 31), and 2 full lands (at index 15 and 31). Therefore the total cost will

be: 1 * 0.5 + 2 * 0.5 + 4 * 0 + 2 * 1 = 3.5.

Explanation for 2nd sample case:

If you level the land at [3, 6] or [4, 7], you don’t need to dispose any land as they are already level

(have the same height).

Explanation for 3rd sample case:

Level the land at [8, 11], and you only need to dispose 1 ascending slope and 1 descending slope.

Sample Input

4

31 7

//_//_\_////\/

10 4

//
____\/

12 4

\\///_

12 1

//////\

Sample Output

Case #1: 3.5

Case #2: 0.0

Case #3: 1.0

Case #4: 0.5

Hint

题意

给你一个类似山峰的东西,你可以使得一个连续的m长度的山峰变成这一块的最低值。

然后问你最小的花费是多少。

(题意还是比较烦的,自己读读吧,我说不是很清楚……

题解:

考虑滑块,我们维护区间和,和区间最小值,那么花费就是区间和减去区间最小值乘以这个区间的大小就好了。

然后我们类似滑块去维护就好了。

O(n)就用单调队列去维护最小值,前缀和维护区间和就行了。

nlogn的做法就相当多了……

代码

#include<bits/stdc++.h>
#define two(x) (1<<(x))
using namespace std;
const int maxn = 1e6+7;
int a[maxn],b[maxn];
char s[maxn];
int mm[maxn];
int c[maxn][21];
int two[maxn];
void initrmp(int n)
{
mm[0]=-1;
for(int i=1;i<=n;i++){
mm[i]=((i&(i-1))==0)?mm[i-1]+1:mm[i-1];
}
} int query(int l,int r){
int k = mm[r-l+1];
return min(c[l][k],c[r-(1<<k)+1][k]);
} int cas = 0;
void solve(){
int n,m;
scanf("%d%d",&n,&m);
scanf("%s",s+1);
initrmp(n);
int now = 0;
for(int i=1;i<=n;i++){
if(s[i]=='/')a[i]=now,b[i]=1,now++;
if(s[i]=='\\')now--,a[i]=now,b[i]=1;
if(s[i]=='_')a[i]=now,b[i]=0;
c[i][0]=a[i];
}
for(int j=1;j<21;j++) for(int i = 1 ; i + ( 1 << j ) - 1 <= n ; ++ i) c[i][j]=min( c[i][j-1] , c[i + two(j-1)][j-1] );
long long sum = 0;
long long sum2 = 0;
for(int i=1;i<=m;i++){
sum+=1LL*a[i];
sum2+=1LL*b[i]; }
double Ans = 1e9;
Ans = 1.0*sum+0.5*sum2-1.0*m*query(1,m);
for(int i=m+1;i<=n;i++){
sum+=1LL*a[i]-1LL*a[i-m];
sum2+=1LL*b[i]-1LL*b[i-m];
Ans=min(Ans,1.0*sum+0.5*sum2-1.0*m*query(i-m+1,i));
}
printf("Case #%d: %.1f\n",++cas,Ans);
}
int main(){
//freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}

UVALive 6915 Leveling Ground 倍增RMQ的更多相关文章

  1. 【bzoj5073】[Lydsy1710月赛]小A的咒语 后缀数组+倍增RMQ+贪心+dp

    题目描述 给出 $A$ 串和 $B$ 串,从 $A$ 串中选出至多 $x$ 个互不重合的段,使得它们按照原顺序拼接后能够得到 $B$ 串.求是否可行.多组数据. $T\le 10$ ,$|A|,|B| ...

  2. hdu 5726 GCD 暴力倍增rmq

    GCD/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5726 Description Give you a sequence ...

  3. 【bzoj1067】[SCOI2007]降雨量 倍增RMQ

    题目描述 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2004和200 ...

  4. 【bzoj2006】[NOI2010]超级钢琴 倍增RMQ+STL-堆

    题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中A ...

  5. Leveling Ground(数论,三分法,堆)

    Leveling Ground(数论,三分法,堆) 给定n个数和a,b每次可以选择一段区间+a,-a,+b或-b,问最少操作几次能把他们都变成0.n<=1e5. 首先差分一下序列,问题就会变成了 ...

  6. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  7. 【bzoj3879】SvT 后缀数组+倍增RMQ+单调栈

    题目描述 (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置来表示), ...

  8. UVALive 6915 J - Leveling Ground

    思路: 简单模拟下.从左向右扫描一次,求出挖出该区间空地的花费,并取个最小值即可. 至于怎么求区间内的高度最小值,就用线段树就好了. #include <bits/stdc++.h> #d ...

  9. LCA算法解析-Tarjan&倍增&RMQ

    原文链接http://www.cnblogs.com/zhouzhendong/p/7256007.html UPD(2018-5-13) : 细节修改以及使用了Latex代码,公式更加美观.改的过程 ...

随机推荐

  1. Linux - 磁盘操作

    Linux 磁盘常见操作 : df -Ph # 查看硬盘容量 df -T # 查看磁盘分区格式 df -i # 查看inode节点 如果inode用满后无法创建文件 du -h 目录 # 检测目录下所 ...

  2. python之celery使用详解(二)

    前言 前面我们了解了celery的基本使用后,现在对其常用的对象和方法进行分析. Celery对象 核心的对象就是Celery了,初始化方法: class Celery(object): def __ ...

  3. 如何将IOS版本的更新下载文件指向到自己的服务器

    针对那些使用企业签名但是没有发布到AppSotre的IOS版本APP自动更新问题解决方案: 在apicloud中是这样说明的: 因为要填写plist地址所以不能向安卓那样直接填写服务器文件地址,但是直 ...

  4. win7下PHP+MySQL+CoreSeek中文检索引擎配置

    1.Windows下的coreseek安装测试 (64位win7旗舰版) 官方参考:http://www.coreseek.cn/products-install/install_on_windows ...

  5. centos7系统下安装配置jdk、tomcat教程

    JDK安装与配置 1.下载linux版本的jdk,我下的版本是jdk6.0,下载rpm版本的. 可通过百度搜索文件名:jdk-6u45-linux-x64-rpm.bin下载 也可通过oracle官网 ...

  6. 11 Go 1.11 Release Notes

    Go 1.11 Release Notes Introduction to Go 1.11 Changes to the language Ports WebAssembly RISC-V GOARC ...

  7. android蓝牙耳机下的语音(输入/识别)及按键监听

    背景:本人负责公司android平台的app开发,最近要开发一个语音助手类的app,类似于灵犀语音助手.虫洞语音助手等.其中有两个蓝牙耳机下的语音识别问题,比较折腾人,问题描述:1.蓝牙耳机连接下捕获 ...

  8. Java基础86 MySQL数据库基础知识

    本文知识点(目录): 1.MySQL数据库的概述    2.MySQL数据库的管理[对数据库的操作](查询.删除.创建数据库,以及查询和修改数据库的编码模式)    3.表的管理[对数据库 表的操作] ...

  9. mybatis-config.xml 模板

    ssm模板 <?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE configuration  ...

  10. UE没法远程修改文件

    UE没法远程修改文件修改ftp和sftp修改方式都没有作用,考虑可能是防火墙的作用,关闭防火墙可以.于是在控制面板->防火墙->修改策略中将UE的公用网络打开.