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

2023-11-06 23:38
文章标签 codeforces round div 456

本文主要是介绍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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
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.


题意:有一个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<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;ll n,m,r,k;
map<pair<int,int>,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<obj.val;}
};ll fun(ll x, ll y) //计算放在点x,y的鱼可以被多少r*r矩阵包括
{return (min(n,x+r-1)-max(x,r)+1)*(min(m,y+r-1)-max(y,r)+1);
}ll solve()
{priority_queue<node> 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; //这里是放一条鱼在点tempk--;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 Round #456 (Div. 2) - (A,B,D)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359757

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There