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



问题 A: Why Did the Cow Cross the Road

时间限制: 1 Sec   内存限制: 128 MB

题目描述

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.


题目大意:

牛  每走一格耗时T   每走三格   停下来吃一次草  耗时为方阵中的数字    从左上角走到右下角   问最短耗时


分析:

思路很简单关键在于时间限制      话说dp+搜索又叫记忆化搜索



AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
using namespace std;
int N,T;

struct node{
    int x;
    int y;
    int step;
    int sum;
    friend bool operator < (node a, node b){
        return a.sum>b.sum;
    }
};
int map[150][150];
int vis[150][150][5];
int dir[4][2]={1,0,0,1,-1,0,0,-1};
int judge(int x,int y){
    if(x<0||y<0||x>=N||y>=N)
        return 0;
    return 1;
}
void bfs(){
    priority_queue<node> Q;
    node start;
    start.step=0;
    start.sum=0;
    start.x=0;
    start.y=0;
    Q.push(start);
    while (!Q.empty()){
        node temp=Q.top();
        Q.pop();
        for (int k=0;k<4;k++){
            node tt;
            tt.x=temp.x+dir[k][0];
            tt.y=temp.y+dir[k][1];
            if(judge(tt.x,tt.y)){
                tt.step=temp.step+1;
                if(tt.step%3==0)
                    tt.sum=temp.sum+map[tt.x][tt.y]+T;
                else
                    tt.sum=temp.sum+T;
                if(vis[tt.x][tt.y][tt.step%3]>tt.sum||!vis[tt.x][tt.y][tt.step%3]){
                    Q.push(tt);
                    vis[tt.x][tt.y][tt.step%3]=tt.sum;
                }

            }
        }
    }
}
int main (){
    while (scanf ("%d%d",&N,&T)!=EOF){
        for (int i=0;i<N;i++){
            for (int j=0;j<N;j++){
                scanf ("%d",&map[i][j]);
            }
        }
        memset(vis,0,sizeof(vis));
        bfs();
        printf("%d\n",min(vis[N-1][N-1][0],min(vis[N-1][N-1][1],vis[N-1][N-1][2])));
    }
    return 0;
}





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

时间限制: 1 Sec   内存限制: 128 MB

题目描述

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



题目大意:

前N个数为一排  后N个数为一排     前N个数与后N个数连线    不能交叉且连线的两数之差的绝对值不大于4      问最大能连多少条线


分析:

类似于最长公共子序列




AC代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
int a[1005];
int b[1005];
int dp[1005][1005];
int main (){
    int n;
    while (scanf("%d",&n)!=EOF){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(dp,0,sizeof(dp));
        for (int i=1;i<=n;i++)
            scanf ("%d",&a[i]);
        for (int j=1;j<=n;j++)
            scanf ("%d",&b[j]);
        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]);
            }
        }
        printf ("%d\n",dp[n][n]);
    }
    return 0;
}






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

时间限制: 1 Sec   内存限制: 128 MB

题目描述

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



题目大意:

2N个数   按顺时针围成一圈    相同的数连线    问有多少个交点


分析:

树状数组可以解决   统计一下两个相同的数字之间有多少个未被统计的数字   即逐渐去掉连线  每去掉一根线少几个点  加一下就是总的交点数




AC代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
const int maxn=100000+10;
int c[maxn];
int a[maxn];
int vis[maxn];
void add(int k,int num){
    while (k<=maxn){
        c[k]+=num;
        k+=(k&-k);
    }
}
int read(int k){
    int sum=0;
    while (k){
        sum+=c[k];
        k-=(k&-k);
    }
    return sum;
}
int main(){
    int n;
    //freopen("data","r",stdin);
    while (scanf("%d",&n)!=EOF){
        memset(c,0,sizeof(c));
        memset(a,0,sizeof(a));
        memset(vis,false,sizeof(vis));
        int ans=0;
        for (int i=1;i<=2*n;i++){
            scanf("%d",&a[i]);
            if(!vis[a[i]]){
                add(i,1);
                vis[a[i]]=i;
            }
            else{
                add(vis[a[i]],-1);
                ans+=(read(i)-read(vis[a[i]]));
            }
        }
        printf ("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值