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:
- Any hotel which is closer to the seashore than M will be more expensive than M.
- 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\n", ans); } return 0; }
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· golang中写个字符串遍历谁不会?且看我如何提升 50 倍
· C# 代码如何影响 CPU 缓存速度?
· 智能桌面机器人:使用 .NET 为树莓派开发 Wifi 配网功能
· C# 模式匹配全解:原理、用法与易错点
· 记一次SSD性能瓶颈排查之路——寿命与性能之间的取舍
· 时隔半年,拾笔分享:来自一个大龄程序员的迷茫自问
· C#-Visual Studio工具使用实践
· 《程序员的底层思维》读后感
· 不写一行代码 .NET 使用 FluentCMS 快速构建现代化内容管理系统(CMS)
· 曾经风光无限的 Oracle DBA 已经落伍了吗?