关闭

CodeForces 638 A Home Numbers(数学,水题)

103人阅读 评论(0) 收藏 举报
分类:
Description
The main street of Berland is a straight line with n houses built along it (n is an even number). The houses are located at both sides of the street. The houses with odd numbers are at one side of the street and are numbered from 1 to n - 1 in the order from the beginning of the street to the end (in the picture: from left to right). The houses with even numbers are at the other side of the street and are numbered from 2 to n in the order from the end of the street to its beginning (in the picture: from right to left). The corresponding houses with even and odd numbers are strictly opposite each other, that is, house 1 is opposite house n, house 3 is opposite house n - 2, house 5 is opposite house n - 4 and so on.


Vasya needs to get to house number a as quickly as possible. He starts driving from the beginning of the street and drives his car to house a. To get from the beginning of the street to houses number 1 and n, he spends exactly 1 second. He also spends exactly one second to drive the distance between two neighbouring houses. Vasya can park at any side of the road, so the distance between the beginning of the street at the houses that stand opposite one another should be considered the same.

Your task is: find the minimum time Vasya needs to reach house a.

Input
The first line of the input contains two integers, n and a (1 ≤ a ≤ n ≤ 100 000) — the number of houses on the street and the number of the house that Vasya needs to reach, correspondingly. It is guaranteed that number n is even.

Output
Print a single integer — the minimum time Vasya needs to get from the beginning of the street to house a.

Sample Input
Input
4 2
Output
2
Input
8 5
Output

3

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
	int n,a;
	while(scanf("%d %d",&n,&a)!=EOF)
	{
		int q[50001],w[50001];
		memset(q,0,sizeof(q));
		memset(w,0,sizeof(w));
		for(int i=1;i<=n/2;i++)
		{
			q[i]=2*i-1;
			w[i]=n-2*i+2;
			if(q[i]==a||w[i]==a)
			{
				printf("%d\n",i);
				break;
			}
		}
	}
	return 0;
}
题意:一个人开车出去玩。路两边是房子,上边是单数1~n-1,下边是偶数n~2;告诉你路边总共有多少房子,还有车经过两个房子间所花费时间为一分钟,问多长时间这个人才能到达他的目的地。

思路:直接定义两个数组保存路两边房子的编号,for循环运算n/2/次,直接每次赋值时判断是不是目的地就行了。是就输出i不是继续运行。

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

leetcode 204题求素数个数

Description: Count the number of prime numbers less than a non-negative number, n   提示晒数法: http://e...
  • wangyaninglm
  • wangyaninglm
  • 2015-04-28 22:20
  • 2121

Codeforces 11D A Simple Task 统计简单无向图中环的个数

状态压缩动态规划计算简单无向图中简单环的个数。
  • fangzhenpeng
  • fangzhenpeng
  • 2015-10-12 19:01
  • 1209

2016美赛MCM_A题解题思路

2016年美赛洗澡盆问题个人思路,如有错误,还请多多指教。
  • qq_27245709
  • qq_27245709
  • 2016-06-05 17:45
  • 3083

Codeforces 638A:Home Numbers(规律)

A. Home Numbers time limit per test 1 second memory limit per test 256 megabytes input s...
  • zugofn
  • zugofn
  • 2016-06-05 23:27
  • 207

Codeforces Round #426 (Div. 2)The Meaningless Game【数学题】【水题】

C. The Meaningless Game time limit per test 1 second memory limit per test 256 megabytes...
  • Archger
  • Archger
  • 2017-08-02 16:07
  • 220

ACMSTEP 1.2.7 Specialized Four-Digit Numbers //水题 模拟 进制转换

原题网址 Specialized Four-Digit Numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536...
  • fjn_lhxy
  • fjn_lhxy
  • 2011-08-08 11:08
  • 378

POJ 1504 Adding Reversed Numbers(我的水题之路——逆向数高精度加法)

Adding Reversed Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:...
  • shiow1991
  • shiow1991
  • 2012-01-30 17:33
  • 776

平常水题 - atcoder 062 - D - 3N Numbers

D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement ...
  • dreams___
  • dreams___
  • 2017-05-25 19:07
  • 76

平常水题 - Sheldon Numbers(逆向思维,位运算)

Sheldon Numbers According to Sheldon Cooper, the best number is 73. In his own words, “The best numb...
  • dreams___
  • dreams___
  • 2017-03-31 20:14
  • 452

POJ 1338 Ugly Numbers(我的水题之路——丑数2,3,5,质因子组成数)

Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15393   ...
  • shiow1991
  • shiow1991
  • 2012-01-28 23:04
  • 1245
    个人资料
    • 访问:35396次
    • 积分:2742
    • 等级:
    • 排名:第15082名
    • 原创:248篇
    • 转载:13篇
    • 译文:0篇
    • 评论:4条