• 解题报告——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;
    }
  • 相关阅读:
    hadoop 学习(一)ubuntu14.04 hadoop 安装
    Java开发笔记(三十四)字符串的赋值及类型转换
    Java开发笔记(三十三)字符包装类型
    Java开发笔记(三十二)字符型与整型相互转化
    Java开发笔记(三十)大小数BigDecimal
    Java开发笔记(二十九)大整数BigInteger
    Java开发笔记(二十八)布尔包装类型
    Java开发笔记(二十七)数值包装类型
    Java开发笔记(二十六)方法的输出参数
    Java开发笔记(二十五)方法的输入参数
  • 原文地址:https://www.cnblogs.com/codingpenguin/p/4281792.html
Copyright © 2020-2023  润新知