【數學思維】【枚舉】codeforces1138B Circus

B. Circus
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarp is a head of a circus troupe. There are n — an even number — artists in the troupe. It is known whether the i-th artist can perform as a clown (if yes, then ci=1, otherwise ci=0), and whether they can perform as an acrobat (if yes, then ai=1, otherwise ai=0).

Split the artists into two performances in such a way that:

each artist plays in exactly one performance,
the number of artists in the two performances is equal (i.e. equal to n2),
the number of artists that can perform as clowns in the first performance is the same as the number of artists that can perform as acrobats in the second performance.
Input
The first line contains a single integer n (2≤n≤5000, n is even) — the number of artists in the troupe.

The second line contains n digits c1c2…cn, the i-th of which is equal to 1 if the i-th artist can perform as a clown, and 0 otherwise.

The third line contains n digits a1a2…an, the i-th of which is equal to 1, if the i-th artist can perform as an acrobat, and 0 otherwise.

Output
Print n2 distinct integers — the indices of the artists that should play in the first performance.

If there are multiple answers, print any.

If there is no solution, print a single integer −1.


 很好的一道題。做多了難題之後,不僅難題依舊不會,簡單題也不會了QwQ。一開始想,這該不會是某種神奇的dp吧,想了半天dp方程沒有得出結論。然後又想到一種方法,把原來的藝術家均分兩等分,計算出第一個集合c的和以及第二個集合a的和,然後交換兩個集合之間的元素,讓這兩個值的差爲0,元素一共只有00,01,10,11四種,分類討論交換的情況,但是發現湊出答案依舊不是一件很容易的事情。
 過了半小時有點自閉了。這才div2的第一題呀!想想最暴力的做法,無非是四重循環枚舉在第一個集合裏的四種元素的個數。等等!不用四重循環,如果第一重枚舉11,第二種枚舉10,那麼就可以得到此時第一個集合c的和-第二個集合a的和,然後可以求解出01元素第二個集合的個數,進而求出01元素第一個集合的個數,最後用集合大小相等求出00集合的分配。這樣看的話似乎只需要兩層循環了。附上代碼。

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;

vector<int> E[4];
int n,a[5005],c[5005];
char s1[5005],s2[5005];

int main()
{
	scanf("%d%s%s",&n,s1+1,s2+1);
	for(int i=1;i<=n;i++)
		c[i]=s1[i]-'0';
	for(int i=1;i<=n;i++)
		a[i]=s2[i]-'0';
	for(int i=1;i<=n;i++)
		E[c[i]*2+a[i]].push_back(i);
	for(int i=0;i<=E[3].size();i++)
		for(int j=0,k,p,q,r;j<=E[2].size();j++)
		{
			k=i*2-E[3].size()+j;
			if(k<0||k>E[1].size())
				continue;
			k=E[1].size()-k;
			p=i+j+k;
			q=E[3].size()+E[2].size()+E[1].size()-p;
			if(abs(p-q)>E[0].size())
				continue;
			r=(E[0].size()-p+q)>>1;
			if(r*2!=E[0].size()-p+q)
				continue;
			for(int s=0;s<i;s++)
				printf("%d ",E[3][s]);
			for(int s=0;s<j;s++)
				printf("%d ",E[2][s]);
			for(int s=0;s<k;s++)
				printf("%d ",E[1][s]);
			for(int s=0;s<r;s++)
				printf("%d ",E[0][s]);
			return 0;
		}
	printf("-1");
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章