[Daily] 20200813

C题.[Codeforces 125C] Hobbits’ Party

题目链接

思路: 有两个要求:一个人最多被邀请2天,任意两天一定有同一个人被邀请。
所以最优方案一定是任意两天被邀请的人中, 只有一个人相同。也就是说任意两天消耗一个人,k天消耗 k*(k-1)/2 个人,那么最大天数k应该是满足 n>=k*(k-1)/2 的最大的k
得到k之后,再k2枚举任意的两天消耗掉一个人的过程,得到结果序列。

//AC代码
#include<bits/stdc++.h>
using namespace std;
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n,k,cnt;
vector<int>ans[1000];
int main(){
	cin>>n;
	for(int i=2;i<=n;i++)
		if(n>=i*(i-1)/2)k=i;else break;
	cout<<k<<endl;
	for(int i=1;i<=k;i++)
		for(int j=i+1;j<=k;j++){
			ans[i].push_back(++cnt);
			ans[j].push_back(cnt);
		}
	for(int i=1;i<=k;i++){
		for(auto j:ans[i])printf("%d ",j);
		puts("");
	}
}

猜你喜欢

转载自blog.csdn.net/qq_45530271/article/details/107990889