题解:P14453 [ICPC 2025 Xi'an R] Grand Voting
题意
有一个变量
- 如果
s 不比当前判断数字小,s 加1 ; - 否则,
s 减1 。
找出
思路
看起来挺复杂的,但当我们看完样例以后发现,这种方案其实就是需要进行排序。样例还怕我们想复杂,特意加了最后一句的方法。
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5 ; //n 很大,所以定义一个常数变量。
int n , a[N] ;
long long s;
//最坏情况为 10 的 5 次方乘上 10 的 5 次方,为 10 的 10 次方,需要开 long long。
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//处理输入。
cin >> n ;
for(int i = 1 ; i <= n ; i++) cin >> a[i] ;
//进行排序。
sort(a + 1 , a + 1 + n);
//求出最大值。
for(int i = 1 ; i <= n ; i++){
if(s >= a[i]) s++;
else s--;
}
cout << s << " " ;
s = 0 ;
//求出最小值。
for(int i = n ; i > 0 ; i--){
if(s >= a[i]) s++;
else s--;
}
cout << s << endl ;
return 0;
}
如果您喜欢这篇题解,麻烦您点个赞,使这篇题解排名上升,谢谢!
(被打回)