当前位置: 代码迷 >> 综合 >> Codeforces Round #687 (Div. 2) C. Bouncing Ball(枚举 思维)
  详细解决方案

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

热度:84   发布时间:2023-12-21 00:08:56.0

题目链接: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;
}
  相关解决方案