题解 | #游游的元素修改#

游游的元素修改

https://www.nowcoder.com/practice/2e3557c96138444db7eca60e8afaaac7

#include <algorithm>
#include <iostream>
#include "bits/stdc++.h"
using ll = long long;

using namespace std;
const int maxn = 2e5+10;
ll nums[maxn];
int main() {
    int t;
    scanf("%d",&t);
    while(t--){
        ll n,l,r;
        scanf("%lld%lld%lld",&n,&l,&r);
        ll d_maxr = 0,dr = 0,dl = 0,d_maxl=0;
        for(int i =0;i<n;i++){
            scanf("%lld",nums+i);
            if(nums[i]>l) d_maxr+=nums[i]-l;  
            if(nums[i]>r)  dr+=nums[i]-r; 
            if(nums[i]<l)   dl+=l-nums[i];
            if(nums[i]<r)   d_maxl+=r-nums[i];
        }
        ll ans = -1;
	  //查看比l小和比r大的元素的个数的范围情况,如果满足任一情况即可
        if(dr<=d_maxl&&dr>=dl||dl<=d_maxr&&dl>=dr)
            ans = max(dl,dr);
        // if(dr<=d_maxl&&dr>=dl)  ans = min(ans,dr);
        // if(ans == 2e18) printf("-1\n");
        printf("%lld\n",ans);
    }


}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

头像
03-20 22:00
重庆大学 Java
适彼乐土:“他们不行再找你” 最后的底牌吗?有点意思
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务