目录
Ⅰ 摄像头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.贪心策略和函数一定要写好,不然
等着你