USACO 2019 JANUARY SILVER

要打格式!!!

 

 

第一题看了半天,不就是求图中一点的度数再+1输出嘛,边输入边记录

#include
int v[100100]; 
int main()
{
	int n,flag=0,maxn=0;
	freopen("planting.in","r",stdin);
	freopen("planting.out","w",stdout); 
	scanf("%d",&n);
	for(int i=1;i

第二题dfs

#include
#include
using namespace std;
int N,now,now1,ans1,ans2,v[1100][1100],a[1100][1100];
char d;
void dfs(int x,int y)
{
	v[x][y]=1;
	++now;
	if(a[x-1][y]==1&&!v[x-1][y])dfs(x-1,y);
	if(a[x+1][y]==1&&!v[x+1][y])dfs(x+1,y);
	if(a[x][y-1]==1&&!v[x][y-1])dfs(x,y-1);
	if(a[x][y+1]==1&&!v[x][y+1])dfs(x,y+1);
	if(a[x-1][y]==0)++now1;if(a[x+1][y]==0)++now1;if(a[x][y-1]==0)++now1;if(a[x][y+1]==0)++now1;
}
int main()
{
	freopen("perimeter.in","r",stdin);
	freopen("perimeter.out","w",stdout);
	scanf("%d%c",&N,&d);
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			scanf("%c",&d);
			if(d=='#')a[i][j]=1;
		}
		scanf("%c",&d);
	}
	for(int i=1;i<=N;i++)
	{
		for(int j=1;j<=N;j++)
		{
			if(a[i][j]&&!v[i][j])
			{
				now=now1=0;
				dfs(i,j);
				if(now>ans1)ans1=now,ans2=now1;
				else if(now==ans1&&now1

第三题大概画一下草图,因为山是个底边在x轴上的等腰直角三角形,直接以右端点为第一关键字,左端点为第二关键字排序即可

#include
#include
using namespace std;
struct edge{
	int x1,x2;
}a[100100];
bool cmp(edge a,edge b)
{
	if(a.x2==b.x2)return a.x1b.x2;
}
int main()
{
	int n,ans=1;
	freopen("mountains.in","r",stdin);
	freopen("mountains.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x,y;
		scanf("%d%d",&x,&y);
		a[i].x1=x-y;
		a[i].x2=x+y;
	}
	sort(a+1,a+1+n,cmp);
	int lare1=a[1].x1,lare2=a[1].x2;
	for(int i=2;i<=n;i++)
	{
		if(a[i].x2!=lare2&&lare1>a[i].x1)
		{
			ans++;
			lare1=a[i].x1;
			lare2=a[i].x2;
		}
	}
	printf("%d",ans);
	fclose(stdin);
	fclose(stdout);
	return 0;
}

 

你可能感兴趣的:(usaco)