`
java-mans
  • 浏览: 11468291 次
文章分类
社区版块
存档分类
最新评论

STL快速解题

 
阅读更多

http://ac.jobdu.com/problem.php?pid=1402 特殊的数

#include<iostream>
#include<bitset>
#include<cstdio>
using namespace std;

void read(int &data)     //快速读取数据
{
 char ch = getchar();
 while (ch < '0' || ch > '9')
	 ch = getchar();
 data = 0;
 do
 {
  data = data*10 + ch-'0';
  ch = getchar(); 
 }while (ch >= '0' && ch <= '9');
}

int main(void)
{
	int n,i,m,max,num,min;
	bitset<1000001> bits,b;

	while(scanf("%d",&n)!=EOF)
	{
		bits.reset();    //清空
		b.reset();
		max=-1;
		min=1000001;
		for(i=0;i<n;i++)
		{
			//scanf("%d",&m);
			read(m);
			if(m>max)
				max=m;
			if(m<min)
				min=m;
			if(bits[m]==0)    //这个数字第一次出现
				bits[m]=1;
			else if(bits[m]==1 && b[m]==0)   //这个数字第二次出现
				b[m]=1;
			//超过两次以上出现的数字不用在进行赋值操作了
		}
		num=0;
		for(i=min;i<=max;i++)
		{
			if(bits[i]==1 && !b[i])    //只出现了一次
				num++;
		}
		//num=bits.count()-b.count();

		printf("%d\n",num);
		if(num)
		{
			m=0;
			for(i=min;i<=max;i++)
			{
				if(b[i]==0 && bits[i]==1)
				{
					if(m==0)
					{
						printf("%d",i);
						m++;
					}
					else
					{
						printf(" %d",i);
						m++;
					}
					if(m==num)
					{
						printf("\n");
						break;
					}
				}
			}
		}
	}
	return 0;
}

http://ac.jobdu.com/problem.php?pid=1403 神奇的开关http://poj.org/problem?id=1176

#include<iostream>
#include<queue>
#include<cstdio>
#include<string>
#include<set>
using namespace std;
#include<memory.h>

struct node  
{
	string name;  //定义一个优先队列
	friend bool operator<(node a,node b)
	{
		return a.name > b.name;   //小到大(字典序)
	}
}w;

string str;
priority_queue<node > q;

set<string>myset;
int ON[101],OFF[101],n,i,j,k,p,r,t;
bool pos[101];

inline void solve()
{
	int h;
	if(i)
		memset(pos,false,sizeof(pos));
	else  //开
		memset(pos,true,sizeof(pos));
	if(j)    //奇数的灯改变状态
	{
		for(h=1;h<=n;h+=2)
			pos[h] ^=1;    //跟 pos[h] =1- pos[h];  是等效的
	}
	if(k)    //偶数的灯改变状态
	{
		for(h=1;h<=n;h++)
		{
			if((h&1)==0)    //由于==的优先级比与运算的优先级别要高,所以与运算需要加括号,导致了多次WA
				pos[h] ^=1;
		}
	}
	if(p)    //编号为(3 * K + 1)(K>=0)的灯改变状态
	{
		for(h=1;h<=n;h+=3)
		{
			if(h%3==1)
				pos[h] ^=1;
		}
	}
	for(h=0;h<r;h++)
	{
		if(!pos[ON[h]])
			return ;
	}
	for(h=0;h<t;h++)
	{
		if(pos[OFF[h]])
			return ;
	}
	str="";
	for(h=1;h<=n;h++)
	{
		if(pos[h])
			str+='1';
		else
			str+='0';
	}
	myset.insert(str);
}

int main(void)
{
	int c,m;
	set<string>::iterator iter;

	while(scanf("%d",&n)!=EOF)
	{
		myset.clear();
		scanf("%d",&c);
		r=t=0;

		while(1)
		{
			scanf("%d",&m);
			if(m==-1)
				break;
			ON[r++]=m;
		}

		while(1)
		{
			scanf("%d",&m);
			if(m==-1)
				break;
			OFF[t++]=m;
		}

		for(i=0;i<=1;i++)    //枚举状态
		{
			for(j=0;j<=1;j++)
			{
				for(k=0;k<=1;k++)
				{
					for(p=0;p<=1;p++)
					{
						if(i+j+k+p>c)
							break;
						if ((i+j+k+p)%2!=c%2)
							continue;
						solve();
					}
				}
			}
		}
		if(myset.empty())
			printf("IMPOSSIBLE\n");
		else
		{
			for(iter=myset.begin();iter!=myset.end();iter++)
				cout<<*iter<<endl;
		}
	}
	return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics