专栏文章
题解:AT_arc197_b 大于均值
AT_arc197_b题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mipcokd0
- 此快照首次捕获于
- 2025/12/03 09:50 3 个月前
- 此快照最后确认于
- 2025/12/03 09:50 3 个月前
AT_arc197_b 大于均值
解题思路
-
首先对序列 排序,并计算其前缀和。
目标是选出一个子序列,其得分为大于子序列均值的元素数量最大。
定义得分为 。 -
对于每个可能的子序列长度 ,累加排序后前 个元素的和 。
设固定的 数量对应于被子序列舍弃的前缀个数 ,使得剩余部分全部大于平均值。
判断条件为 并使用双指针寻找最小满足条件的 。 -
最终得分即为最大值 ,
其中 表示选取长度为 的子序列中大于均值的元素个数。
时间复杂度为 。
#include<bits/stdc++.h>
using namespace std;
void FileIO(){
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
namespace sunburstfan{
#define int long long
const int mod=1e9+7;
const int N=1e5+5;
const int INF=1e18+5;
void solve(){
int n;
cin>>n;
vector<int> a(n+1,INF);
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a.begin(),a.end());
int sum=0,k=0,ans=0;
for(int i=1;i<=n;i++){
sum+=a[i-1];
while(k<i&&a[k]*i<=sum)k++;
ans=max(ans,i-k);
}
cout<<ans<<"\n";
}
}
using namespace sunburstfan;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T=1; cin>>T;
while(T--){
solve();
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...