CSU-1751 卡牌通行证(贪心&&结构体排序)
2016-06-28 09:00
375 查看
CSU-1751 卡牌通行证(结构体排序)
Time Limit: 1 Sec Memory Limit: 128 MBDescription
A城市的宴会宾客进场都是要有对应编号的卡牌的,在A城市的各个商店中都能买到这样的卡牌,不同标号的卡牌价钱都一样为单位1,最近这段时间有n场宴会,每场宴会都有自己要求对应的卡牌编号,这些编号保证是连续,也就是说你只要有标号为[ai,bi]中的任意一张卡牌你都能进入这个宴会。小明现在很渴望参加宴会并在其中找到自己心仪的女生,所以他每场都不想错过,他现在已经知道了每场宴会对应要求的编号,所以他跑到了商店要买需要的卡,他希望花的钱尽可能少,请你计算一下花费的最少的钱是多少。
Input
第一行输入一个正整数T,表示T组数据
每组数据第一行输入一个正整数n,表示n场宴会
接下来n行,每行输入两个正整数a , b 表示这场宴会需要有[a,b]这个闭区间的卡牌才能进
Output
每组数据输出一个花费的最少的钱
Sample Input
1
3
5 8
1 3
2 9
Sample Output
2
题意:求在这些[ai,bi]区间里面最优解,贪心取区间的问题。结构体排序,在遍历考虑包含关系,若无关不断地更新a[i].y。
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <ctime> #include <set> #include <map> #include <cmath> using namespace std; #define pi 3.14159265359 typedef long long LL; const int maxn = 1001000; int n, h, k; struct node{ int x,y; }a[maxn]; int cmp(node aa,node bb){ if(aa.y!=bb.y) return aa.y<bb.y; else return aa.x>bb.x; } int main(){ int t; scanf("%d",&t); while(t--){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d %d",&a[i].x,&a[i].y); } sort(a+1,a+1+n,cmp); int cnt = 1; for(int i=2;i<=n;i++){ if(temp < a[i].x){ temp = a[i].y; cnt++; } } printf("%d\n",cnt); } return 0; }
相关文章推荐
- 【HDU 5366】The mook jong 详解
- HDU 5240 Exam (好水的题)
- 北大—1006——Biorhythms
- 时间计算(heaven.pas/cpp)
- 工作依赖(job.cpp/pas)
- 题目 英雄 (BFS)
- kmp 学习 hihocoder #1015
- HDU 1096 A+B for Input-Output Practice (VIII)
- HUST-1601 - Shepherd 暴力
- P1478
- P1035
- P1008 难度2.7
- 2020 绝对值排序
- 2021 发工资咯
- 2022 海选女主角
- 2024 C语言合法标识符
- 2025 查找最大元素
- 2026 首字母变大写
- 2027 统计元音
- 2028 Lowest Common Multiple Plus