「USACO 做题笔记」USACO 2011 Nov Bronze

做题时间:2022.8.9
不能算 VP,没给自己定闹钟(

Contest Timing(ctiming)

题意

11.11   11  ⁣ :  ⁣ 11 11.11\ 11\!:\!11 11.11 11:11 11. D H  ⁣ :  ⁣ M 11.\text D\ \text H\!:\!\text M 11.D H:M 所经过的分钟数。

思路

数据较小,特判掉不合法情况后直接模拟即可。
时间复杂度约为 O ( DHM ) O(\text{DHM}) O(DHM)

代码

#include<bits/stdc++.h>
using namespace std;

int main()
{
	int bd=11,bh=11,bm=11,d,h,m,ans=0;
	cin>>d>>h>>m;
	if(bd>d||bd==d&&bh>h||bd==d&&bh==h&&bm>m)
	{
		puts("-1");
		return 0;
	}
	while(bd!=d||bh!=h||bm!=m)
	{
		if(bm<59) bm++;
		else
		{
			bm=0;
			if(bh<23) bh++;
			else
			{
				bh=0;
				bd++;
			}
		}
		ans++;
	}
	cout<<ans;
 	return 0;
}

Awkward Digits(digits)

题意

给出一个二进制数和一个三进制数,两个数都有一位相较于答案来说是错误的,求十进制状态下的答案。

思路

枚举二进制每一位,取反,转成三进制看是否与输入的三进制有一位错误,正确则输出。
时间复杂度约为 O ( log ⁡ 2 n × log ⁡ 3 n × log ⁡ 3 n ) O(\log_2n\times\log_3n\times\log_3n) O(log2n×log3n×log3n)

代码

#include<bits/stdc++.h>
using namespace std;

int to10(string s)
{
	int res=0;
	for(int i=0;i<s.size();i++)
	{
		res=res*2+s[i]-48;
	}
	return res;
}

string to3(int x)
{
	string res="";
	while(x)
	{
		res=char(x%3+48)+res;
		x/=3;
	}
	return res;
}

int main()
{
	string a,b;
	cin>>a>>b;
	for(int i=0;i<a.size();i++)
	{
		for(int j=0;j<b.size();j++)
		{
			string newa=a;
			newa[i]=!(newa[i]-48)+48;
			int x=(to10(newa));
			newa=to3(x);
			bool f=0;
			if(newa.size()!=b.size()) continue;
			for(int i=0;i<b.size();i++)
			{
				if(newa[i]!=b[i])
				{
					if(f) goto p;
					f=1;
				}
			}
			if(f)
			{
				cout<<x;
				return 0;
			}
			p:;
		}
	}
 	return 0;
}

Moo Sick(moosick)

题意

给出长度为 n n n 的序列 a a a 和长度为 c c c 的序列 b b b,求 a a a 中有多少方案使得连续 c c c 个数经过排序或同时减去相同数得到的答案和 b b b 相同,并输出每种方案的起始位置。

思路

先将 b b b 从小到大排序,然后暴力枚举起始位置,将每次处理出的排序然后判断每两个数之间的差是否与 b b b 的那两个数的差相同,相同则记录。
时间复杂度 O ( n c log ⁡ c ) O(nc\log c) O(nclogc)

代码

#include<bits/stdc++.h>
using namespace std;

int n,m,a[20005],b[20005],c[20005],k,ans[20005];

int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	cin>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>b[i];
	}
	sort(b+1,b+m+1);
	for(int i=1;i+m-1<=n;i++)
	{
		for(int j=i;j<=i+m-1;j++) c[j]=a[j];
		sort(c+i,c+m+i);
		//cerr<<"DEBUG 1:"<<i<<','<<c[i]<<' ';
		for(int j=i+1;j<=i+m-1;j++)
		{
			//cerr<<c[i]<<' ';
			if(c[j]-c[j-1]!=b[j-i+1]-b[j-i]) goto p;
		}
		ans[++k]=i;
		p:;
		//cerr<<endl;
	}
	cout<<k<<endl;
	for(int i=1;i<=k;i++)
	{
		cout<<ans[i]<<endl;
	}
 	return 0;
}

Cow Beauty Pageant(pageant)

题意

给出一个矩阵,其中有两个连通块,问最少染色多少个点使得这两个连通块相连。

思路

先随机取一个连通块,dfs 一遍这个块,将所有涉及到的坐标记录下来。然后对于每一个坐标进行 bfs,求出最少的染色个数。
时间复杂度 O ( n 2 m 2 ) O(n^2m^2) O(n2m2)

代码

#include<bits/stdc++.h>
using namespace std;

const int X[]={1,0,-1,0};
const int Y[]={0,1,0,-1};
int n,m,a[55][55],c[55][55],f[55][55],sx,sy,k,qx[250005],qy[250005],qdep[250005],ans=INT_MAX;
pair<int,int>b[2505];

void dfs(int x,int y)
{
	//cerr<<"DEBUG 4:"<<x<<' '<<y<<endl;
	b[++k]={x,y};
	c[x][y]=1;
	a[x][y]=2;
	for(int i=0;i<4;i++)
	{
		int x_=x+X[i],y_=y+Y[i];
		if(x_>0&&x_<=n&&y_>0&&y_<=m&&a[x_][y_]&&!c[x_][y_]) dfs(x_,y_);
	}
}

int bfs(int bx,int by)
{
	int h=1,t=1,res=INT_MAX;
	qx[h]=bx;
	qy[h]=by;
	qdep[h]=0;
	while(h<=t)
	{
		int x=qx[h],y=qy[h],dep=qdep[h++];
		//cerr<<"DEBUG 1:"<<x<<' '<<y<<' '<<dep<<' '<<res<<endl;
		if(x<1||y<1||x>n||y>m||f[x][y]||a[x][y]==2&&h!=2) continue;
		if(a[x][y]==1) /*cerr<<"DEBUG 3:"<<dep<<endl,*/res=min(res,dep);
		f[x][y]=1;
		for(int i=0;i<4;i++)
		{
			qx[++t]=x+X[i];
			qy[t]=y+Y[i];
			qdep[t]=dep+1;
		}
	}
	return --res;
}

int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			char c;
			cin>>c;
			a[i][j]=c=='X'?1:0;
			if(!sx&&!sy&&c=='X') sx=i,sy=j;
		}
	}
	dfs(sx,sy);
	//cerr<<"DEBUG 2:"<<k<<endl;
	for(int i=1;i<=k;i++)
	{
		memset(f,0,sizeof(f));
		ans=min(ans,bfs(b[i].first,b[i].second));
	}
	cout<<ans;
 	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值