求调,样例通过,3pts
查看原帖
求调,样例通过,3pts
923364
OIerlb楼主2025/7/24 19:56
#include <bits/stdc++.h>

#define int long long

#define double long double

#define INF (int)(2e18)

#define maxn 300005

#define eps (double)(1e-6)

using namespace std;

int n,k;

int a[maxn];

bool check(int x){
	
	int ans = 0;
	
	for(int i = 1;i <= k;++ i){
		
		ans += (x % a[i] == 0 ? x / a[i] : x / a[i] + 1);
		
	}
	
	return ans <= n;
	
}

struct node{
	
	int b,id;
	
}bs[maxn];

bool cmp(node a,node b){
	
	if(a.b != b.b) return a.b < b.b;
	
	else return a.id > b.id;
	
}

priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;

unordered_map<int,bool> m;

int ansss[maxn];

signed main(){

	ios::sync_with_stdio(0);
	
	cin.tie(0);
	
	cout.tie(0);
	
	cin >> n >> k;
	
	for(int i = 1;i <= k;++ i) cin >> a[i];
	
	int l = 1,r = INF;
	
	int ans = l;
	
	while(l <= r){
		
		int mid = (r - l) / 2 + l;
		
		if(check(mid)) l = mid + 1,ans = mid;
		
		else r = mid - 1;
		
	}
	
//	cout << ans << "\n"; 
	
	int num = 0;
	
	for(int i = 1;i <= k;++ i){
		
		bs[i].b = (ans % a[i] == 0 ? ans / a[i] : ans / a[i] + 1);
		
		num += bs[i].b;
		
	}
	
	for(int i = 1;i <= k && num < n;++ i){
		
		if(ans % a[i] == 0) ++ bs[i].b,++ num;
		
	}
	
	for(int i = 1;i <= k;++ i){
		
		bs[i].b *= a[i]; 
		
		bs[i].id = i;
		
//		cout << bs[i].b << ' ' << bs[i].id << "&&\n";
		
		q.push({0,i});
		
	}
	
	sort(bs + 1,bs + k + 1,cmp);
	
	int anss = 0;
	
	for(int i = 1;i <= k;++ i){
		
		while(m[q.top().second] == 1 && !q.empty()) q.pop();
		
//		if(q.empty()) break;
		
//		cout << q.top().first << ' ' << bs[i].b << "\n";
		
		while((q.top().first < bs[i].b - a[bs[i].id] || (q.top().first + a[bs[i].id] == bs[i].b && q.top().second <= bs[i].id)) && !q.empty()){
			
			int wz = q.top().first,idd = q.top().second;
			
			q.pop();
			
			int nd = bs[i].b - wz - a[bs[i].id];
			
			nd = (nd % a[idd] == 0 ? nd / a[idd] : nd / a[idd] + 1);
			
			int xin_wz = wz + nd * a[idd];
			
			if(xin_wz == bs[i].b - a[bs[i].id] && idd <= bs[i].id) ++ nd,xin_wz += a[idd];
			
			int xin_idd = idd;
			
			 q.push({xin_wz,xin_idd});
			
			anss += nd;
			
//			cout << bs[i].id << ' ' << xin_wz << ' ' << xin_idd << ' ' << anss << ' ' << nd << "*\n";
			
		}
		
		m[bs[i].id] = 1;
		
		ansss[bs[i].id] = anss;
		
	}
	
	for(int i = 1;i <= k;++ i) cout << ansss[i] << " \n"[i == k];
	
	return 0;

}
/*

*/
2025/7/24 19:56
加载中...