当前位置:网站首页>区间问题 : 今年暑假不AC
区间问题 : 今年暑假不AC
2022-08-02 03:22:00 【寒江飞冰】
题目描述
“今年暑假不AC?”
“是的。”
“那你干什么呢?”
“看世界杯呀,笨蛋!”
“@#$%^&*%…”
确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会抛开电脑,奔向电视了。
作为球迷,一定想看尽量多的完整的比赛,当然,作为新时代的好青年,你一定还会看一些其它的节目,比如新闻联播(永远不要忘记关心国家大事)、非常6+7、超级女生,以及王小丫的《开心辞典》等等,假设你已经知道了所有你喜欢看的电视节目的转播时间表,你会合理安排吗?(目标是能看尽量多的完整节目)
输入
输入数据包含多个测试实例,每个测试实例的第一行只有一个整数n(n<=100),表示你喜欢看的节目的总数,然后是n行数据,每行包括两个数据Ti_s,Ti_e (1<=i<=n),分别表示第i个节目的开始和结束时间,为了简化问题,每个时间都用一个正整数表示。n=0表示输入结束,不做处理。
输出
对于每个测试实例,输出能完整看到的电视节目的个数,每个测试实例的输出占一行。
样例输入
12
1 3
3 4
0 7
3 8
15 19
15 20
10 15
8 18
6 12
5 10
4 14
2 9
0
样例输出
5
方法一:结构体+排序
#include<bits/stdc++.h>
using namespace std;
struct Node{
int a;
int b;
};
bool cmp(Node x,Node y)
{
return x.b<y.b;
}
int main()
{
int n;
while(cin>>n&&n!=0)
{
Node m[n];
for(int i=0;i<n;i++)
{
cin>>m[i].a>>m[i].b;
}
sort(m,m+n,cmp);
int count=1; // 排好序之后 第一个肯定是能看的 因为它的结束时间最早
int temp=m[0].b;
for(int i=1;i<n;i++) // 如果第二个的开始时间大于等于上一个的结束时间
{
if(m[i].a>=temp) // 第二个肯定能看 依次循环
{
count++;
temp=m[i].b;
}
}
cout<<count;
}
方法二:pair函数
#include<bits/stdc++.h>
using namespace std;
const int Max=10000;
int N,S[Max],T[Max];
pair<int,int> itv[Max];
void solve(){
for(int i=0;i<N;i++)
{
itv[i].first=T[i];//pair函数以first进行整体排序
itv[i].second=S[i];
}
sort(itv,itv+N);
int ans=0,t=0;
for(int i=0;i<N;i++)
{
if(t<=itv[i].second){
ans++;
t=itv[i].first;
}
}
cout<<ans<<endl;
}
int main()
{
while(cin>>N&&N!=0)
{
for(int i=0;i<N;i++)
{
cin>>S[i]>>T[i];
}
solve();
}
}
边栏推荐
- querystring模块
- STM32 触发HardFault_Handler如何查找原因
- 磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000
- np.unique()函数
- docker 安装 sqlserver中的坑点
- 【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
- 远程调试、无cuDnn、自定义模块无法导入问题记录
- basic operator
- Error: with open(txt_path,'r') as f: FileNotFoundError: [Errno 2] No such file or directory:
- 猴子选大王
猜你喜欢

subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.

云服务器安装部署Nacos2.0.4版本

Chapter 10 Clustering

Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案

Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide

nucleo stm32 h743 FREERTOS CUBE MX配置小记录

oracle内连接和外连接

环形链表---------约瑟夫问题

如何查看一个现有的keil工程之前由什么版本的keil IDE编译

Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000
随机推荐
URL模块
getattr()函数解析
SOCKS5
猴子选大王
The usage of json type field in mysql
pyppeteer使用样例脚本
小程序 van-cell 换行能左对齐问题
MySQL中JOIN的用法
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
Deveco studio 鸿蒙app访问网络详细过程(js)
Problems when yolov5 calls ip camera
Redis simple study notes
mysql阶段总结
由中序遍历和前序遍历得到后序遍历(树的遍历)
kettle 安装与配置
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
暴力方法求解(leetcode14)查找字符串数组中的最大公共前缀
meime模块
ssm各类配置模板
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000