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

本博客探讨了在特定农场环境中,两头牛在路径上的移动模式及其社交互动,通过模拟每一步的坐标来计算它们之间的'哞'叫声次数。详细分析了输入格式、输出格式,并提供了实例输入输出,帮助理解如何解决此类问题。

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

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;
    
}                                 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值