zoukankan      html  css  js  c++  java
  • 解题报告——POJ 2726

    Holiday Hotel
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 8204   Accepted: 3211

    Description

    Mr. and Mrs. Smith are going to the seaside for their holiday. Before they start off, they need to choose a hotel. They got a list of hotels from the Internet, and want to choose some candidate hotels which are cheap and close to the seashore. A candidate hotel M meets two requirements: 
    1. Any hotel which is closer to the seashore than M will be more expensive than M. 
    2. Any hotel which is cheaper than M will be farther away from the seashore than M.

    Input

    There are several test cases. The first line of each test case is an integer N (1 <= N <= 10000), which is the number of hotels. Each of the following N lines describes a hotel, containing two integers D and C (1 <= D, C <= 10000). D means the distance from the hotel to the seashore, and C means the cost of staying in the hotel. You can assume that there are no two hotels with the same D and C. A test case with N = 0 ends the input, and should not be processed.

    Output

    For each test case, you should output one line containing an integer, which is the number of all the candidate hotels.

    Sample Input

    5
    300 100
    100 300
    400 200
    200 400
    100 500
    0
    

    Sample Output

    2
    -------------------------------------------------
    这道题折腾了一个下午,最后还是看着网上的代码才弄明白的。
    二元组(D,C)作为一个候选者(candidate)的条件为:对任意不为(D,C)的二元组(D`,C`),存在以下不等式:
    1)若D`>D,则C`>C
    2)若C`>C,则D'>D
    我们先按D排序,对于Di观察其对应的Ci与Ci-1,...,C1之间的关系,可以得出:当Ci为当前出现过的最小C时,满足候选者条件。
    因此,问题最后转化为排序后对最小值的判断,问题解决。
    然而,我最初的C语言版本还是WA,不知道哪里出了问题,不过C++版本是可以用的。
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    #define maxn 10005
    
    struct Hotel
    {
        int c, d;
    } f[maxn];
    
    int n, ans;
    
    bool operator <(const Hotel &a, const Hotel &b)
    {
        if (a.c == b.c)
            return a.d < b.d;
        return a.c < b.c;
    }
    
    void input()
    {
        for (int i = 0; i < n; i++)
            scanf("%d%d", &f[i].c, &f[i].d);
    }
    
    void work()
    {
        ans = 0;
        int maxd = 100000000;
        for (int i = 0; i < n; i++)
            if (f[i].d < maxd)
            {
                ans++;
                maxd = f[i].d;
            }
    }
    
    int main()
    {
        //freopen("D:\t.txt", "r", stdin);
        while (scanf("%d", &n) != EOF && n != 0)
        {
            input();
            sort(f, f + n);
            work();
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    JavaScript(js)的replace问题的解决
    如何让Log4net日志文件按每月归成一个文件夹,StaticLogFileName参数的用法
    点到线的距离计算公式
    如何写一个Python万能装饰器,既可以装饰有参数的方法,也可以装饰无参数方法,或者有无返回值都可以装饰
    Python如何动态的为对象添加方法或属性,__slots__用法
    Python生成器的用法,使用生成器灵活的生成斐波那契数列
    Python函数名做参数,闭包,装饰器
    Python迭代器的用法,next()方法的调用
    property用法,使Python中的get方法和set方法使用更简单
    python中for循环删除不全的问题
  • 原文地址:https://www.cnblogs.com/codingpenguin/p/4281792.html
Copyright © 2011-2022 走看看