【枚举】【二分】【推导】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution

题意:有两个服务要求被满足,服务S1要求x1数量的资源,S2要求x2数量的资源。有n个服务器来提供资源,第i台能提供a[i]的资源。当你选择一定数量的服务器来为某个服务提供资源后,资源需求会等量地分担给它们,要求每台服务器承担的资源需求不超过其所能提供的资源需求。给定一种合法的方案,每台服务器要么没有被分配给任何一个服务,或者被分配给其中一个服务。

对服务器按能提供的资源从小到大排序。枚举给S1分配的服务器数量i,然后在a数组中二分,就可以得到给S1提供的是哪i台服务器,它们占据了a数组中连续的一段。

然后给S2哪些呢?分两种情况:①倘若a数组最后存在k台能满足S2,并且这k台不与给S1的相交,那么不妨就给它这最后的k台。

②倘若这i台之后,不存在a的某个后缀能够满足S2,那么预处理一个东西:pre[i]代表(n-i+1)-need[i](倘若要使用第i台那么至少要使用的总台数),然后对这个玩意求个前缀max,如果S1的i台前存在某个位置j,能使得pre[j]>=i,就是说j所对应的能空出来的台数大于等于S1占据掉的台数,那么不妨从第n台,倒着数need[j]台(跳过S1的i台),分配给S2即可。

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
struct data{
	int val,pos;
	data(){}
	data(const int &val,const int &pos){
		this->val=val;
		this->pos=pos;
	}
};
bool cmp(const data &a,const data &b){
	return a.val!=b.val ? a.val<b.val : a.pos<b.pos;
}
int n,m1,m2;
data a[300005];
int premax[300005],premaxpos[300005],need[300005];
int main(){
	//freopen("d.in","r",stdin);
	scanf("%d%d%d",&n,&m1,&m2);
	for(int i=1;i<=n;++i){
		scanf("%d",&a[i].val);
		a[i].pos=i;
	}
	sort(a+1,a+n+1,cmp);
	for(int i=1;i<=n;++i){
		int tmp;
		if(m2%a[i].val==0){
			tmp=m2/a[i].val;
		}
		else{
			tmp=m2/a[i].val+1;
		}
		if((n-i+1)-tmp>premax[i-1]){
			premax[i]=(n-i+1)-tmp;
			premaxpos[i]=i;
			need[i]=tmp;
		}
		else{
			premax[i]=premax[i-1];
			premaxpos[i]=premaxpos[i-1];
			need[i]=need[i-1];
		}
	}
	int hou=0;
	for(int i=n;i>=1;--i){
		if(m2<=(ll)a[i].val*(ll)(n-i+1)){
			hou=i;
			break;
		}
	}
	if(!hou){
		puts("No");
		return 0;
	}
	for(int i=1;i<=n;++i){
		int tmp;
		if(m1%i==0){
			tmp=m1/i;
		}
		else{
			tmp=m1/i+1;
		}
		data *p=lower_bound(a+1,a+n+1,data(tmp,0),cmp);
		if(p-a+i-1>n){
			continue;
		}
		if(hou>=p-a+i){
			puts("Yes");
			printf("%d %d\n",i,n-hou+1);
			for(int j=p-a;j<p-a+i;++j){
				printf("%d%c",a[j].pos,j==p-a+i-1 ? ‘\n‘ : ‘ ‘);
			}
			for(int j=hou;j<=n;++j){
				printf("%d%c",a[j].pos,j==n ? ‘\n‘ : ‘ ‘);
			}
			return 0;
		}
		else if(premax[p-a-1]>=i){
			puts("Yes");
			printf("%d %d\n",i,need[p-a-1]);
			for(int j=p-a;j<p-a+i;++j){
				printf("%d%c",a[j].pos,j==p-a+i-1 ? ‘\n‘ : ‘ ‘);
			}
			int cnt=0;
			for(int j=n;j>=1;--j){
				if(j>=p-a && j<p-a+i){
					continue;
				}
				++cnt;
				printf("%d%c",a[j].pos,cnt==need[p-a-1] ? ‘\n‘ : ‘ ‘);
				if(cnt==need[p-a-1]){
					break;
				}
			}
			return 0;
		}
	}
	puts("No");
	return 0;
}

原文地址:https://www.cnblogs.com/autsky-jadek/p/8975479.html

时间: 2024-08-08 05:22:22

【枚举】【二分】【推导】Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) D. Resource Distribution的相关文章

Codeforces Round #477 (rated, Div. 2, based on VK Cup 2018 Round 3) C. Stairs and Elevators【二分查找】

In the year of 30XX30XX participants of some world programming championship live in a single large hotel. The hotel has nn floors. Each floor has mm sections with a single corridor connecting all of them. The sections are enumerated from 11 to mm alo

Codeforces Round #470 (rated, Div. 2, based on VK Cup 2018 Round 1)C. Producing Snow+差分标记

题目链接:C. Producing Snow 题意:给两个数组v[N],T[N],v[i]表示第i天造的雪,T[i],表示第i天的温度,一堆雪如果<=T[i],当天就会融完,否则融化T[i],要求输出每天的融雪总量. 题解:我对T数组求个前缀和,就可以二分找到每堆雪在那一天(pos)融化,余下的要加进答案中ans[i],然后用一个an数组在a[i]+1,a[pos]-1,最后求再求一次前缀和. ans[i]再加上an[i]*t[i].每次操作二分logn,N次操作.复杂度O(nlogn) #in

Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2)

A. Tritonic Iridescence 题解:分类讨论.注意题目要求,至少有两种方案. 1 #pragma warning(disable:4996) 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<iostream> 6 #include<algorithm> 7 using namespace std; 8 9 int n, m; 10 stri

【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是,我们现在只需要统计sigma((L+f(L,K))),最后除以K即可. 统计sigma(L)时,我们考虑计算每条边出现在了几条路径中,设u为edgei的子节点,那么这条边对答案的贡献就是siz(u)*(n-siz(u)),siz(u)为u的子树大小. 统计sigma(f(L,K))时,我们需要dp出

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B

Description Bear Limak examines a social network. Its main functionality is that two members can become friends (then they can talk with each other and share funny pictures). There are n members, numbered 1 through n. m pairs of members are friends.

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C

Description In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if

Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) A

Description Bear Limak wants to become the largest of bears, or at least to become larger than his brother Bob. Right now, Limak and Bob weigh a and b respectively. It's guaranteed that Limak's weight is smaller than or equal to his brother's weight.

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心

E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k ≥ 0, 0 < r ≤ 2k. Let's call that representation prairie partition of x. For example, the p

Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) B. T-Shirt Hunt

B. T-Shirt Hunt time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participant