一,思路:
這題我們可以通過二分 w來直接得到答案,時間復雜度是nlogn的級別,但是這里有個很坑的地方,就是假如你用二分做,會面臨報 long long 的問題,但是問題不大,直接用 unsigned long long 水過去,(long long ---->1e18,unsigned long long ---->1e19)。
二,代碼:
#include <iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;const int N=2e5+10,M=1e9+10;
typedef unsigned long long ll;
ll arr[N],n,c;bool check(ll w){ll sum=0;for(int i=0;i<n;i++){ll len=arr[i]+2*w;sum = sum + len*len;//主要面臨報 long long 的問題,就在這個求和的地方
//這里一定要在求的過程中就要判是否符合大于等于 c,
//不能求完再比,要不然unsigned long long 都不夠if(sum>=c) return true;}return false;
}void Solved(){cin>>n>>c;for(int i=0;i<n;i++) cin>>arr[i];//二分模板ll l=1,r= 4e9;while(l<r){ll mid=(l+r)>>1;if(check(mid)) r=mid;else l=mid+1;}cout<<l<<endl;
}int main()
{int t;cin>>t;while(t--) {Solved();}return 0;
}