Codeforces Round #328 (Div. 2) (592A,592B,592C,592D)

PawnChess

题目链接:

http://codeforces.com/problemset/problem/592/A

解题思路:

Player A wins if the distance of his nearest pawn to the top of the board is less than or equal to the distance of the Player’s B nearest pawn to the bottom of the board (Note that you should only consider pawns that are not blocked by another pawns).

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

char board[10][10];

bool judge1(int i,int j){
    for(int x = i-1; x >= 0; x--)
        if(board[x][j] != '.')
            return false;
    return true;
}

bool judge2(int i,int j){
    for(int x = i+1; x < 8; x++)
        if(board[x][j] != '.')
            return false;
    return true;
}

int main(){
    int a,b;
    for(int i = 0; i < 8; i++)
        scanf("%s",board[i]);
    int flag = 0;
    for(int i = 0; i < 8; i++){
        for(int j = 0; j < 8; j++){
            if(board[i][j] == 'W' && judge1(i,j)){
                a = i;
                flag = 1;
                break;
            }
        }
        if(flag)
            break;
    }
    flag = 0;
    for(int i = 7; i >= 0; i--){
        for(int j = 0; j < 8; j++){
            if(board[i][j] == 'B' && judge2(i,j)){
                b = 7-i;
                flag = 1;
                break;
            }
        }
        if(flag)
            break;
    }
    if(a > b)
        printf("B\n");
    else
        printf("A\n");
    return 0;
}


The Monster and the Squirrel

题目链接:

http://codeforces.com/problemset/problem/592/B

解题思路:

After drawing the rays from the first vertex (n - 2) triangles are formed. The subsequent rays will generate independently sub-regions in these triangles. Let's analyse the triangle determined by vertices 1, i, i + 1, after drawing the rays from vertex i and (i + 1) the triangle will be divided into (n - i) + (i - 2) = n - 2 regions. Therefore the total number of convex regions is (n - 2)2

If the squirrel starts from the region that have 1 as a vertex, then she can go through each region of triangle (1, i, i + 1) once. That implies that the squirrel can collect all the walnuts in (n - 2)2 jumps.

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

typedef long long ll;

int main(){
    ll n;
    scanf("%lld",&n);
    printf("%lld\n",(n-2)*(n-2));
    return 0;
}


The Big Race

题目链接:

http://codeforces.com/problemset/problem/592/C

解题思路:

Let D be the length of the racetrack, Since both athletes should tie .

Let M = lcm(B, W), then D = k·M + r. None of the athletes should give one step further, therefore r ≤ min{B - 1, W - 1, T} = X.

D must be greater than 0 and less than or equal to T so  - r / M < k ≤ (T - r) / M.

For r = 0, the number of valid racetracks is , and for r > 0 the number of racetracks is .

Iterating over all possible r, we can count the number of racetracks in which Willman and Bolt ties: 

Note that . That means that  for exactly M values of r.

We can count the number of values of r in which , and the values of r in which . Each of the remaining values v1 - 1, v1 - 2, ..., v2 + 1 will appear exactly M times.

题目大意:

两个人A和B每走一步的距离分别为w,b。现在给一个跑道,跑道后面是悬崖,人不能掉进悬崖。问最终A和B谁离起点越远谁获胜。 

然后给一个长度L,问在长度L的范围里面,不能判断胜负的概率是多少。

算法思想:

1.当距离小于min(w,b)时,两人都在原点,不能判断出胜负。 
2.当距离为w,b的最小公倍数时,即 lcm(w,b)时,不能判断出胜负。 
3.当距离为 K*lcm(w,b)+min(w-1,b-1)时,两人都在 K * lcm(w,b)处,不能判断胜负。 
4.当跳完最后一个满足 K*lcm(w,b)<=t的K时,后面有一段距离要巧妙的处理,这时候要取 min(min(w-1,b-1),t%lcm(w,b))。主要是避

免 K * lcm(w,b) + min(w-1,b-1) > t的情况。

其中求lcm时,换成double型增大long long的范围。 

AC代码:

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long ll;
const double eps = 1e-9;
ll gcd(ll a,ll b){
    if(b == 0)
        return a;
    else
        return gcd(b,a%b);
}

int main(){
    ll t,w,b;
    while(~scanf("%lld%lld%lld",&t,&w,&b)){
        ll tmp = gcd(w,b);
        ll ans = min(min(w-1,b-1),t);
        if(1.0*w/tmp * b - t <= eps){
            ll  lcm = w/tmp *b;
            ans += t/lcm + (t/lcm-1)*min(w-1,b-1) + min(min(w-1,b-1),t%lcm);
        }
        tmp = gcd(ans,t);
        printf("%lld/%lld\n",ans/tmp,t/tmp);
    }
    return 0;
}


Super M

题目链接:

http://codeforces.com/problemset/problem/592/D

解题思路:

Observation 1: Ari should teleport to one of the attacked cities (it doesn't worth going to a city that is not attacked since then she should go to one of the attacked cities)

Observation 2: The nodes visited by Ari will determine a sub-tree T of the original tree, this tree is unique and is determined by all the paths from two attacked cities.

Observation 3: If Ari had to return to the city from where she started, then the total distance would be 2e, where e is the number of edges of T, that is because she goes through each edge forward and backward

Observation 4: If Ari does not have to return to the starting city (the root of T), then the total distance is 2e - L, where L is the distance of the farthest node from the root

Observation 5: In order to get a minimum total distance, Ari should chose one diameter of the tree, and teleport to one of its leaves.

The problem is now transformed in finding the diameter of a tree that contains the smallest index for one of its leaves. Note that all diameters pass through the center of the tree, so we can find all the farthest nodes from the center...and [details omitted].

题目大意:

给你一棵树,然后标记树上的m个点,问从哪个点出发走完所有被标记的点走过的边数最少。

算法思想:

首先我们求解的对象是在包含所有的被标记点的最小子树上进行移动,其他多余的边我们是没有必要走的。所以我们便以这棵子树为基础,假设从这棵子树上的某一点出发,遍历完所有的点之后回到该点需要走的边数是个固定值为这棵子树的边数的两倍,因为走过去还要走回来所以每条边均被走了两遍,但是根据我们题目的要求,其实遍历的最后一个点是没有必要返回的,因此答案应该为边数的两倍减去这两点之间的距离,要使结果最优,必然要使这两点之间的距离尽可能的大,使两点之间的距离的最大值便为树的直径,因此我们最终要求解的问题便转换成求解这棵子树并且这棵子树的直径。

AC代码:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值