Educational Codeforces Round 85 B. Middle Class(排序/贪心/水题)
Many years ago Berland was a small country where only nn people lived. Each person had some savings: the ii -th one had aiai burles.
The government considered a person as wealthy if he had at least xx burles. To increase the number of wealthy people Berland decided to carry out several reforms. Each reform looked like that:
- the government chooses some subset of people (maybe all of them);
- the government takes all savings from the chosen people and redistributes the savings among the chosen people equally.
For example, consider the savings as list [5,1,2,1][5,1,2,1] : if the government chose the 11 -st and the 33 -rd persons then it, at first, will take all 5+2=75+2=7 burles and after that will return 3.53.5 burles to the chosen people. As a result, the savings will become [3.5,1,3.5,1][3.5,1,3.5,1] .
A lot of data was lost from that time, so we don't know how many reforms were implemented and to whom. All we can do is ask you to calculate the maximum possible number of wealthy people after several (maybe zero) reforms.
The first line contains single integer TT (1≤T≤10001≤T≤1000 ) — the number of test cases.
Next 2T2T lines contain the test cases — two lines per test case. The first line contains two integers nn and xx (1≤n≤1051≤n≤105 , 1≤x≤1091≤x≤109 ) — the number of people and the minimum amount of money to be considered as wealthy.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109 ) — the initial savings of each person.
It's guaranteed that the total sum of nn doesn't exceed 105105 .
Print TT integers — one per test case. For each test case print the maximum possible number of wealthy people after several (maybe zero) reforms.
4 4 3 5 1 2 1 4 10 11 9 11 9 2 5 4 3 3 7 9 4 9
2 4 0 3
可以贪心地考虑,只需要超过x或者大于等于x就是富有的,那么何必多浪费钱呢?如果一个人的钱数超过x,就先++ans,再把超过的钱累加到变量sum里,最后按照钱数从小到大排序,从后往前扫一遍数组,对于不足x的人看看能否用sum使之钱数达到x。(为保证人数尽可能多,先满足钱数与x差距小的人)
注意:It's guaranteed that the total sum of nn doesn't exceed 105105. 所以这个复杂度是没问题的。
#include <bits/stdc++.h> using namespace std; int n,x,a[100005]; int main() { int t; cin>>t; while(t--) { scanf("%d%d",&n,&x); int i; int ans=0; long long sum=0;//sum存储的是超过x的钱数 for(i=1;i<=n;i++) { scanf("%d",&a[i]); if(a[i]>=x)ans++,sum+=a[i]-x; } sort(a+1,a+n+1); for(i=n;i>=1;i--) { if(sum<=0)break; if(a[i]<x) { if(x-a[i]<=sum) { ans++; sum-=(x-a[i]); } } } printf("%d\n",ans); } return 0; }
原文地址:https://www.cnblogs.com/lipoicyclic/p/12678567.html
- 1084: [SCOI2005]最大子矩阵
- 关于一般的并查集求根操作的一组对照研究
- vue计算属性详解——小白速会
- 【技巧】Java工程中的Debug信息分级输出接口及部署模式
- 1934: [Shoi2007]Vote 善意的投票
- 算法模板——线段树8 (字符串回文变换)
- 算法模板——哈希单模板字符串匹配
- javascript 原型及原型链详解
- 算法模板——并查集 2(支持快速即时查询本连通块内容,纯原创!)
- 1707: [Usaco2007 Nov]tanning分配防晒霜
- JavaScript 图片的上传前预览(兼容所有浏览器)
- 算法模板——sap网络最大流 3(递归+邻接表)
- BZOJ4819: [Sdoi2017]新生舞会(01分数规划)
- 3401: [Usaco2009 Mar]Look Up 仰望
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 线性表--顺序表--静态链表(八)
- 线性表--顺序表--循环链表(五)
- java学习之路:1.编译器Eclipse安装教程
- 指针作形参,需要注意的问题。
- SetConsoleTextAttribute函数用法
- C语言俄罗斯方块(旧版本)
- 线性表--顺序表--数组(三)
- 算法复杂度(二)
- 线性表--顺序表--单向链表(四)
- C语言俄罗斯方块(新版本完整代码)
- 线性表--顺序表--双向链表(六)
- C/C++什么时候使用二级指针,你知道吗?
- 萌新学习C++容易漏掉的知识点,看看你中招了没有(一)
- 萌新不看会后悔的C++string字符串常用知识点总结
- salesforce零基础学习(九十六)项目中的零碎知识点小总结(四)