当前位置:网站首页>区间问题 : 今年暑假不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();
}
}
边栏推荐
- 3 minutes to take you to understand WeChat applet development
- 猴子选大王
- oracle内连接和外连接
- np.unique() function
- 错误:with open(txt_path,‘r‘) as f: FileNotFoundError: [Errno 2] No such file or directory:
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- 磷脂-聚乙二醇-酰肼,DSPE-PEG-Hydrazide,DSPE-PEG-HZ,MW:5000
- Deveco studio Hongmeng app access network detailed process (js)
- MySQL中字符串比较大小(日期字符串比较问题)
- parser = argparse.ArgumentParser()解析
猜你喜欢

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu
[email protected]在static属性上的使用"/>@Autowired详解[email protected]在static属性上的使用

DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine supply, MW: 5000

【程序人生】做了多年的运维,靠什么转行拿下12K+年终奖的薪资?

新工程加载YOLOV6的预训练权重问题

配置mmdet来训练Swin-Transformer之一配置环境

C语言入门小游戏—三子棋

磷脂-聚乙二醇-巯基,DSPE-PEG-Thiol,DSPE-PEG-SH,MW:5000

啃瓜记录又一天

Redis simple study notes
随机推荐
The @autowired distinguished from @ the Resource
页面加载流程
活体检测 Adaptive Normalized Representation Learning for GeneralizableFace Anti-Spoofing 阅读笔记
pyppeteer使用样例脚本
针对简历上的问题
URL模块
Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG
npm和package.json
subprocess.CalledProcessError: Command ‘pip install ‘thop‘‘ returned non-zero exit status 1.
远程调试、无cuDnn、自定义模块无法导入问题记录
DSPE-PEG-DBCO 磷脂-聚乙二醇-二苯并环辛炔 一种线性杂双官能聚乙二醇化试剂
知识工程作业2:知识工程相关领域介绍
Debian 10 NTP Service Configuration
语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
化学试剂磷脂-聚乙二醇-羟基,DSPE-PEG-OH,DSPE-PEG-Hydroxyl,MW:5000
Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
PCL—点云数据分割
querystring模块
PCL—point cloud data segmentation