Codeforces Round #687 (Div. 2) C. Bouncing Ball(枚举 思维)

2023-10-30 04:08

本文主要是介绍Codeforces Round #687 (Div. 2) C. Bouncing Ball(枚举 思维),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接:https://codeforc.es/contest/1457/problem/C

You’re creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from 1, and in each cell you can either put a platform or leave it empty.

In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell p, then bounces off it, then bounces off a platform in the cell (p+k), then a platform in the cell (p+2k), and so on every k-th platform until it goes farther than the last cell. If any of these cells has no platform, you can’t pass the level with these p and k.

You already have some level pattern a1, a2, a3, …, an, where ai=0 means there is no platform in the cell i, and ai=1 means there is one. You want to modify it so that the level can be passed with given p and k. In x seconds you can add a platform in some empty cell. In y seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can’t do any other operation. You can not reduce the number of cells to less than p.

Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added.
What is the minimum number of seconds you need to make this level passable with given p and k?

Input
The first line contains the number of test cases t (1≤t≤100). Description of test cases follows.

The first line of each test case contains three integers n, p, and k (1≤p≤n≤105, 1≤k≤n) — the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required.

The second line of each test case contains a string a1a2a3…an (ai=0 or ai=1) — the initial pattern written without spaces.

The last line of each test case contains two integers x and y (1≤x,y≤104) — the time required to add a platform and to remove the first cell correspondingly.

The sum of n over test cases does not exceed 105.

Output
For each test case output a single integer — the minimum number of seconds you need to modify the level accordingly.

It can be shown that it is always possible to make the level passable.

Example

input

3
10 3 2
0101010101
2 2
5 4 1
00000
2 10
11 2 3
10110011000
4 3

output

2
4
10

分析

先想一下要跳出去,最后一步是踩在哪里,可以是在 n - k + 1 ~ n 的位置上;那么又是如何到达最后一步的呢,可以是从倒数第二步 x 跳过来,或者是直接从起点跳到这一步。使得能够从起点跳到这一步的条件是什么,要将 x - p 及以前的所有点都搬空。这样我们就能知道花费是多少。然后一直遍历到不能搬为止。
我们只要遍历一遍 n - k + 1 ~ n ,然后从这个位置向前枚举所有情况,过程中一直更新 ans 就行了。

代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int t;
int n,p,k,x,y;
char a[100007];int main()
{scanf("%d",&t);while(t--){ll ans = 1e18;scanf("%d%d%d",&n,&p,&k);scanf("%s",a+1);scanf("%d%d",&x,&y);for(ll begin=n-k+1;begin<=n;begin++){ll tmp = 0;for(ll j=begin;j>=1;j-=k){if(a[j] == '0') tmp += x;ll t = j - p;if(t < 0) break;t = t * y + tmp;ans = min(ans, t);}}printf("%lld\n",ans);}return 0;
}

这篇关于Codeforces Round #687 (Div. 2) C. Bouncing Ball(枚举 思维)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/305486

相关文章

C#实现获得某个枚举的所有名称

《C#实现获得某个枚举的所有名称》这篇文章主要为大家详细介绍了C#如何实现获得某个枚举的所有名称,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... C#中获得某个枚举的所有名称using System;using System.Collections.Generic;usi

Java 枚举的常用技巧汇总

《Java枚举的常用技巧汇总》在Java中,枚举类型是一种特殊的数据类型,允许定义一组固定的常量,默认情况下,toString方法返回枚举常量的名称,本文提供了一个完整的代码示例,展示了如何在Jav... 目录一、枚举的基本概念1. 什么是枚举?2. 基本枚举示例3. 枚举的优势二、枚举的高级用法1. 枚举

Rust中的Option枚举快速入门教程

《Rust中的Option枚举快速入门教程》Rust中的Option枚举用于表示可能不存在的值,提供了多种方法来处理这些值,避免了空指针异常,文章介绍了Option的定义、常见方法、使用场景以及注意事... 目录引言Option介绍Option的常见方法Option使用场景场景一:函数返回可能不存在的值场景

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

hdu 6198 dfs枚举找规律+矩阵乘法

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description We define a sequence  F : ⋅   F0=0,F1=1 ; ⋅   Fn=Fn