Nordic Collegiate Programming Contest 2015 E. Entertainment Box
Ada, Bertrand and Charles often argue over which TV shows to watch, and to avoid some of their fights they have finally decided to buy a video tape recorder. This fabulous, new device can record kk different TV shows simultaneously, and whenever a show recorded in one the machine's kk slots ends, the machine is immediately ready to record another show in the same slot.
The three friends wonder how many TV shows they can record during one day. They provide you with the TV guide for today's shows, and tell you the number of shows the machine can record simultaneously. How many shows can they record, using their recording machine? Count only shows that are recorded in their entirety.
Input Format
The first line of input contains two integers nn, kk (1 \le k < n \le 100 000)(1≤k<n≤100000). Then follow nn lines, each containing two integers x_i, y_ixi,yi, meaning that show ii starts at time x_ixi and finishes by time y_iyi. This means that two shows iiand jj, where y_i = x_jyi=xj, can be recorded, without conflict, in the same recording slot. You may assume that 0 \le x_i < y_i \le 1 000 000 0000≤xi<yi≤1000000000.
Output Format
The output should contain exactly one line with a single integer: the maximum number of full shows from the TV guide that can be recorded with the tape recorder.
样例输入1
3 1 1 2 2 3 2 3
样例输出1
2
样例输入2
4 1 1 3 4 6 7 8 2 5
样例输出2
3
样例输入3
5 2 1 4 5 9 2 7 3 8 6 10
样例输出3
3
题目来源
Nordic Collegiate Programming Contest 2015
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <string> 7 #include <map> 8 #include <cmath> 9 #include <set> 10 #include <algorithm> 11 using namespace std; 12 const int N=1e5+9; 13 struct Node 14 { 15 int s,e; 16 }node[N]; 17 bool cmp(Node a,Node b) 18 { 19 return a.e<b.e;//按终止时间从小到大 20 } 21 int n,k; 22 multiset<int>se;//可以存储等值元素 23 /* 24 1 8 25 2 9 26 5 10 27 7 10 28 _____ 29 就会有10 10 (k==2) 30 */ 31 multiset<int>::iterator it;//注意::写在前面 32 int main() 33 { 34 scanf("%d%d",&n,&k); 35 for(int i=0;i<n;i++ ) scanf("%d%d",&node[i].s,&node[i].e); 36 sort(node,node+n,cmp); 37 se.clear(); 38 for(int i=0;i<k;i++) se.insert(0); 39 int ans=0; 40 for(int i=0;i<n;i++) 41 { 42 it=se.upper_bound(node[i].s); 43 if(it==se.begin()) continue; 44 it--;//第一个>node[i].s的数的前面的数一定是小于node[i].sb_type 45 // 并且一定是和node[i].s的差值最小的(贪心),把更早结束的留给开始时间比较小的 46 se.erase(it);//播放完了,就要更新掉/ 47 se.insert(node[i].e); 48 ans++; 49 } 50 printf("%d\n",ans); 51 return 0; 52 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现