题目链接:点击打开链接
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 ogo, ogogo, ogogogo are fillers, but the words go, og, ogog, ogogog 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!
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.
Print the interview text after the replacement of each of the fillers with "***". It is allowed for the substring "***" to have several consecutive occurences.
7 aogogob
a***b
13 ogogmgogogogo
***gmg***
9 ogoogoogo
*********
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;
}
题目链接: 点击打开链接
思路:其实就是判断 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;
}
题目链接:点击打开链接
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.
The first line contains four positive integers n, k, s and t (1 ≤ n ≤ 2·105, 1 ≤ k ≤ 2·105, 2 ≤ s ≤ 109, 1 ≤ 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.
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.
3 1 8 10 10 8 5 7 11 9 3
10
2 2 10 18 10 4 20 6 5 3
20
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;
}
题目链接:点击打开链接
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.
The first line contains four positive integers n, a, b, k (1 ≤ n ≤ 2·105, 1 ≤ a, b ≤ n, 0 ≤ 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.
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.
5 1 2 1 00100
2 4 2
13 3 2 3 1000000010001
2 7 11
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;
}