Codeforces Round #456 (Div. 2) - (A,B,D)

这场codeforces打的非常惨,B题被HACK成功了两次,终于明白为什么比赛时通过了叫pratest passed而不是Accepeted,B题我思路还不对时就能通过两次,说明codeforces是故意留给HACK空间的,然后B题D题都是非常不错的题目,B题关于二进制位运算,D题是概率题,

A. Tricky Alchemy
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

During the winter holidays, the demand for Christmas balls is exceptionally high. Since it's already 2018, the advances in alchemy allow easy and efficient ball creation by utilizing magic crystals.

Grisha needs to obtain some yellow, green and blue balls. It's known that to produce a yellow ball one needs two yellow crystals, green — one yellow and one blue, and for a blue ball, three blue crystals are enough.

Right now there are A yellow and B blue crystals in Grisha's disposal. Find out how many additional crystals he should acquire in order to produce the required number of balls.

Input

The first line features two integers A and B (0 ≤ A, B ≤ 109), denoting the number of yellow and blue crystals respectively at Grisha's disposal.

The next line contains three integers xy and z (0 ≤ x, y, z ≤ 109) — the respective amounts of yellow, green and blue balls to be obtained.

Output

Print a single integer — the minimum number of crystals that Grisha should acquire in addition.

Examples
input
4 3
2 1 1
output
2
input
3 9
1 1 3
output
1
input
12345678 87654321
43043751 1000000000 53798715
output
2147483648
Note

In the first sample case, Grisha needs five yellow and four blue crystals to create two yellow balls, one green ball, and one blue ball. To do that, Grisha needs to obtain two additional crystals: one yellow and one blue.


题意:合成一个yellow球需要2个yellow元素,合成一个green球需要1个yellow元素加1个blud元素,合成一个blue球需要3个blue元素,现有A个yellow元素,B个blude元素,想要得到A,B,C个yellow,grean,blue球,问每种元素还需要多少

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll a,b,x,y,z;

int main()
{
    scanf("%I64d%I64d",&a,&b);
    scanf("%I64d%I64d%I64d",&x,&y,&z);
    ll t1=2*x;
    t1+=y;
    ll t2=y;
    t2+=3*z;
    if(t1-a>0)
        t1=t1-a;
    else t1=0;
    if(t2-b>0)
        t2=t2-b;
    else t2=0;
    printf("%I64d\n",t1+t2);
    return 0;
}

B. New Year's Eve
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Since Grisha behaved well last year, at New Year's Eve he was visited by Ded Moroz who brought an enormous bag of gifts with him! The bag contains n sweet candies from the good ol' bakery, each labeled from 1 to n corresponding to its tastiness. No two candies have the same tastiness.

The choice of candies has a direct effect on Grisha's happiness. One can assume that he should take the tastiest ones — but no, the holiday magic turns things upside down. It is the xor-sum of tastinesses that matters, not the ordinary sum!

A xor-sum of a sequence of integers a1, a2, ..., am is defined as the bitwise XOR of all its elements: , here denotes the bitwise XOR operation; more about bitwise XOR can be found here.

Ded Moroz warned Grisha he has more houses to visit, so Grisha can take no more than k candies from the bag. Help Grisha determine the largest xor-sum (largest xor-sum means maximum happiness!) he can obtain.

Input

The sole string contains two integers n and k (1 ≤ k ≤ n ≤ 1018).

Output

Output one number — the largest possible xor-sum.

Examples
input
4 3
output
7
input
6 6
output
7
Note

In the first sample case, one optimal answer is 12 and 4, giving the xor-sum of 7.

In the second sample case, one can, for example, take all six candies and obtain the xor-sum of 7.


题意:给出n,k,意思为可以从1~n中选不多余k个数,使得这些数按位相异或的结果最大

这里当k=1即只能选一个数时输出n即为最大

k>=2时,n化为二进制后每一位都换为1的值即为结果,比如n=8,K=2;

那么8化为二进制8->1000,我们只需在1~8中选7,7化为二进制7->0111,则(1111)XOR(0111)=1111,答案即为(2^5)-1

其实这里当k>=2时,我们也只需要选一个数,即能与n异或时互补的那个数,

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll n,k;

int main()
{
    scanf("%I64d%I64d",&n,&k);
    if(k==1)
    {
        printf("%I64d\n",n);
        return 0;
    }
    ll ans=1;
    while(n>0)
    {
        n>>=1;
        ans<<=1;
    }
    printf("%I64d\n",ans-1);
    return 0;
}

D. Fishes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size n × m, divided into cells of size 1 × 1, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!).

The gift bundle also includes a square scoop of size r × r, designed for fishing. If the lower-left corner of the scoop-net is located at cell (x, y), all fishes inside the square (x, y)...(x + r - 1, y + r - 1) get caught. Note that the scoop-net should lie completely inside the pond when used.

Unfortunately, Sasha is not that skilled in fishing and hence throws the scoop randomly. In order to not frustrate Sasha, Misha decided to release k fishes into the empty pond in such a way that the expected value of the number of caught fishes is as high as possible. Help Misha! In other words, put k fishes in the pond into distinct cells in such a way that when the scoop-net is placed into a random position among (n - r + 1)·(m - r + 1) possible positions, the average number of caught fishes is as high as possible.

Input

The only line contains four integers n, m, r, k (1 ≤ n, m ≤ 1051 ≤ r ≤ min(n, m)1 ≤ k ≤ min(n·m, 105)).

Output

Print a single number — the maximum possible expected number of caught fishes.

You answer is considered correct, is its absolute or relative error does not exceed 10 - 9. Namely, let your answer be a, and the jury's answer be b. Your answer is considered correct, if .

Examples
input
3 3 2 3
output
2.0000000000
input
12 17 9 40
output
32.8333333333
Note

In the first example you can put the fishes in cells (2, 1)(2, 2)(2, 3). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value.

Codeforces Round #456 (Div. 2) - (A,B,D)_第1张图片

题意:有一个n*m的方格(鱼塘),有一个r*r的渔网,我们要往鱼塘中放k条鱼,一小方块只能放一条鱼,有某种放鱼的方法使得一网捞到的鱼的数量的期望值最大,求次期望值

题目可以一条一条鱼的往池塘里放,每次都放到当前可以被最多的r*r矩形覆盖的点上,并记录该点被r*r矩形的覆盖的个数val,放进优先队列。

这里关键是我们从可以被最多的r*r矩阵覆盖的点开始放鱼(也就是可以最大概率有鱼的点),然后搜索此点周围的点。

这里先放进优先队列的点并不是我们要选择的点,而是选择队列中val最大的点

其实此做法可以转化为公式(p1/p2),p2是所有放网的情况,p1是所有情况下可以捞到的鱼的数量之和(最优方案下)

参考:http://codeforces.com/contest/912/submission/33958896

http://www.cnblogs.com/FxxL/p/8207466.html

代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;

ll n,m,r,k;
map,bool> vis; //用map记录点的访问情况,一个点(x,y)对应一个bool值
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

struct node{ //放鱼的点
    int x,y;
    ll val;
    bool operator <(const node & obj) const //在队列中按val升序排
    {
        return val que;
    node temp,tt;
    temp.x=(n+1)/2;
    temp.y=(m+1)/2;
    temp.val=fun(temp.x,temp.y);
    que.push(temp);         //先加入中心点
    vis[make_pair(temp.x,temp.y)]=true;

    ll sum=0;
    while(!que.empty())
    {
        temp=que.top();
        que.pop();
        sum+=temp.val; //这里是放一条鱼在点temp
        k--;
        if(k==0)      
            return sum;
        for(int i=0;i<4;i++)
        {
            tt.x=temp.x+dx[i];
            tt.y=temp.y+dy[i];
            if(tt.x>=1&&tt.x<=n&&tt.y>=1&&tt.y<=m&&!vis[make_pair(tt.x,tt.y)])
            {
                vis[make_pair(tt.x,tt.y)]=true;
                tt.val=fun(tt.x,tt.y);
                que.push(tt);
            }
        }
    }
    return sum;
}

int main()
{
    scanf("%I64d%I64d%I64d%I64d",&n,&m,&r,&k);
    double sumNet=(n-r+1)*(m-r+1); //总的撒网情况
    double ans=solve()/sumNet;
    printf("%.12f\n",ans);
    return 0;
}


你可能感兴趣的:(Codeforces,ACM比赛练习)