nkoj P5220/5221/5227 摄像头1 2 3

目录


Ⅰ 摄像头1

Ⅱ 摄像头2

Ⅲ 摄像头3

Ⅳ 总结归纳 

Q:今天我们讲什么呢? 

A:摄像头,摄像头,还是摄像头。 

这里有3道关于摄像头 的题目,今天我们一一解决它们

Ⅰ 摄像头1

Ⅱ 摄像头2 

Ⅲ 摄像头3 

附上代码: 

#include<bits/stdc++.h>
using namespace std;
long long n,ans=1,now;
struct node {
	long long x;
	long long y;
} sxt[100100];
bool cmp(node l,node r) {
	return l.y<r.y;
}
int main() {
	scanf("%lld",&n);
	for(long long i=1ll; i<=n; i++) scanf("%d %d",&sxt[i].x,&sxt[i].y);
	sort(sxt+1,sxt+1+n,cmp);
	now=sxt[1].y;
	for(long long i=2ll; i<=n; i++) {
		if(now<sxt[i].x) {
			ans++;
			now=sxt[i].y;
		}
	}
	printf("%lld",ans);
	return 0;
}
//摄像头1

#include<bits/stdc++.h>
using namespace std;
long long n,ans=1,now_Far,L,mxy;
struct node {
	long long x;
	long long y;
} sxt[100100];
bool cmp(node l,node r){
	if(l.x==r.x) return l.y>r.y;
	return l.x<r.x;
}
long long solve() { 
	if(sxt[1].x>0) return -1;
	now_Far=sxt[1].y;
	mxy=sxt[1].y;
	if(now_Far>=L) return ans;
	long long i=2ll;
	while(true){
		for(;i<=n&&sxt[i].x<=now_Far;i++) mxy=max(mxy,sxt[i].y);
		if(mxy<=now_Far) return -1;
		ans++;
		now_Far=mxy;
		if(now_Far>=L) return ans;
	}
	return -1;
}
int main(){
	scanf("%lld %lld",&n,&L);
	for(long long i=1ll; i<=n; i++) scanf("%lld %lld",&sxt[i].x,&sxt[i].y);
	sort(sxt+1,sxt+1+n,cmp);
	printf("%lld",solve());
	return 0;
}
//摄像头2


#include<bits/stdc++.h>
using namespace std;
int n,ans=1;
struct sxt {
	int x;
	int y;
} a[1000100] ;
bool cmp(sxt a,sxt b) {
	return a.y<b.y;
}
int main() {
	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 END=a[1].y;
	for(int i=2; i<=n; i++) {
		if(a[i].x>=END) {
			END=a[i].y;
			ans++;
		}
	}
	printf("%d",ans);
	return 0;
}
//摄像头3

Ⅳ 总结归纳

这三道题说明了什么呢?

1.结构体是很重要的,我们总是逃不出它的“怪圈”

2.贪心策略和CMP函数一定要写好,不然WA等着你

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值