(第一次周赛)Drazil and Date【CodeForces - 515A】

Drazil and Date

题目 [CodeForces - 515A]

Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. Drazil’s home is located in point (0, 0) and Varda’s home is located in point (a, b). In each step, he can move in a unit distance in horizontal or vertical direction. In other words, from position (x, y) he can go to positions (x + 1, y), (x - 1, y), (x, y + 1) or (x, y - 1).

Unfortunately, Drazil doesn’t have sense of direction. So he randomly chooses the direction he will go to in each step. He may accidentally return back to his house during his travel. Drazil may even not notice that he has arrived to (a, b) and continue travelling.

Luckily, Drazil arrived to the position (a, b) successfully. Drazil said to Varda: “It took me exactly s steps to travel from my house to yours”. But Varda is confused about his words, she is not sure that it is possible to get from (0, 0) to (a, b) in exactly s steps. Can you find out if it is possible for Varda?

Time limitMemory limitTagsSource
1000 ms32768 kBmath *1100Codeforces Round #292 (Div. 2)

Input

You are given three integers a, b, and s ( - 109 ≤ a, b ≤ 109, 1 ≤ s ≤ 2·109) in a single line.

Output

If you think Drazil made a mistake and it is impossible to take exactly s steps and get from his home to Varda’s home, print “No” (without quotes).
Otherwise, print “Yes”.

Example

InputOutput
5 5 11No
10 15 25Yes
0 5 1No
0 0 2Yes

Note

In fourth sample case one possible route is:

问题链接: CodeForces - 515A

问题描述

先放个中文翻译吧(记得提高英文水平啊,作者本人)

有一天,Drazil想和瓦尔达约会。德拉吉和瓦尔达生活在笛卡尔平面上。Drazil的家位于点(0,0),瓦尔达的家位于点(A,B)。在每个步骤中,他可以在水平或垂直方向上以单位距离移动。换言之,从位置(x,y)他可以到达位置(x+1,y)、(x-1,y)、(x,y+1)或(x,y_1)。
不幸的是,Drazil没有方向感。所以他随机选择每个方向的方向。在旅途中,他可能不小心回到家里。德拉齐尔甚至没有注意到他已经到达(A,B)并继续旅行。
幸运的是,Drazil成功地到达了(A,B)的位置。Drazil对瓦尔达说:“我从我的房子到你家走了整整一步。”但是瓦达对他的话感到困惑,她不能确定是否可以按正确的步骤从(0,0)到(a,b)。你能查明瓦尔达是否可能吗?

问题分析

从位置(x,y)他可以到达位置(x+1,y)、(x-1,y)、(x,y+1)或(x,y_1)。

Drazil可能已经到了Varda家但是它不知道,换言之就是可以经过点(a,b)但还继续走,这时如果步数不能让他回到(a,b)点也要输出“No”

我们要做的就是去判断Drazil所说的步数到底能不能走到Varda的家,如果不能我们就输出“No”,如果能我们就输出“Yes

(其实我蛮想把一次一次的WA过程以及原因都说出来的题目不难但是我WA了好多次)

其实我想的比较复杂,赛后听完别人的阐述之后觉得别人的简单很多。

代码分析

作为坐标,a,b的值可能是负数(这个在Example中没有给出,一开始就没有考虑到)【所以就要先把a、b变成正数,这样在相加的时候才不会出现错误】

首先把a、b变成正数
这是我的实现方法:

        if (a < 0)
		{
			a = -a;
		}
		if (b < 0)
		{
			b = -b;

其实还是复杂了(当时还不知道绝对值的函数(ಥ﹏ಥ))
其实可以这样干的

#include<cmath>//C语言是math.h(头文件)
a = abs(a);//求a的绝对值
b = abs(b);//求b的绝对值

(其实还是还是觉得用一个变量去储存(a+b)的值好一点,不然在书写上有点麻烦)

如果a和b都等于0那么就是在原点,这是步数一定是偶数才能回到原点
【这一步其实是看到Example里面的0 0 2想到的】

       if ((a == 0 && b == 0) && s % 2 == 0)
		{
			cout << "Yes" << endl;
		}

如果s刚好等于(a+b),那么是可以的也是最短的程

当然也是可以输出“Yes”的

我处理这个的代码如下

else if ((a + b) == s)
		{
			cout << "Yes" << endl;
		}

接下来判断当s>(a+b)时的情况
我当时在桌上话了很多次
就是当(a+b)与s的奇偶类型相同时就可以到达
所以实现的代码如下(略显复杂了)

else if (((a + b) < s) && ((a + b) % 2 == 0 && s % 2 == 0))
		{
			cout << "Yes" << endl;
		}
		else if (((a + b) < s) && ((a + b) % 2 != 0 && s % 2 != 0))
		{
			cout << "Yes" << endl;
		}

如果不是的话就输出“No
else cout << "No" << endl;

AC通过的CPP全部代码如下

#include<iostream>
using namespace std;
int main()
{
	int a, b, s;
	while (cin >> a >> b >> s)
	{
		if (a < 0)
		{
			a = -a;
		}
		if (b < 0)
		{
			b = -b;
		}
		if ((a == 0 && b == 0) && s % 2 == 0)
		{
			cout << "Yes" << endl;
		}
		else if ((a + b) == s)
		{
			cout << "Yes" << endl;
		}
		else if (((a + b) < s) && ((a + b) % 2 == 0 && s % 2 == 0))
		{
			cout << "Yes" << endl;
		}
		else if (((a + b) < s) && ((a + b) % 2 != 0 && s % 2 != 0))
		{
			cout << "Yes" << endl;
		}
		else cout << "No" << endl;
	}
	return 0;
}

More

【赛后听了一下别人的想法】
其实只要当步数s大于x+y时若s-(x+y)是偶数则能到达,若为奇数则不能到达

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值