第6次总结

感觉今天什么事都没干,这里忙一下,那里忙一下。。。

# [COCI2008-2009#2] PERKET

## 题目描述

Perket 是一种流行的美食。为了做好 Perket,厨师必须谨慎选择食材,以在保持传统风味的同时尽可能获得最全面的味道。你有 $n$ 种可支配的配料。对于每一种配料,我们知道它们各自的酸度 $s$ 和苦度 $b$。当我们添加配料时,总的酸度为每一种配料的酸度总乘积;总的苦度为每一种配料的苦度的总和。

众所周知,美食应该做到口感适中,所以我们希望选取配料,以使得酸度和苦度的绝对差最小。

另外,我们必须添加至少一种配料,因为没有任何食物以水为配料的。

## 输入格式

第一行一个整数 $n$,表示可供选用的食材种类数。

接下来 $n$ 行,每行 $2$ 个整数 $s_i$ 和 $b_i$,表示第 $i$ 种食材的酸度和苦度。

## 输出格式

一行一个整数,表示可能的总酸度和总苦度的最小绝对差。

## 样例 #1

### 样例输入 #1

```
1
3 10
```

### 样例输出 #1

```
7
```

## 样例 #2

### 样例输入 #2

```
2
3 8
5 8
```

### 样例输出 #2

```
1
```

## 样例 #3

### 样例输入 #3

```
4
1 7
2 6
3 8
4 9
```

### 样例输出 #3

```
1
```

## 提示

#### 数据规模与约定
对于 $100\%$ 的数据,有 1<=1e9,且将所有可用食材全部使用产生的总酸度和总苦度小于 1 \times 10^9,酸度和苦度不同时为0和1。
#### 说明
- 本题满分 $70$ 分。
思路:这个题因为数据比较小,我用的是暴力枚举的方法,也就是通过枚举全部的可能发生的情况来得到答案,把所有可能性中酸度的乘积与苦度的和用递归算出来,算出一个就放入数里面,通过打擂台的方式,来得到我们的最小值

#include<bits/stdc++.h>
using namespace std;
int n,s[25],b[25],ans=1000000000;
void f(int sd,int kd,int step)
{
	if(step==n)
	{
		int tmep=abs(sd-kd);
		if(tmep<ans&&kd!=0)
			ans=tmep;
			return ;
	}
	step++;
	f(sd*s[step],kd+b[step],step);
	f(sd,kd,step);
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	cin>>s[i]>>b[i];
	f(1,0,0);
	cout<<ans;
	return 0;
 } 

# [USACO08FEB]Meteor Shower S

## 题面翻译

贝茜听说了一个骇人听闻的消息:一场流星雨即将袭击整个农场,由于流星体积过大,它们无法在撞击到地面前燃烧殆尽,届时将会对它撞到的一切东西造成毁灭性的打击。很自然地,贝茜开始担心自己的安全问题。

以 Farmer John 牧场中最聪明的奶牛的名誉起誓,她一定要在被流星砸到前,到达一个安全的地方(也就是说,一块不会被任何流星砸到的土地)。

如果将牧场放入一个直角坐标系中,贝茜现在的位置是原点,并且,贝茜不能踏上一块被流星砸过的土地。 

根据预报,一共有 $M$ 颗流星 $(1\leq M\leq50,000)$ 会坠落在农场上,其中第i颗流星会在时刻 $T_i$ $(0\leq T_i\leq1,000)$ 砸在坐标为 $(X_i, Y_i)$ $(0\leq X_i\leq 300$,$0\leq Y_i\leq300)$ 的格子里。流星的力量会将它所在的格子,以及周围 $4$ 个相邻的格子都化为焦土,当然贝茜也无法再在这些格子上行走。

贝茜在时刻 $0$ 开始行动,它只能在第一象限中,平行于坐标轴行动,每 $1$ 个时刻中,她能移动到相邻的(一般是 $4$ 个)格子中的任意一个,当然目标格子要没有被烧焦才行。如果一个格子在时刻 $t$ 被流星撞击或烧焦,那么贝茜只能在 $t$ 之前的时刻在这个格子里出现。 贝西一开始在$(0,0)$。

请你计算一下,贝茜最少需要多少时间才能到达一个安全的格子。如果不可能到达输出  $-1$。

Translated by @奆奆的蒟蒻 @跪下叫哥

## 题目描述

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

## 输入格式

\* Line 1: A single integer: M

\* Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti

## 输出格式

\* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

## 样例 #1

### 样例输入 #1

```
4
0 0 2
2 1 2
1 1 2
0 3 5
```

### 样例输出 #1

```
5
```

这是我一开始的代码,提交后总是没过,跟学长交流后发现思路是对的,但就是哪里错了

#include<bits/stdc++.h>
using namespace std;
int dx[5]={-1,1,0,0,0},dy[5]={0,0,-1,1,0};
struct point{
	int x,y,t,step,visit;
}ps[1005][1005];
int main()
{
	int m=0,x=0,y=0,t=0,tx=0,ty=0;
	cin>>m;
	memset(ps,-1,sizeof(ps));
	for(int i=0;i<1000;i++)
	for(int j=0;j<1000;j++)
	{
		ps[i][j].x=i;
		ps[i][j].y=j;
	}
	for(int i=0;i<m;i++)
	{
		cin>>x>>y>>t;
		for(int j=0;j<5;j++)
		{
			tx=x+dx[j];
			ty=y+dy[j];
			if(dx<0||dy<0)
			continue; 
			if(ps[tx][ty].t==-1)
			{
				ps[tx][ty].t=max(t,ps[tx][ty].t);
			}
		}
	}
	queue<point>q;
	ps[0][0].visit=1;
	ps[0][0].step=0;
	q.push(ps[0][0]);
	while(!q.empty())
	{
		point p=q.front();
		q.pop();
			for(int i=0;i<4;i++)
			{
				tx=p.x+dx[i];
				ty=p.y+dy[i];
				if(tx<0||ty<0||ps[tx][ty].visit==1)
				continue;
				if(ps[tx][ty].t==-1)
				{
					cout<<p.step+1;	
					return 0;
				}
				if(p.step+1<ps[tx][ty].t)
				{
				ps[tx][ty].visit=1;
				ps[tx][ty].step=p.step+1;
				q.push(ps[tx][ty]);
				}
			}
	}
	cout<<-1;
}
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int x,y,time;
} p; 
int m,x,y,t,nx,ny,time1[305][305],c[305][305]; 
int b1[4]= {0,0,1,-1},b2[4]= {1,-1,0,0};
queue<node>q;
int main()
{
	cin>>m;
	memset(time1,-1,sizeof(time1));
	for(int i=0;i<m;i++)
	{
		cin>>x>>y>>t;
		if(time1[x][y]==-1||t<time1[x][y])
		time1[x][y]=t;
		for(int j=0;j<4;j++)
		{
			nx=b1[j]+x,ny=y+b2[j];
		if(nx>=0&&ny>=0&&(time1[nx][ny]==-1||t<time1[nx][ny]))
			time1[nx][ny]=t;
		}
	}
	p.x=0,p.y=0,p.time=0,c[0][0]=1;
	q.push(p);  
	while(!q.empty())
	{
		p=q.front();q.pop();
		for(int i=0; i<4; i++)
		{
			nx=p.x+b1[i],ny=p.y+b2[i];
			if(nx>=0&&ny>=0&&c[nx][ny]==0&&(time1[nx][ny]==-1||p.time+1<time1[nx][ny])) 
			{
				node txt;
				txt.x=nx,txt.y=ny,txt.time=p.time+1,c[nx][ny]=1;
				q.push(txt);
				if(time1[nx][ny]==-1) 
				{
					cout<<txt.time<<endl; 
					return 0;
				}
			}
		}
	}
	cout<<-1<<endl; 
	return 0;
}

这个是参考了别人部分代码的结果,终于发现了是哪里的问题,一开始我以为是bfs的错误,原来一直是矩阵的问题,少了东西

思路:我们先将时间表处理好,我一开始就是因为时间表中只想着上下左右的时间,而忽视了最原本的点(整无语了),时间表也就是那个地方,什么时候会被流星砸中的时间,处理好后,bfs将点压进循环,需要判断这个点我们是否走过了,对比我们现在的步数与时间表中时间的大小,当时间表为最初的-1时,跳出循环,要是bfs没找到点,那就代表着无解,输出-1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值