首页 > 其他 > 详细

中大周赛 2014年每周一赛第二场 Meet and Greet

时间:2014-03-09 17:42:56      阅读:591      评论:0      收藏:0      [点我收藏+]

10392. Meet and Greet

限制条件

时间限制: 1 秒, 内存限制: 256 兆

题目描述

As is commonly known, cows are very socially polite creatures: any time two cows meet after being apart, they greet each-other with a friendly "moo". Bessie the cow and her friend, Elsie, are walking around on a long path on Farmer John‘s farm. For all practical purposes, we can think of this path as a one-dimensional number line. Bessie and Elsie both start at the origin, and they both then begin walking around at identical speeds for some amount of time. Given a description of the movements taken by each cow, please determine the number of "moos" exchanged. Bessie and Elsie can stop moving at different points in time, and neither cow will travel for more than 1,000,000 units of time.

输入格式

* Line 1: Two space-separated integers, B (1 <= B <= 50,000) and E (1 <= E <= 50,000).

* Lines 2..1+B: These B lines describe Bessie‘s movements. Each line contains a positive integer followed by either "L" or "R", indicating the distance Bessie moves in a direction that is either left or right.

* Lines 2+B..1+B+E: These E lines describe Elsie‘s movements. Each line contains a positive integer followed by either "L" or "R", indicating the distance Elsie moves in a direction that is either left or right.

输出格式

* Line 1: An integer specifying the number of "moos" exchanged by the two cows. Their initial shared starting position at the origin does not cause a "moo".

样例输入

4 5
3 L
5 R
1 L
2 R
4 R
1 L
3 L
4 R
2 L

样例输出

3

提示

In the sample, Bessie moves left for 3 units of time, then right for 5 units of time, then left for 1 unit of time, and finally right for 2 units of time; she then stands still. Elsie moves right for 4 units of time, then left for 4 units of time, then right for 4 units of time, then left for 2 units of time; she then stands still. So Bessie and Elsie meet after being temporarily apart at time 7, time 9, and time 13.

题目来源

2014年每周一赛第二场

提交

这道题理解题意不难:就是有两头牛,大家都是从原点出发,在一维坐标上以相同的速度一起走,然后每一次碰到就会叫一声

问一共叫了多少声。。就是直接模拟,模拟每一个时刻的所在的坐标,判断他们是不是在这个时刻相遇。然后里面有很多坑,就是

一般做这种题要细心想出每一步的最简单的判断条件,我就是想得太复杂了,然后就是wa到我做噩梦。。

后来还是中大的doublehh神点醒我,其实每一步只要判断“上一秒如果站在一起的话,如果现在还在一起,不算答案加一”

就是说只要判断一下上一步和现在这一步就行了。

我因为改了很多,可能代码有点冗余。。。

还可以精简很多。但是我懒得改了

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<math.h>

using namespace std;

int a[2000005];
int o[2000005];
int work(int r,int k,int f)
{
    int ans = 0,i,j;
    if(f==1)
    {
        for(i=1;i<r;i++)
        {
          if(i<k && a[i]==o[i] && a[i-1]!=o[i-1])
          ans++;
          else if(i>=k && a[k-1]==o[i])
          ans++;
        }    
    }    
    else
    {
        for(i=1;i<k;i++)
        {
          if(i<r && a[i]==o[i] && a[i-1]!=o[i-1])
          ans++;
          else if(i>=r && a[i]==o[r-1])
          ans++;
        }    
    }    
    return ans;
}    
int main()
{
    int n,b,e,i,j,num;
    char dir[10];
    while(~scanf("%d%d",&b,&e))
    {
        
        int k = 1;
        a[0] = 0;
        for(i=0;i<b;i++)
        {
            scanf("%d %s",&num,dir);
            if(dir[0]==‘L‘)
            {
                num*=2;
                for(j=0;j<num;j++)
                {
                    a[k] = a[k-1]-1; 
                
                    k++;
                }
            }
            else
            {
                num*=2;
                for(j=0;j<num;j++)
                {
                    a[k] = a[k-1]+1;
                
                    k++;
                }
            }
            
        }
        int ans = 0,r = 1;
        o[0] = 0;
        for(i=0;i<e;i++)
        {
            scanf("%d %s",&num,dir);
            if(dir[0]==‘L‘)
            {
                num*=2;
                for(j=0;j<num;j++)
                {
                    o[r] = o[r-1]-1;
                    
                    r++;
                    
                }
            }
            else
            {
                num*=2;
                for(j=0;j<num;j++)
                {
                    o[r] = o[r-1]+1;
                    
                    r++;
                }
            }
            
        }
        if(r>k)
        printf("%d\n",work(r,k,1));
        else
        printf("%d\n",work(r,k,0));
    }
    return 0;
    
}                                 




中大周赛 2014年每周一赛第二场 Meet and Greet,布布扣,bubuko.com

中大周赛 2014年每周一赛第二场 Meet and Greet

原文:http://blog.csdn.net/min_lala/article/details/20838271

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!