cr545 - div2-b-cirus (简单数学,暴力

B. Circus
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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
.

之前做这类题就是在脑子思考几类物品直观的均衡分配的方案,最终会分类讨论出很多情况(就像高中的导数大题),因情况太多思考不下去不了了之;

看了题解发现就是找这几个量的关系式,然后再解方程即可;

因为这几个量之间的制衡关系较多,所以关系式并不那么直观,想时要细心列出满足每一个条件的式子;

题解: 分别设a,b,c,d为状态01 , 10 ,11 ,00这四种人的数量,a1,b1,c1,d1为第一次上场的数量;

则有   a+b+c+d=n/2; b1+c1+=a2+c2,即 b1+c1=a-a1+c-c1,即a+c=a1+b1+2c1;

此时a1 c1 b1 可直接枚举求解,但不够方便

联立 : a+c=n/2-d1+c1 枚举c1,d1求解即可,注意约束条件c1+d1<=n/2;

#include<bits/stdc++.h>
using namespace std;
int s[5005],ss[5005];

int main(){
   int a=0,b=0,c=0,d=0;
   int c1=0,d1=0;
   int n;
   scanf("%d",&n);
   char ch[5005];
   scanf("%s",ch);
   for(int i=0;i<n;i++)s[i]=ch[i]-'0';
   scanf("%s",ch);
   for(int i=0;i<n;i++)ss[i]=ch[i]-'0';
   for(int i=0;i<n;i++){
         if(s[i]==0&&ss[i]==1)a++;
         if(s[i]==1&&ss[i]==0)b++;
         if(s[i]==1&&ss[i]==1)c++;
         if(s[i]==0&&ss[i]==0)d++;
   }

   int flag=0;
   for(c1=0 ; c1<=c ; c1++){
         for(d1=0 ; d1<=d ; d1++){
                //cout<<c1<<' '<<d1<<endl;
                //cout<<a+c<<' '<<n/2-d1+c1<<endl;
             if( a+c == n/2-d1+c1&& n/2-c1-d1 >= 0 && n/2-c1-d1 <= a+b){
                 flag=1;
                 break;
             }
         }
         if(flag)break;
   }
   int  ab1=n/2-c1-d1,fr=1;//cout<<ab1<<' '<<c1<<' '<<d1<<endl;
   if(!flag)printf("-1");
   else{
     for(int i=0;i<n;i++){
         if(ab1>0){
             if(s[i]==0 && ss[i]==1 || s[i]==1 && ss[i]==0){
                  if(fr)fr=0,printf("%d",i+1); else printf(" %d",i+1); ab1--;
                }
         }
         if(s[i] ==1 && ss[i] == 1&&c1>0){
             c1--;
             if(fr)fr=0,printf("%d",i+1); else printf(" %d",i+1);
         }
         if(s[i] == 0 && ss[i] == 0 && d1>0){
             d1--;
            if(fr)fr=0,printf("%d",i+1); else printf(" %d",i+1);
         }
     }
   }
   return 0;
}
posted @ 2019-03-12 11:17  易如鱼  阅读(305)  评论(0编辑  收藏  举报