ACM暑假训练第3场中石油集训题(记忆化搜索,dp,树状数组)

本文整理了本次训练的ABC题和J题,共4题

问题 A: Why Did the Cow Cross the Road

时间限制: 1 Sec  内存限制:128 MB
提交: 148  解决: 23
[提交][状态][讨论版]

题目描述

Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of roads, making it impossible for his cows to travel around without crossing many of them. 
FJ's farm is arranged as an N×N square grid of fields (3N100), with a set of N1 north-south roads and N1east-west roads running through the interior of the farm serving as dividers between the fields. A tall fence runs around the external perimeter, preventing cows from leaving the farm. Bessie the cow can move freely from any field to any other adjacent field (north, east, south, or west), as long as she carefully looks both ways before crossing the road separating the two fields. It takes her T units of time to cross a road (0T1,000,000).

One day, FJ invites Bessie to visit his house for a friendly game of chess. Bessie starts out in the north-west corner field and FJ's house is in the south-east corner field, so Bessie has quite a walk ahead of her. Since she gets hungry along the way, she stops at every third field she visits to eat grass (not including her starting field, but including possibly the final field in which FJ's house resides). Some fields are grassier than others, so the amount of time required for stopping to eat depends on the field in which she stops.

Please help Bessie determine the minimum amount of time it will take to reach FJ's house.

输入

The first line of input contains N and T. The next N lines each contain N positive integers (each at most 100,000) describing the amount of time required to eat grass in each field. The first number of the first line is the north-west corner.

输出

Print the minimum amount of time required for Bessie to travel to FJ's house.

样例输入

4 2
30 92 36 10
38 85 60 16
41 13 5 68
20 97 13 80

样例输出

31

提示

The optimal solution for this example involves moving east 3 squares (eating the "10"), then moving south twice and west once (eating the "5"), and finally moving south and east to the goal.

【解析】:

bfs广搜+优先队列+dp维护

用bfs提交超时了。

所以优化了一下,价格限制条件dp[x][y][step]表示第step步到达(x,y)的最小值


其实step可以看做只有0,1,2。第3步可以从0重新计,以此类推。

【代码】:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const ll INF=0x3f3f3f3f3f3f3f;
int N,T;
struct node{
	int x,y;
	ll cost;
	int step;
	friend bool operator<(node a,node b)
	{
		return a.cost>b.cost;
	}
};
ll a[120][120];
ll dp[120][120][520];
int dir[5][2]={0,1,0,-1,1,0,-1,0};
priority_queue<node>q;
ll bfs()
{
	while(!q.empty())q.pop();
	node u={1,1,0,0};
	q.push(u);
	ll ans=INF;
	while(!q.empty())
	{
		u=q.top();q.pop();
		if(u.x==N&&u.y==N){
			ans=min(ans,u.cost);
			continue;
		}
			
		for(int i=0;i<4;i++)
		{
			node v;
			v.x=u.x+dir[i][0];
			v.y=u.y+dir[i][1];
			if(v.x>0&&v.x<=N&&v.y>0&&v.y<=N){
				v.cost=u.cost+T;
				v.step=u.step+1;
				if(v.step%3==0&&v.step>0){
					v.cost+=a[v.x][v.y];
				}
				if(v.cost<dp[v.x][v.y][v.step%3]){
					q.push(v);	
					dp[v.x][v.y][v.step%3]=v.cost;
				}
			}
		}
	}
	return ans;
}
int main()
{
	scanf("%d%d",&N,&T);
	for(int i=1;i<=N;i++)
		for(int j=1;j<=N;j++){
			scanf("%lld",a[i]+j);
			for(int t=0;t<420;t++)
				dp[i][j][t]=INF;
		}
	printf("%lld\n",bfs());
	return 0;
}

问题 B: Why Did the Cow Cross the Road II

时间限制: 1 Sec  内存限制:128 MB
提交: 61  解决: 23
[提交][状态][讨论版]

题目描述

Farmer John raises N breeds of cows (1N1000), conveniently numbered 1N. Some pairs of breeds are friendlier than others, a property that turns out to be easily characterized in terms of breed ID: breeds a and b are friendly if |ab|4, and unfriendly otherwise. 
A long road runs through FJ's farm. There is a sequence of N fields on one side of the road (one designated for each breed), and a sequence of N fields on the other side of the road (also one for each breed). To help his cows cross the road safely, FJ wants to draw crosswalks over the road. Each crosswalk should connect a field on one side of the road to a field on the other side where the two fields have friendly breed IDs (it is fine for the cows to wander into fields for other breeds, as long as they are friendly). Each field can be accessible via at most one crosswalk (so crosswalks don't meet at their endpoints).

Given the ordering of N fields on both sides of the road through FJ's farm, please help FJ determine the maximum number of crosswalks he can draw over his road, such that no two intersect.

输入

The first line of input contains N. The next N lines describe the order, by breed ID, of fields on one side of the road; each breed ID is an integer in the range 1N. The last N lines describe the order, by breed ID, of the fields on the other side of the road. Each breed ID appears exactly once in each ordering.

输出

Please output the maximum number of disjoint "friendly crosswalks" Farmer John can draw across the road.

样例输入

6
1
2
3
4
5
6
6
5
4
3
2
1

样例输出

5

【解析】:

dp求最长公共子序列。

只不过相差4以内就看做相等。

【代码】:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
int n;
int a[1200],b[1200];
int dp[1200][1200];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		cin>>b[i];
	
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(abs(a[i]-b[j])<=4){
				dp[i][j]=dp[i-1][j-1]+1;
			}
			else {
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	cout<<dp[n][n]<<endl;
	return 0;
}

问题 C: Why Did the Cow Cross the Road III

时间限制: 1 Sec  内存限制:128 MB
提交: 43  解决: 14
[提交][状态][讨论版]

题目描述

The layout of Farmer John's farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn. 
As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns N cows, conveniently identified with the integer IDs 1N, so there are precisely 2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.

Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a "crossing" pair if cow a's path from entry to exit must cross cow b's path from entry to exit. Please help Farmer John count the total number of crossing pairs.

输入

The first line of input contains N (1N50,000), and the next 2N lines describe the cow IDs for the sequence of entry and exit points around the field.

输出

Please print the total number of crossing pairs.

样例输入

4
3
2
4
4
1
3
2
1

样例输出

3

【解析】:

扫一遍,一边连线,一遍计数。树状数组维护。复杂度n*log(n)


扫到没出现过的数字就标记一下。

扫到第二次出现的数字,那么这两个相同数字之间的所有带标记的数,都与之有交叉,记下交叉数。累加即可


同时把已经出现过两次的数字,抹掉标记,因为这条连线已经被累加过了

【代码】:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
int c[102000];
int vis[52000];
void add(int k,int num)
{
	while(k<=n){
		c[k]+=num;
		k+=k&-k;
	}
}
int sum(int k)
{
	int ans=0;
	while(k){
		ans+=c[k];
		k-=k&-k;
	}
	return ans;
}
int main()
{
	scanf("%d",&n);
	n=n*2;
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		if(!vis[x]){
			vis[x]=i;
			add(i,1);
		}
		else{
			add(vis[x],-1);
			ans+=sum(i)-sum(vis[x]);
			vis[x]=0;
		}
	}
	printf("%d\n",ans);
	return 0;
}

问题 J: 【贪心】取数游戏

时间限制: 1 Sec  内存限制:64 MB
提交: 180  解决: 85
[提交][状态][讨论版]

题目描述

给出2n(n≤100)个自然数(小于等于30000)。将这2n个自然数排成一列,游戏双方A和B从中取数,只允许从两端取数。A先取,然后双方轮流取数。取完时,谁取得数字总和最大为取胜方;若双方和相等,属B胜。试问A方是否有必胜策略?

输入

共2行,第1行一个整数n;第2行有2*n个自然数。

输出

只有1行,若A有必胜策略,则输出“YES”,否则输出“NO”。

样例输入

4
7 9 3 6 4 2 5 3

样例输出

YES

【解析】:

如果A一直按最好的取法取,只会产生两种情况。

1、A==B

2、A>B

如果A==B,那么这组数可以均匀的分成两块,且相等,则这些数做位运算亦或,结果为0。

否则A!=B,其异或结果不会为0。

【代码】:

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{
	int n,a;
	cin>>n;
	int ans=0;
	for(int i=0;i<2*n;i++){
		cin>>a;
		ans=ans^a;
	}
	if(ans)puts("YES");
	else puts("NO");
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值