Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2)(详细题意+思路 A-C题)

A. Prison Break

There is a prison that can be represented as a rectangular matrix with n rows and m columns. Therefore, there are n⋅m prison cells. There are also n⋅m prisoners, one in each prison cell. Let’s denote the cell in the i-th row and the j-th column as (i,j).

There’s a secret tunnel in the cell (r,c), that the prisoners will use to escape! However, to avoid the risk of getting caught, they will escape at night.

Before the night, every prisoner is in his own cell. When night comes, they can start moving to adjacent cells. Formally, in one second, a prisoner located in cell (i,j) can move to cells (i−1,j) , (i+1,j) , (i,j−1) , or (i,j+1), as long as the target cell is inside the prison. They can also choose to stay in cell (i,j).

The prisoners want to know the minimum number of seconds needed so that every prisoner can arrive to cell (r,c) if they move optimally. Note that there can be any number of prisoners in the same cell at the same time.

Input
The first line contains an integer t (1≤t≤104), the number of test cases.

Each of the next t lines contains four space-separated integers n, m, r, c (1≤r≤n≤109, 1≤c≤m≤109).

Output
Print t lines, the answers for each test case.

Example
inputCopy
3
10 10 1 1
3 5 2 4
10 2 5 1
outputCopy
18
4
6

题意: 有一个n*m的监狱,每个房间里有一个罪犯,告诉你一个房间号x,y,这个房间里有个通道可以逃出监狱,每个罪犯在一秒内可以向上下左右任意一个方向走一步,问所有罪犯成功逃脱最少要多久。
思路: 我们只要算离逃脱点最远的房间就行了,同时我们发现,最远的房间有四个可能,就是监狱的四角,所以我们只要算四角逃脱的时间然后比较一下,输出最小的就好了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int t;
	cin>>t;
	while(t--){
		ll n,m,x,y;
		cin>>n>>m>>x>>y;
		ll a1=abs(1-x)+abs(1-y);
		ll a2=abs(n-x)+abs(1-y);
		ll a3=abs(1-x)+abs(m-y);
		ll a4=abs(n-x)+abs(m-y);
		printf("%lld\n",max(max(a1,a2),max(a3,a4)));
	}
	return 0;
 } 

B. Repainting Street

There is a street with n houses in a line, numbered from 1 to n. The house i is initially painted in color ci. The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom’s painting capacity is defined by an integer, let’s call it k.

On one day, Tom can do the following repainting process that consists of two steps:

He chooses two integers l and r such that 1≤l≤r≤n and r−l+1=k.
For each house i such that l≤i≤r, he can either repaint it with any color he wants, or ignore it and let it keep its current color.
Note that in the same day Tom can use different colors to repaint different houses.

Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful.

Input
The first line of input contains a single integer t (1≤t≤104), the number of test cases. Description of test cases follows.

In the first line of a test case description there are two integers n and k (1≤k≤n≤105).

The second line contains n space-separated integers. The i-th of these integers represents ci (1≤ci≤100), the color which house i is initially painted in.

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output
Print t lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case.

Example
inputCopy
3
10 2
1 1 2 2 1 1 2 2 2 1
7 1
1 2 3 4 5 6 7
10 3
1 3 3 3 3 1 2 1 3 3
outputCopy
3
6
2
Note
In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day.

In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3.

In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3.

题意: 有一个数组n,每个位置有不同的数字,给你一个范围k,你每次可以把k范围的数变为你想要的数,问最少要几次才能把所有的数变成一样的。

思路: 因为数据范围很小,一共就100种颜色,所以我们就把每种出现过的颜色都尝试一遍,看看哪种比较小就行了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[100007];
int vis[1000];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d%d",&n,&k);
		for(int i=1;i<=100;i++){
			vis[i]=0;
		}
		for(int i=1;i<=n;i++){
			cin>>a[i];
			vis[a[i]]++;
		}
		int minn=n/k;
		if(n%k!=0)minn++;
		for(int i=1;i<=100;i++){
			if(vis[i]!=0){
				int cnt=0;
				for(int j=1;j<=n;j++){
					if(a[j]!=i){
						cnt++;
						j=j+k-1;
					}
				}
				minn=min(minn,cnt);
			}
		}
		printf("%d\n",minn);
	}
	return 0;
 } 

C. Bouncing Ball

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
inputCopy
3
10 3 2
0101010101
2 2
5 4 1
00000
2 10
11 2 3
10110011000
4 3
outputCopy
2
4
10
Note
In the first test case it’s best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is y=2.

In the second test case it’s best to add a platform to both cells 4 and 5: 00000 → 00011. The time required is x⋅2=4.

In the third test case it’s best to to remove the first cell twice and then add a platform to the cell which was initially 10-th: 10110011000 → 10110011010. The time required is y⋅2+x=10.

题意: n个区域,每个区域可能有平台,你只能弹到有平台的地方。你从最左边抛出一个球,抛到位置p,之后每弹一次往后k个区域。你有两个操作,一个是在一个没有平台的地方放一个平台,耗时x,一个是删去最左边的区域(这里不要看错了,我就是看成了删去任意一个,然后裂开了),同时更新后面所有区域的编号。耗时y。现在要求你花最少的时间,使球能弹过这整个区域。

思路: 我们发现,只有在n到n-k+1的地方的球才有机会弹出去。所以我们就反向遍历,从能弹出去的点往回遍历,每往回弹一次,算一下结果,就是前面留了p的距离后其他的都删去要多少时间,在加上后面填的平台要多少时间。这样我们就遍历了所有的情况。我们只要比较每一次的答案,然后输出最小的时间就好了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char a[100007];
int vis[100007];
int shi[100007];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n,p,pp,k;
		scanf("%d%d%d",&n,&p,&k);
		scanf("%s",a+1);
		int x,y;
		scanf("%d%d",&x,&y);
		ll sum=0,ans=1e18;
		for(ll i=n-k+1;i<=n;i++){
			sum=0;
			for(ll j=i;j>=1;j-=k){
					if(a[j]=='0'){
						sum+=x;
					}
					ll e=j-p;
					if(e<0)break;
					e=e*y+sum;
					ans=min(e,ans);
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值