Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:3000MS以下是如何做到的??

Posted by tasty at 2013-01-19 21:59:02 on Problem 1903
In Reply To:3000MS以下是如何做到的?? Posted by:ZxMYS at 2009-04-01 12:10:08
> orz
这样做的:(复杂度O(N*2^(N/2)))
Source Code

Problem: 1903  User: tasty 
Memory: 264K  Time: 47MS 
Language: C++  Result: Accepted 

Source Code 
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct pp
{
    int id,value,cnt;
    bool operator <(const pp &temp)const
    {
         if(value==temp.value)
             return cnt>temp.cnt;
         return value<temp.value;
    }
};
int a[30],ansstate,n[2];
pp list[2][1<<12];
char tmp[1000000];
int main()
{
	int N,i,j,s,p,q;
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	{
		scanf("%s",tmp);
		int state=0;
        for(int j=0;tmp[j];j++)
		   state|=(1<<(tmp[j]-'A'));
	    a[i]=state;
	}
	int ans=0,orz[2];
	n[0]=N/2;
	n[1]=N-n[0];
	orz[0]=0;
	orz[1]=n[0];
	for(i=0;i<2;i++)
	   for(j=0;j<(1<<n[i]);j++)
	   {
           list[i][j].value=list[i][j].cnt=0;
           list[i][j].id=j;
           for(s=0;s<n[i];s++)
           {
               if(j&(1<<s))
               {
                  list[i][j].value^=a[s+orz[i]+1];
                  list[i][j].cnt++;
               }
           }
       }
    sort(list[0],list[0]+(1<<n[0]));
    sort(list[1],list[1]+(1<<n[1]));
    for(i=0,j=0;i<(1<<n[0])&&j<(1<<n[1]);)
    {
         if(list[0][i].value<list[1][j].value)
             i++;
         else if(list[0][i].value>list[1][j].value)
             j++;
         else
         {
             if(list[0][i].cnt+list[1][j].cnt>ans)
             {
                 ans=list[0][i].cnt+list[1][j].cnt;
                 ansstate=list[0][i].id+(list[1][j].id<<n[0]);
             }
             i++;
         }
    }
	printf("%d\n",ans);
	for(int i=0;i<N;i++)
		if(ansstate&(1<<i))	printf("%d ",i+1);
	printf("\n");
//	system("pause");
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator