2017暑期ACM俱乐部个人训练赛第3场 7.28


问题 A: Why Did the Cow Cross the Road

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

题目描述

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.


123

题意:从 (1,1) 到(n,n) 每走 3 步 就会 休息一下, 休息时间为maps[i][j]   每走一步花费时间为 t 

问 最少花费时间是多少?

DFS 超时,  用 BFS + 优先队列+ DP 维护

DP[x][y][step ]表示到达  x,y 这个点最短花费时间

#include <stdio.h>
#include <queue>
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N=11000;
const int INF=1<<29;
typedef long long ll;

int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n;
ll t;
ll ans;
int maps[120][120];
ll dp[120][120][600];
struct node{
    int x,y;
    ll sum;
    int step;
    friend bool operator  < (node n1, node n2)
    {
        return n1.sum > n2.sum;
    }
};

priority_queue <node>Q;

void bfs()
{
    while(!Q.empty())
        Q.pop();
    node first={1,1,0,0};
    Q.push(first);
    while(!Q.empty())
    {
        node now=Q.top();
        Q.pop();
        if(now.x==n&&now.y==n)
        {
            ans=min(ans,now.sum);
            continue;
        }
        for(int i=0;i<4;i++)
        {
            node then;
            int px=now.x+dir[i][0];
            int py=now.y+dir[i][1];
            then.x=px;
            then.y=py;
            if(then.x<=n&&then.x>0&&then.y<=n&&then.y>0)
            {
                then.step=now.step+1;
                then.sum=now.sum+t;
                if(then.step%3==0&&then.step>0)
                    then.sum+=maps[px][py];
                if(then.sum<dp[px][py][then.step%3])
                {
                        Q.push(then);
                        dp[px][py][then.step%3]=then.sum;
                }
            }
        }
    }
}
int main()
{
	while(~scanf("%d %lld",&n,&t))
	{
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                scanf("%d",maps[i]+j);
                for(int k=0;k<=600;k++)
                    dp[i][j][k]=INF;
            }

        ans=INF;
        bfs();
        cout<<ans<<endl;
	}
	return 0;
}


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

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

题目描述

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

题意: 

去最长公共子序列 

|a-b| <4  认为相等;

#include <stdio.h>
#include <queue>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int N=1010;

int a[N],b[N];
int dp[N][N];
int main()
{
    int n;
    while(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
提交: 79  解决: 34
[提交][状态][讨论版]

题目描述

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 个点, 2*n条线,  顺时针话点围成一个圈  问 相同的数字连起来 最多有几条交叉的数目


枚举+树状数组 维护

#include <iostream>
#include <queue>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <cmath>
#include <stdlib.h>
#include <set>
#include <stack>
#include <vector>
#define mem(v,b) memset(v,b,sizeof(v))
using namespace std;
const int INF=1<<29;
const int N =101000;
typedef long long ll;
int tree[N];
int vis[N];
int n;
void add(int k,int num)//维护添加值
{
	while(k<=2*n)
	{
		tree[k]+=num;
		k+=k&(-k);// lowbit(k)= k&(-k);
	}
}
int  Sum(int x)//求区间[1,x] 的和
{
	int sum=0;
	while(x)
	{
		sum+=tree[x];
		x-=x&(-x);
	}
	return sum;
}
int Get_sum(int y,int x)// x-y区间的和
{
	return Sum(y)-Sum(x);
}

int main()
{
    //freopen("input.txt","r",stdin);
    cin>>n;
    mem(vis,0);
    mem(tree,0);
    ll sum=0;
    int x;
    for(int i=1;i<=2*n;i++)
    {
        scanf("%d",&x);
        if(!vis[x])
        {
            vis[x]=i;
            add(i,1);
        }
        else
        {
            add(vis[x],-1);
            sum+=Get_sum(i,vis[x]);
            vis[x]=0;
        }
    }
    cout<<sum<<endl;
    return 0;
}



posted @ 2017-07-31 20:11  Sizaif  阅读(245)  评论(0编辑  收藏  举报