Buying A House (Codeforces-796A)

本文介绍了一道Codeforces上的编程题,主人公需在有限预算内购买离心仪女孩住所最近的房子。文章给出了题目的背景故事、输入输出说明及示例,并提供了一种简单直接的解题思路与代码实现。

题目链接:

http://codeforces.com/problemset/problem/796/A

A. Buying A House
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us.

The girl lives in house m of a village. There are n houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house n. The village is also well-structured: house i and house i + 1 (1 ≤ i < n) are exactly 10meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased.

You will be given n integers a1, a2, ..., an that denote the availability and the prices of the houses. If house i is occupied, and therefore cannot be bought, then ai equals 0. Otherwise, house i can be bought, and ai represents the money required to buy it, in dollars.

As Zane has only k dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love.

Input

The first line contains three integers nm, and k (2 ≤ n ≤ 1001 ≤ m ≤ n1 ≤ k ≤ 100) — the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively.

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 100) — denoting the availability and the prices of the houses.

It is guaranteed that am = 0 and that it is possible to purchase some house with no more than k dollars.

Output

Print one integer — the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy.

Examples
input
5 1 20
0 27 32 21 19
output
40
input
7 3 50
62 0 0 0 99 33 22
output
30
input
10 5 100
1 0 1 0 0 0 0 0 1 1
output
20

题目大意:

n套房子在同一条直线上,编号为1-n,每间房子之间的距离为10米,价格已知(价格为0元:表示该房子已被别人购买)女孩住在编号为m的房子里,男孩有k元钱,想买一套房子,需要满足两个条件:1 距离女孩的房子最近 2 价格<=k元。题目要求输出最终男孩买的房子距离女孩房子的距离。第一行输入n m k,加下来n个整数表示每套房子的价格。

解题思路:

水题,这道题不会做应该是题意不懂,建议大家明白题意后,自己编写代码。

代码:

#include<iostream>
using namespace std;
int main()
{
    int n,m,k,a[110];
    while(cin>>n>>m>>k)
    {
        for(int i=0;i<=n-1;i++)
            cin>>a[i];
        int left=0,right=0;
        int zr=0,zl=0;
        for(int i=m;i<=n-1;i++)
        {
            right=right+10;
            if(a[i]<=k&&a[i]!=0)
            {
                zr=1;
                break;
            }
        }
        for(int i=m-2;i>=0;i--)
        {
            left=left+10;
            if(a[i]<=k&&a[i]!=0)
            {
                zl=1;
                break;
            }
        }
        if(zr==0)
            cout<<left<<endl;
        else if(zl==0)
            cout<<right<<endl;
        else
            cout<<min(right,left)<<endl;
    }
    return 0;
}


### 关于 Codeforces Problem 1802A 目前提供的引用内容并未涉及 Codeforces 编号为 1802A 的题目详情或解决方案[^1]。然而,基于常见的竞赛编程问题模式以及可能的解决方法,可以推测该类题目通常围绕算法设计、数据结构应用或者特定技巧展开。 如果假设此题属于典型的算法挑战之一,则可以从以下几个方面入手分析: #### 可能的方向一:字符串处理 许多入门级到中级难度的问题会考察字符串操作能力。例如判断子串是否存在、统计字符频率或是执行某种转换逻辑等。以下是 Python 中实现的一个简单例子用于演示如何高效地比较两个字符串是否相匹配: ```python def are_strings_equal(s1, s2): if len(s1) != len(s2): return False for i in range(len(s1)): if s1[i] != s2[i]: return False return True ``` #### 方向二:数组与列表的操作 另一常见主题是对整数序列进行各种形式上的变换或者是查询最值等问题。下面给出一段 C++ 程序片段来展示快速寻找最大元素位置的方法: ```cpp #include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a(n); for(auto &x : a){ cin>>x; } auto max_it = max_element(a.begin(),a.end()); cout << distance(a.begin(),max_it)+1; // 输出索引加一作为答案 } ``` 由于具体描述缺失,在这里仅提供通用框架供参考。对于确切解答还需要访问实际页面获取更多信息后再做进一步探讨[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值