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;
    }
  • 相关阅读:
    day67——前后端传输数据的编码格式、ajax传json数据/传文件、批量插入
    day66——choices参数、MTV/MVC模型、三种创建多对多的方式、AJAX
    day65——聚合函数、分组查询、F与Q查询、django开事务、orm查询优化
    dayⅢ、基本数据类型+运算符作业
    dayⅡ:编程语言+变量+垃圾回收制
    dayⅡ:变量作业
    dayⅠ:计算机基础知识
    ⅩⅥ:无参装饰器
    ⅩⅤ:作业
    ⅩⅤ:名称空间与作用域
  • 原文地址:https://www.cnblogs.com/codingpenguin/p/4281792.html
Copyright © 2011-2022 走看看