codeforces-729【思维】【二分】

本文介绍了一场编程竞赛中的四个问题,包括去除采访记录中的特定词汇、在舞台平面图中找到放置聚光灯的最佳位置、规划到达电影院的最优路线以及一维海战游戏中的策略。通过解析问题并提供解决方案,展示了算法设计和实现的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

A. Interview with Oleg
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters.

There is a filler word ogo in Oleg's speech. All words that can be obtained from ogo by adding go several times to the end of it are also considered to be fillers. For example, the words ogoogogoogogogo are fillers, but the words googogogogogog and oggo are not fillers.

The fillers have maximal size, for example, for ogogoo speech we can't consider ogo a filler and goo as a normal phrase. We should consider ogogo as a filler here.

To print the interview, Polycarp has to replace each of the fillers with three asterisks. Note that a filler word is replaced with exactly three asterisks regardless of its length.

Polycarp has dealt with this problem in no time. Can you do the same? The clock is ticking!

Input

The first line contains a positive integer n (1 ≤ n ≤ 100) — the length of the interview.

The second line contains the string s of length n, consisting of lowercase English letters.

Output

Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences.

Examples
input
7
aogogob
output
a***b
input
13
ogogmgogogogo
output
***gmg***
input
9
ogoogoogo
output
*********
Note

The first sample contains one filler word ogogo, so the interview for printing is "a***b".

The second sample contains two fillers ogo and ogogogo. Thus, the interview is transformed to "***gmg***".


思路:先判断 "ogo" 然后找到后续有几个 "go"

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char str[110];
int main()
{
	while(~scanf("%d",&n))
	{
		scanf("%s",str);
		for(int i=0;str[i];i++)
		{
			if(i+2<n&&str[i]=='o'&&str[i+1]=='g'&&str[i+2]=='o')
			{
				while(str[i+1]=='g'&&str[i+2]=='o')
				{
					i+=2;
				}
				printf("***");
			}
			else
				printf("%c",str[i]);
		}
		puts("");
	}
	return 0;
}

题目链接: 点击打开链接

B. Spotlights
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.

You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.

A position is good if two conditions hold:

  • there is no actor in the cell the spotlight is placed to;
  • there is at least one actor in the direction the spotlight projects.

Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

Input

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.

The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Output

Print one integer — the number of good positions for placing the spotlight.

Examples
input
2 4
0 1 0 0
1 0 1 0
output
9
input
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
output
20
Note

In the first example the following positions are good:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.


大意:1 代表演员,然后要在 0 处放聚光灯,聚光灯只能照一个方向,要保证放聚光灯地方的四个方向(上下左右)至少有一个方向有个演员。

思路:其实就是判断 0 所在的行或列上是否 1。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
bool map[1010][1010];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
				scanf("%d",&map[i][j]);
		}
		long long ans=0;
		for(int i=1;i<=n;i++) // 左边 
		{
			bool flag=0;
			for(int j=1;j<=m;j++)
			{
				if(map[i][j])
					flag=1;
				if(!map[i][j]&&flag)
					ans++;
			}
		}
		for(int i=1;i<=n;i++) // 右边 
		{
			bool flag=0;
			for(int j=m;j>=1;j--)
			{
				if(map[i][j])
					flag=1;
				if(!map[i][j]&&flag)
					ans++;
			}
		}
		for(int j=1;j<=m;j++) // 上边 
		{
			bool flag=0;
			for(int i=1;i<=n;i++)
			{
				if(map[i][j])
					flag=1;
				if(!map[i][j]&&flag)
					ans++;
			}
		}
		for(int j=1;j<=m;j++) // 下边 
		{
			bool flag=0;
			for(int i=n;i>=1;i--)
			{
				if(map[i][j])
					flag=1;
				if(!map[i][j]&&flag)
					ans++;
			}
		}
		printf("%I64d\n",ans);
	}
	return 0;
}

题目链接:点击打开链接

C. Road to Cinema
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in t minutes. There is a straight road of length s from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point0, and the cinema is at the point s.

There are k gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly.

There are n cars in the rental service, i-th of them is characterized with two integers ci and vi — the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity vi. All cars are completely fueled at the car rental service.

Each of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times.

Your task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in tminutes. Assume that all cars are completely fueled initially.

Input

The first line contains four positive integers nks and t (1 ≤ n ≤ 2·1051 ≤ k ≤ 2·1052 ≤ s ≤ 1091 ≤ t ≤ 2·109) — the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts.

Each of the next n lines contains two positive integers ci and vi (1 ≤ ci, vi ≤ 109) — the price of the i-th car and its fuel tank capacity.

The next line contains k distinct integers g1, g2, ..., gk (1 ≤ gi ≤ s - 1) — the positions of the gas stations on the road in arbitrary order.

Output

Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in t minutes). If there is no appropriate car, print -1.

Examples
input
3 1 8 10
10 8
5 7
11 9
3
output
10
input
2 2 10 18
10 4
20 6
5 3
output
20
Note

In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel.


大意:某人在起点处要去看电影,到终点的距离为s。 汽车租赁公司提供n中车型,每种车型有属性 ci(租车费用), vi(油箱容量)。 车子有两种前进方式 :①. 慢速:1km 消耗 1L 汽油,花费 2 分钟。 ②.快速:1km 消耗 2L 汽油,花费 1 分钟。 路上有 k 个加油站,加油不需要花费时间,且直接给油箱加满。 问在 t 分钟内到达终点的最小花费是多少?(租车子的费用)  若无法到达终点,输出-1

题解: 
我们先分析不能到达终点的情况:
①.油箱容量最大的汽车在一个加油站到下一个加油站的途中油量耗尽了。(也就是慢速情况下都过不去)
②.全程用快速跑,也不能在t时间内到达终点。

我们可以先求出在 t 时间内到达终点的最小油箱容量,再在大于等于这个油箱容量中找租车费用最小的车子。 找最小油箱容量用二分查找就行了。 然后再判定不能到到达终点的情况就行了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int MAXN=2e5+10;
const int INF=0x3f3f3f3f;
int n,k,s,t;
int c[MAXN],v[MAXN],g[MAXN];
/**************************************

这里建立方程组:设 cost1、cost2 分别为快速、慢速过程中耗费的油量
	cost1 + cost2 = mid ; mid为油箱容量 
	cost1/2 +cost2 = dis ; dis 为两个加油站之间距离 
	
	解得: cost1 = mid - dis >> 1 ; 
		   cost2 = mid -cost1 ;
		   
**************************************/ 
bool judge(LL x)
{
	LL time=0;
	for(int i=1;i<=k+1;i++) // k+1 是因为又加入的终点 
	{
		LL dis=g[i]-g[i-1]; // 两个加油站的距离 
		if(x<dis) // 如果慢速条件下都过不去就 GG 
			return 0;
		LL cost1=x-dis<<1; // 上述条件可知 cost1 肯定大于 0 
		LL cost2=x-cost1;
		if(cost2<0) // 能够用快速通过这段距离
			time+=dis;
		else
			time+=cost1/2+cost2*2;
	}
	return time<=t;
}
int main()
{
	while(~scanf("%d%d%d%d",&n,&k,&s,&t))
	{
		for(int i=0;i<n;i++)
			scanf("%d%d",&c[i],&v[i]);	
		for(int i=1;i<=k;i++)
			scanf("%d",&g[i]);
		g[0]=0;	g[k+1]=s; // 加入起点、终点 
		sort(g,g+k+2); // 题中给出的加油站是 乱序 
		LL tp=-1,mid,l=0,r=2*s;
		while(l<=r)
		{
			mid=l+r>>1;
			if(judge(mid))
			{
				tp=mid;
				r=mid-1;
			}
			else
			{
				l=mid+1;	
			}
		}
		if(tp==-1) // 没有合法的解 
		{
			puts("-1");
			continue;
		}
		int ans=INF;
		for(int i=0;i<n;i++)
		{
			if(v[i]>=tp)
				ans=min(ans,c[i]);
		}
		if(ans==INF)
			puts("-1");
		else
			printf("%d\n",ans);
	}
	return 0;
}

题目链接:点击打开链接

D. Sea Battle
time limit per test
 1 second
memory limit per test
 256 megabytes
input
 standard input
output
 standard output

Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of bconsecutive cells. No cell can be part of two ships, however, the ships can touch each other.

Galya doesn't know the ships location. She can shoot to some cells and after each shot she is told if that cell was a part of some ship (this case is called "hit") or not (this case is called "miss").

Galya has already made k shots, all of them were misses.

Your task is to calculate the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.

It is guaranteed that there is at least one valid ships placement.

Input

The first line contains four positive integers nabk (1 ≤ n ≤ 2·1051 ≤ a, b ≤ n0 ≤ k ≤ n - 1) — the length of the grid, the number of ships on the grid, the length of each ship and the number of shots Galya has already made.

The second line contains a string of length n, consisting of zeros and ones. If the i-th character is one, Galya has already made a shot to this cell. Otherwise, she hasn't. It is guaranteed that there are exactly k ones in this string.

Output

In the first line print the minimum number of cells such that if Galya shoot at all of them, she would hit at least one ship.

In the second line print the cells Galya should shoot at.

Each cell should be printed exactly once. You can print the cells in arbitrary order. The cells are numbered from 1 to n, starting from the left.

If there are multiple answers, you can print any of them.

Examples
input
5 1 2 1
00100
output
2
4 2
input
13 3 2 3
1000000010001
output
2
7 11
Note

There is one ship in the first sample. It can be either to the left or to the right from the shot Galya has already made (the "1" character). So, it is necessary to make two shots: one at the left part, and one at the right part.



题意:有n个位置,这里面包含a条船,每条船占b个位置,不知道船的位置。Galya 之前射击过k次,k次都没有打中船。 给出n长度的字符串,0表示未知位置,1表示被Galya射击过的没有船的位置。问要保证 Galya至少射击中一条船,需要再射击几次,并输出这些位置编号。


题解:先算出可能存在船的位置数num,并且记录下每条船的最后一个位置编号。 然后我们需要射击这些船,使存在船的位置变成a-1就行了。(保证能射击到一条船) 位置输出前 num+1-a 个就行了。


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=2e5+10;
int n,a,b,k;
char str[MAXN];
int pos[MAXN]; // 船可能存在的位置 
int main()
{
	while(~scanf("%d%d%d%d",&n,&a,&b,&k))
	{
		scanf("%s",str+1);
		int cnt=0,k=0;
		for(int i=1;i<=n;i++)
		{
			if(str[i]=='0')
			{
				cnt++;
				if(cnt==b)
				{
					pos[++k]=i;
					cnt=0;
				}
			}
			else
				cnt=0;
		}
		printf("%d\n",k-a+1); // k-a 是打不到船的最坏结果,+1 是为了能打到船 
		for(int i=1;i<=k-a+1;i++)
			printf(i==1?"%d":" %d",pos[i]);
		puts("");
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值