出题人的RP值(C++)

出题人的RP值(C++)

点击做题网站链接

题目描述
众所周知,每个人都有自己的rp值(是个非负实数),膜别人可以从别人身上吸取rp值。
然而当你膜别人时,别人也会来膜你,互膜一段时间后,你们就平分了两人原有的rp值,当你膜过一个人之后,你就不能再膜那个人了
出题人发现自己的rp值为x,出题人周围有n个人,第i个人的rp值为a[i]
你要选择膜哪些人和膜人的顺序,使出题人的最终rp值最大

输入描述:
第一行两个数n,x,人数和出题人的初始rp值
第二行n个数,第i个数a[i]表示第i个人的rp值

输出描述:
一行一个数表示出题人的最终rp值(保留三位小数)

示例1
输入
1 0
1

输出
0.500

备注:
数据范围:
n<=100000,a[i]<=100000,x<=100000,(a[i],x都是整数)

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1e5 + 5;

int main() 
{
    int n;
    double x, a[N];
    cin >> n >> x;
    for(int i = 0; i < n; ++i)
        cin >> a[i];
    
    sort(a, a + n);
    
    for(int i = 0; i < n; ++i) 
        if (a[i] > x)
          x = (a[i] + x) * 0.5;
    
    printf("%.3f
", x);
    return 0;
}

笔记

关于sort()函数的用法:

  1. sort函数的时间复杂度为n*log2(n),执行效率较高。
  2. sort函数的形式为sort( first,end,method ),其中第三个参数可写可不写。
  3. 若为两个参数,则sort的排序默认是从小到大。
  4. 若为三个参数,则需要写一个“比较”函数,用于判断是从小到大排序还是从大到小排序。
#include <algorithm>
#include <iostream>
using namespace std;
 
bool com(int a,int b) { return a>b; }//“比较”函数
									 //使sort()函数排序从大到小
int main()
{
	int a[10] = { 0, 3, 1, 6, 9, 4, 5, 7, 2, 8 };
	for(int i=0;i<10;i++) cout << a[i] << endl;//打印原数列
	sort(a,a+10,com);//排序从大到小,不需要对com函数传入参数
	for(int i=0;i<10;i++) cout << a[i] << endl;//打印排序完后的数列
	return 0;
}
原文地址:https://www.cnblogs.com/yuzilan/p/10626125.html