Codeforces Round #363 (Div. 2)

A:

A. Launch of Collider
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. n particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, xi is the coordinate of the i-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers.

You know the direction of each particle movement — it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time.

Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point.

Input

The first line contains the positive integer n (1 ≤ n ≤ 200 000) — the number of particles.

The second line contains n symbols "L" and "R". If the i-th symbol equals "L", then the i-th particle will move to the left, otherwise the i-th symbol equals "R" and the i-th particle will move to the right.

The third line contains the sequence of pairwise distinct even integers x1, x2, ..., xn (0 ≤ xi ≤ 109) — the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order.

Output

In the first line print the only integer — the first moment (in microseconds) when two particles are at the same point and there will be an explosion.

Print the only integer -1, if the collision of particles doesn't happen.

Examples
Input
4
RLRL
2 4 6 10
Output
1
Input
3
LLR
40 50 60
Output
-1
Note

In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3.

In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point.

题意:

n个人,每个人的速度相同,给出方向和初始位置问经过几秒有人相遇,没有输出-1.

题解:

因为每个人的速度相同,所以能够相遇的情况只能是前一个人向右(R)走,后一个人向左(L)走。判断这种情况即可。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define M 200005
#define inf 1000000007
using namespace std;
char a[M];
int b[M];
int main()
{
    int t;
  cin>>t;
        int mx=inf;
           scanf("%s",a);
        for(int i=0;i<t;i++)
            scanf("%d",&b[i]);
            bool flag=false;
        for(int i=0;i<t;i++)
            if(a[i]=='R')
                flag=true;
            else
            {
                if(flag)
                    mx=min(mx,(b[i]-b[i-1])/2);
                    flag=false;
            }
            if(mx!=inf)
                cout<<mx<<endl;
            else
                cout<<-1<<endl;
    }
B:

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

You are given a description of a depot. It is a rectangular checkered field of n × m size. Each cell in a field can be empty (".") or it can be occupied by a wall ("*").

You have one bomb. If you lay the bomb at the cell (x, y), then after triggering it will wipe out all walls in the row x and all walls in the column y.

You are to determine if it is possible to wipe out all walls in the depot by placing and triggering exactly one bomb. The bomb can be laid both in an empty cell or in a cell occupied by a wall.

Input

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

The next n lines contain m symbols "." and "*" each — the description of the field. j-th symbol in i-th of them stands for cell (i, j). If the symbol is equal to ".", then the corresponding cell is empty, otherwise it equals "*" and the corresponding cell is occupied by a wall.

Output

If it is impossible to wipe out all walls by placing and triggering exactly one bomb, then print "NO" in the first line (without quotes).

Otherwise print "YES" (without quotes) in the first line and two integers in the second line — the coordinates of the cell at which the bomb should be laid. If there are multiple answers, print any of them.

Examples
Input
3 4
.*..
....
.*..
Output
YES
1 2
Input
3 3
..*
.*.
*..
Output
NO
Input
6 5
..*..
..*..
*****
..*..
..*..
..*..
Output
YES
3 3
题意;给出一个图,*是墙,炸弹可以炸掉它所在行所在列的墙,问 你给你一个炸弹,是否能炸掉所有的墙,如果能输出YES,并且输出炸弹位置,如果有多组解,任意一组即可,不能输出NO。

题解:

首先统计每行每列和总的墙个数,遍历判断,如果该位置放炸弹可以炸掉所有的墙,输出。注意,当该位置本身是墙时,行列和会多算一次。

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <stack>
#define mem(a) memset(a, 0, sizeof(a))
#define eps 1e-5
#define M 200005
#define inf 1000000007
using namespace std;
char ap[1001][1001];
int r[1001];
int l[1001];
int x,y,k;
int main()
{
    scanf("%d%d",&x,&y);
    mem(r);
    mem(l);
    k=0;
    for(int i=1; i<=x; i++)
    {
        scanf("%s",ap[i]+1);
    }
    for(int i=1; i<=x; i++)
    {
        for(int j=1; j<=y; j++)
        {
            if(ap[i][j]=='*')
            {
                r[i]++;
                l[j]++;
                k++;
            }
        }
    }
    for(int i=1; i<=x; i++)
    {
        for(int j=1; j<=y; j++)
        {
            if(r[i]+l[j]+(ap[i][j]=='*'?-1:0)==k)
            {
                puts("YES");
                printf("%d %d\n",i,j);
                return 0;
            }
        }
    }

    puts("NO");
    return 0;
}
C:

C. Vacations
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasya knows the following information about each of this n days: whether that gym opened and whether a contest was carried out in the Internet on that day. For the i-th day there are four options:

  1. on this day the gym is closed and the contest is not carried out;
  2. on this day the gym is closed and the contest is carried out;
  3. on this day the gym is open and the contest is not carried out;
  4. on this day the gym is open and the contest is carried out.

On each of days Vasya can either have a rest or write the contest (if it is carried out on this day), or do sport (if the gym is open on this day).

Find the minimum number of days on which Vasya will have a rest (it means, he will not do sport and write the contest at the same time). The only limitation that Vasya has — he does not want to do the same activity on two consecutive days: it means, he will not do sport on two consecutive days, and write the contest on two consecutive days.

Input

The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of days of Vasya's vacations.

The second line contains the sequence of integers a1, a2, ..., an (0 ≤ ai ≤ 3) separated by space, where:

  • ai equals 0, if on the i-th day of vacations the gym is closed and the contest is not carried out;
  • ai equals 1, if on the i-th day of vacations the gym is closed, but the contest is carried out;
  • ai equals 2, if on the i-th day of vacations the gym is open and the contest is not carried out;
  • ai equals 3, if on the i-th day of vacations the gym is open and the contest is carried out.
Output

Print the minimum possible number of days on which Vasya will have a rest. Remember that Vasya refuses:

  • to do sport on any two consecutive days,
  • to write the contest on any two consecutive days.
Examples
Input
4
1 3 2 0
Output
2
Input
7
1 3 3 2 1 2 3
Output
0
Input
2
2 2
Output
1
Note

In the first test Vasya can write the contest on the day number 1 and do sport on the day number 3. Thus, he will have a rest for only 2 days.

In the second test Vasya should write contests on days number 1, 3, 5 and 7, in other days do sport. Thus, he will not have a rest for a single day.

In the third test Vasya can do sport either on a day number 1 or number 2. He can not do sport in two days, because it will be contrary to the his limitation. Thus, he will have a rest for only one day.

题意:

n天,每天可以比赛,锻炼,休息,但是相邻两天不可做同样事情(休息除外),给你n天的状态(0:体育场和比赛都没有。1:有比赛没体育场。2:有体育场没比赛。3:有体育场,有比赛。)问至少可以休息几天。

题解:

一看就是dp.

dp[i][j]表示第i天作J这件事(j:0代表休息,1代表比赛,2代表锻炼)休息了几天。

状态转移方程:

</pre><pre name="code" class="cpp">           if(s[i]==0)(
                {
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else if(s[i]==1)
                {
                    dp[i][1]=min(dp[i-1][1]+1,min(dp[i-1][0],dp[i-1][2]));
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else if(s[i]==2)
                {
                    dp[i][2]=min(dp[i-1][2]+1,min(dp[i-1][0],dp[i-1][1]));
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else
                {
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                       dp[i][1]=min(dp[i-1][1]+1,min(dp[i-1][0],dp[i-1][2]));
                     dp[i][2]=min(dp[i-1][2]+1,min(dp[i-1][0],dp[i-1][1]));
代码:

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <queue>
    #include <stack>
    #define mem(a) memset(a, 0, sizeof(a))
    #define eps 1e-5
    #define M 200005
    #define inf 1000000007
    using namespace std;
    int s[105];
    int dp[105][3];
    int main()
    {
            int n;
            cin>>n;
            for(int i=1;i<=n;i++)
            {
                cin>>s[i];
                for(int j=0;j<3;j++)
                    dp[i][j]=inf;
            }
            for(int i=1;i<=n;i++)
            {
                if(s[i]==0)
                {
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else if(s[i]==1)
                {
                    dp[i][1]=min(dp[i-1][1]+1,min(dp[i-1][0],dp[i-1][2]));
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else if(s[i]==2)
                {
                    dp[i][2]=min(dp[i-1][2]+1,min(dp[i-1][0],dp[i-1][1]));
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                }
                else
                {
                    dp[i][0]=min(min(dp[i-1][0],dp[i-1][1]),dp[i-1][2])+1;
                       dp[i][1]=min(dp[i-1][1]+1,min(dp[i-1][0],dp[i-1][2]));
                     dp[i][2]=min(dp[i-1][2]+1,min(dp[i-1][0],dp[i-1][1]));

                }
            }
            printf("%d\n",min(dp[n][0],min(dp[n][1],dp[n][2])));
            return 0;
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值