link
题意
有 个数和一个计数器 ,你可以随便排列它们这 个数,使得:
· 如果 , 增加 ;
· 如果 , 减少 。
你需要求出排列序列后, 的 最大值 和 最小值 分别是多少。
解法
目的就是求最长不下降子序列和最长下降子序列,剩余的再加上或补上就行了,所以给数组排序,再按题意模拟就行了。
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, a[100005];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)cin >> a[i];
int s = 0;
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++){
if(s >= a[i])s++;
if(s < a[i])s--;
}
cout << s << " ";
s = 0;
for(int i = n; i >= 1; i--){
if(s >= a[i])s++;
if(s < a[i])s--;
}
cout << s;
return 0;
}求赞求关!