出题人的RP值(牛客练习赛38--A题)(排序)

链接:https://ac.nowcoder.com/acm/contest/358/A
来源:牛客网

题目描述

众所周知,每个人都有自己的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都是整数)

题目链接

链接:https://ac.nowcoder.com/acm/contest/358/A
来源:牛客网

 题意:中文题面,自行阅读。

思路:

首先把RP值的数组排序一下,

然后扫一边,碰到数值比当前自己的r人品值大的数就去膜。

注意:题目要保留3位小数,是严格的三位,多输出一位都算错的。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"==  "<<x<<"  =="<<endl;
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,x;
double ans=0.00;
int a[maxn];
int main()
{
    gg(n);
    gg(x);
    repd(i,1,n)
    {
        gg(a[i]);
    }
    ans=1.00*x;
    sort(a+1,a+1+n);
    repd(i,1,n)
    {
        if(a[i]>ans)
            ans=(ans+1.0*a[i])/2.000;
    }
    printf("%.3lf",ans);
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/qieqiemin/p/10289946.html
今日推荐