#include<iostream>usingnamespace std;constint N =500004;constint M =10004;int a[N], cnt[N];intmain(){
int n, phrase =0;// 整数个数,段数int i;
cin >> n;for(i =1; i <= n; i++){
scanf("%d",&a[i]);if(a[i]> a[i-1]){
cnt[a[i-1]+1]++;
cnt[a[i]+1]--;}}int sum =0;for(i =1; i < M; i++){
sum += cnt[i];
phrase =max(phrase, sum);}printf("%d\n", phrase);return0;}
题目3:岛
#include<iostream>#include<cstring>#include<algorithm>#include<map>typedeflonglong LL;usingnamespace std;constint N =100005,M =1e9+1;int a[N];
map<int,int>b;int n;intmain(){
cin >> n;for(int i =1; i <= n; i ++){
cin >> a[i];if(a[i]>a[i-1]){
//数的大小在[a[i-1],a[i]-1]之间的所有数大小都+1
b[a[i-1]]++,b[a[i]]--;}}
LL sum =0,res =0;for(auto i:b ){
//求前缀和
sum+=i.second;
res =max(res,sum);}
cout << res;}
文章评论